C # three features of object-oriented:
Encapsulation, inheritance, and Polymorphism
Encapsulation:
Ensure the integrity and security of the object's own data
1. Integrity: encapsulation is to package some scattered code so that they can complete a function together.
For example, classes, attributes, and methods are encapsulated in one form.
2. Security: for example, when creating an object or calling a method, we only need to instantiate a class object.
Or call a method name to complete the functions we need, but we cannot see how it is implemented internally.
Inheritance:
C # inherited Syntax:
Public class subclass: parent class
{
// Use the symbol for inheritance
}
Concept:
Establish relationships between classes (parent classes and child classes) to reuse code and facilitate system expansion
1. in inheritance, the Child class is divided into the parent class (base class) and subclass (derived class). That is to say, when the child class inherits the parent class,
Then the subclass has non-private members (attributes, methods, etc.) in the parent class)
Example:
Students include names, ages...
Employees include names, ages...
Then, if we want to add male and female humans at this time,
Is it difficult to write these attributes, such as name and age?
However, both students and employees have two attributes: name and age.
They both have these two attributes because they both belong to humans.
In this case, we can extract the attributes they share and encapsulate them together.
In a Person class, the student class and employee class are inherited from the Person class.
In this way, both students and employees have the attributes of name and age,
Their common attributes and methods are managed by the Person class, and they only need to manage
You can use your own properties and methods.
The advantage of this is that the Code is reused to facilitate system expansion,
For example:
In the future, we will add another male or female human,
You don't have to write names, ages, and other Members that they all share. You just need to inherit the Person class,
Similarly, to add a common member, you only need to add it to the parent class Person.
Instead of adding new members to each of their classes.
Example:
Public class Person // human (this is the parent class)
{
Public string Name {get; set;} // Name
Public int Age {get; set;} // Age
}
Public class Student: Person // Student class inherited by humans
{
// After Student inherits Person, Student automatically owns non-private Members of the Person class.
}
Public class Employee: Person // The Employee class inherits from humans.
{
// After the Employee inherits the Person, the Employee automatically owns the non-private member of the Person class.
}
When we create a Student object and an Employee object:
Student stu = new Student ();
Employee emp = new Employee ();
You can use:
Stu. Name = "James ";
Stu. Age = 20;
Emp. Name = "Li Si ";
Emp. Age = 30;
This is because they all inherit the Person class.
2. the inheritance must conform to the "subclass is a parent class" relationship, that is, the subclass is a parent class.
Example: As mentioned above, to implement inheritance, it is necessary to conform to the relation of the Child class is a parent class,
For example, if a student is a student and an employee is a student.
3. Single inheritance. in C #, a subclass can only inherit one parent class, just as a son cannot have multiple fathers)
4. Subclass can replace the parent class and appear anywhere the parent class can appear, and the program behavior will not change,
In turn, parent class objects cannot replace subclass objects. This feature is called "Liskor Subsitution Principle )"
Example:
The above example can also be written as follows:
Person p1 = new Student ();
Person p2 = new Employee (); // The principle of replacement by Lishi
In this case, an error is reported:
Student stu = new Person ();
Employee emp = new Person (); // Error
The is and as operators can be used in the Rys replacement principle.
Is: used to determine whether a type is a given type and return the bool value.
For example, p1 is Student // determine whether p1 is of the Student type. true: false: No
As: used to convert a type to another type, similar to forced conversion, but in forced conversion,
If the conversion fails due to incompatibility of the given conversion type), an error is returned, but the as conversion fails,
No error is reported, and null is returned.
For example, p1 as Student // converts p1 to the Student type. If a failure occurs, null is returned.
Polymorphism:
Concept: Polymorphism refers to two or more objects belonging to different classes, for the same message method call)
Different Response methods.
Implementation of polymorphism:
1. Reload
2. Virtual Methods
3. Abstract METHODS (and abstract attributes) → abstract Members do not have subjects
4. Generics such as: <T>
Syntax for defining virtual methods:
Access modifier virtual return type method name ()
{
// Method body
// Note: the virtual method cannot be private.
}
Define the abstract method Syntax:
Access modifier abstract return type method name (); // No method body, cannot be privatte
Define the abstract attribute Syntax:
Public abstract data type attribute name
{
Get; set; // fields are not allowed.
}
Rewrite the syntax of the virtual method and abstract method in the subclass:
Access modifier override return type method name ()
{
// Method body
// Note: the method used to override the parent class must be: the return type, method name, and parameters are the same.
}
Example of overwriting an abstract attribute in a subclass:
Public override int Age
{
Get {return age ;}
Set {age = value ;}
}
Differences between virtual and abstract methods:
Virtual method:
1. Modify with virtual
2. There must be a method body
3. quilt override (rewrite)
4. You can write (virtual method) in addition to the sealing class and static class)
Abstract method:
1. Modify with abstract
2. No method body
3. override must be a non-Abstract subclass)
4. It must exist in the abstract class
Abstract class:
1. classes with abstract methods (attributes) must be abstract classes.
2. abstract classes can not only contain abstract methods and abstract attributes, but also include other attributes, methods, and constructors.
3. abstract classes cannot be instantiated.
4. abstract classes cannot be static or sealed, because both static and sealed classes cannot be inherited.
Example:
Public abstract class Person // abstract class Person is the parent class
{
// Defines an abstract Method
Public abstract string Test ();
}
Public class Employee: Person // inherits the Person class
{
// Because the Employee class is not an abstract class, you must override the Test () method of the override parent class Person.
Public override string Test ()
{
// Method body
Return ""; // override must have the same method name, parameter, and return type.
}
}
Remember: abstract classes cannot be instantiated.
Person p = new Person (); // an error is returned when writing this code.
However, you can write as follows:
Person p = new Employee (); // This write is correct
Call the p. Test () method to execute the Test () method in the Employee,
That is to say, once the quilt class overwrites override, the parent class is created.
The parent class object will call the method of its actual type object is the method after being overwritten)
PS: Abstract members must be overwritten by non-Abstract subclass override
Of course, if it is a virtual method, it is not overwritten in the subclass, then the method of the parent class will be called.
This article is from the "My_Dream" blog, please be sure to keep this http://vitality.blog.51cto.com/7335206/1227854