Inheritance, object class, keyword

Posted by anu123 on Sun, 31 Oct 2021 23:05:53 +0100

Inheritance, object class, keyword

Introduction to Object Class

The Object class is the most primitive and important class in the C#language, the "ancestor" of all classes, and each C#class is its

Subclass, which implements the basic methods that each class must have.

Attributes and methods in an Object class can be used in any class.

When a programmer specifies a class's parent when he defines it, the compiler defaults to the class inheriting from the Object class.

Four commonly used methods are provided in the Object class, namely Equals, GetHashCode, GetType, and

ToString method.

1. Equals method

The Equals method is primarily used to compare whether two objects are equal, returning True if they are equal, or False if they are not.

If it is an object of reference type, it is used to determine whether two objects refer to the same object.

In C#language, the Equals method provides two methods, one static and one non-static, which are defined as follows.

. 
1 Equals (object ol, object o2); //Static method
2 Equals (object o); //Non-static method

For example:

1 class Program
2 {
3 static void Main(string[] args)
4 {
5 Student stu1 = new Student();
6 Student stu2 = new Student();
7 bool flag = Equals(stu1, stu2);
8 Console.WriteLine("stu1 and stu2 The comparison results are:{0}", flag);
9 }
10 }

2. GetHashCode method

The GetHashCode method returns the hash code of the current System.Object, with each object having a fixed hash value

Of. This method does not have any parameters and is not static, so it needs to be called with an instance.

Hash values are different for different instances, so this method can also be used to compare whether objects are equal.

For example:

1 class Program
2 {
3 static void Main(string[] args)
4 {
5 Student stu1 = new Student();
6 Student stu2 = new Student();
7 Console.WriteLine(stu1.GetHashCode());
8 Console.WriteLine(stu2.GetHashCode());
9 }
10 }

3. The GetType method GetType method is used to get the type of the current instance with a return value of System.Type type.

1 class Program
2 {
3 static void Main(string[] args)
4 {
5 int i = 100;
6 string str = "abc";
7 Student stu = new Student();
8 Console.WriteLine(i.GetType());
9 Console.WriteLine(str.GetType());
10 Console.WriteLine(stu.GetType());
11 }
12 }

4. ToString method

The ToString method returns the string of an object instance, which by default returns the qualified name of the class type.

For value types, the value is converted to a string type.

Any class can override the ToString method to return a custom string.

For example:

1 class Program
2 {
3 static void Main(string[] args)
4 {
5 Int32 a = 1;
6 Object b = new Object();
7 Console.WriteLine("Value type(Int32 type)The representation of the string of:{0}", a.ToString());
8 Console.WriteLine("Representation of reference type string:{0}", b.ToString());
9 }
10 }

Keyword involved in inheritance

1. Base keyword In an inherited relationship, a subclass can use the base keyword to invoke members in the parent class if they need to

Complete.

base refers to part of the parent class in the current object.

Using the base keyword in a method with the same name to call a method in the parent class is equivalent to duplicating the method content in the parent class

In this method.

The specific usage is as follows:

1 base. Parent Member

For example:

1 //Person.cs
2
3 class Person
4 {
5 public Person()
6 {
7 name = "Zhang San";
8 age = 18;
9 sex = "male";
10 }
11
12 public string name;
13 public int age;
14 public string sex;
15
16 public void PrintPersonInfo()
17 {
18 Console.WriteLine("Full name:{0} , Gender:{1}, Age:{2}", name, sex, age);
19 }
20 }
21
22 //Student.cs
23 class Student:Person
24 {
25 public Student()
26 {
27 classRoom = "403";
28 course = "computer technology";
29 }
30
31 //class
32 string classRoom;
33
34 //curriculum
35 string course;
36
37 public void PrintStudentInfo()
38 {
39 base.name = "Zhang Wuji";
40 base.PrintPersonInfo();
41 Console.WriteLine("class:{0},curriculum:{1}", classRoom, course);
42 }
43 }
44
45
46 //Program.cs
47 class Program
48 {
49 static void Main(string[] args)
50 {
51 Student stu = new Student();
52 stu.PrintStudentInfo();
53 }
54 }

2. virtual keywords

Virtual is the meaning of virtualization. In C#language, by default, members of a class are non-virtual, usually within a class

Members are defined as virtual, meaning that they will override their contents after inheritance.

The virtual keyword modifies methods, attributes, indexers, events, and so on, and is used in members of the parent class.

Subclass overrides require the use of the keyword override to modify the description.

Override concept: When a method with the same name and structure as the parent is redefined in a subclass, it is called a method override.

Note: The virtual keyword cannot decorate members decorated with static.

The virtual keyword is transitive, and override methods in subclasses are modified by virtual by default.

An example is as follows: The Person class overrides the virtual method ToString() in the Object class to print out personnel information.

The Student class overrides the virtual method ToString() in the Person class to print out student information.

1 //Person.cs
2
3 class Person
4 {
5 public Person()
6 {
7 name = "Zhang San";
8 age = 18;
9 sex = "male";
10 }
11
12 public string name;
13 public int age;
14 public string sex;
15
16 public void PrintPersonInfo()
17 {
18 Console.WriteLine("Full name:{0} , Gender:{1}, Age:{2}", name, sex, age);
19 }
20
21 public override string ToString()
22 {
23 return string.Format("Full name:{0} , Gender:{1}, Age:{2}", name, sex, age);
24 }
25
26 }
27
28 //Student.cs
29 class Student:Person
30 {
31 public Student()
32 {
33 classRoom = "403";
34 course = "computer technology";
35 }
36
37 //class
38 string classRoom;
39
40 //curriculum
41 string course;
42
43 public void PrintStudentInfo()
44 {
45 PrintPersonInfo();
46 Console.WriteLine("class:{0},curriculum:{1}", classRoom, course);
47 }
48
49 //Use of base
50 public override string ToString()
51 {
52 return string.Format("{0}\n class:{1},curriculum:{2}", base.ToString(), classRoo
m, course);
53 }
54 }
55
56
57 //Program.cs
58 class Program
59 {
60 static void Main(string[] args)
61 {
62 Student stu = new Student();
63 Console.WriteLine(stu.ToString());
64
65 //Console.WriteLine(stu);
66 }
67 }

3. abstract keywords

The abstract keyword represents abstraction, which can be used to modify classes and methods, a method called abstraction

Methods, modified classes are called abstract classes.

In C#language, an abstract method is a method without a method body and contains only the declaration of the method.

Definition format for abstract classes:

1 Access modifier abstract class Class name
2 {
3 //Class members
4 }

Definition format for abstract methods:

1 Access modifier abstract Method Return Value Type Method Name(parameter list)

Matters needing attention:

Abstract methods can be defined in abstract classes, or non-abstract methods can be defined.

An abstract class can define a construction method, but cannot be instantiated An abstract method has no method body.

The';'symbol after the abstract method definition must be preserved.

An abstract method must be defined in an abstract class.

The abstract method of the parent class must be redefined in the subclass.

1 //Modify Person class to abstract class and add abstract method DoWork, which is implemented separately by its subclasses.
2 abstract class Person
3 {
4 public Person()
5 {
6 name = "Zhang San";
7 age = 18;
8 sex = "male";
9 }
10
11 public string name;
12 public int age;
13 public string sex;
14
15 public void PrintPersonInfo()
16 {
17 Console.WriteLine("Full name:{0} , Gender:{1}, Age:{2}", name, sex, age);
18 }
19
20 public override string ToString()
21 {
22 return string.Format("Full name:{0} , Gender:{1}, Age:{2}", name, sex, age);
23 }
24
25 /// <summary>
26 ///Abstract methods, work content
27 /// </summary>
28 public abstract void DoWork();
29
30 }
1 //Implement the abstract method DoWork inherited from Person in the Student class
2 class Student:Person
3 {
4 public Student()
5 {
6 classRoom = "403";
7 course = "computer technology";
8 }
9
10 //class
11 string classRoom;
12
13 //curriculum
14 string course;
15
16 public void PrintStudentInfo()
17 {
18 base.name = "Zhang Wuji";
19 base.PrintPersonInfo();
20 Console.WriteLine("class:{0},curriculum:{1}", classRoom, course);
21 }
22
23 //Use of base
24 public override string ToString()
25 {
26 return string.Format("{0}\n class:{1},curriculum:{2}", base.ToString(), classRoo
m, course);
27 }
28
29 public override void DoWork()
30 {
31 Console.WriteLine("I'm learning");
32 }
33 }
1 //Program.cs
2 class Program
3 {
4 static void Main(string[] args)
5 {
6 Student stu = new Student();
7 Console.WriteLine(stu.ToString());
8 //Console.WriteLine(stu);
9
10 stu.DoWork();
11 }
12 }

4. sealed keyword

The sealed keyword is sealed and can be used to modify methods in a class or class, which is called a sealed class.

Sealing classes, modification methods are called sealing methods.

However, a sealed method must appear in a subclass, and it is the parent method overridden by the subclass, that is, the sealed keyword must match the

The override keyword is used together.

sealed method features: Can't be inherited Can't be overridden Scenario: Some classes or methods don't want to be inherited or overridden anymore

Write, which can be defined as a sealed class or a sealed method.

For example:

1 /// <summary>
2 ///abstract base class, define abstract method to find area
3 /// </summary>
4 abstract class AreaAbstract
5 {
6 public abstract void Area();
7 }
8
9 /// <summary>
10 ///Rectangle class, define length and width, override area method.
11 /// </summary>
12 class Rectangle : AreaAbstract
13 {
14 public double Width { get; set; }
15 public double Length { get; set; }
16
17 /// <summary>
18 ///Seal method, cannot be overridden by subclasses.
19 /// </summary>
20 public sealed override void Area()
21 {
22 Console.WriteLine("The area of the rectangle is:" + Width * Length);
23 }
24 }
25
26 /// <summary>
27 ///Circle class, define radius, override area method.
28 ///Sealed class, cannot be inherited
29 /// </summary>
30 sealed class Circle : AreaAbstract
31 {
32 public double r { get; set; }
33 public override void Area()
34 {
35 Console.WriteLine("The area of a circle is:" + r * 3.14 * 3.14);
36 }
37 }

Topics: C#