10.1 Relationship of objects and base classes of derived classes
(1) is A relationship (belongs to the relationship):
Objects belong to a class, so objects and classes belong to a relationship; subclasses belong to the parent class, so the subclass and parent class belong to the relationship;
Because: Object belongs to class, so subclass object belongs to subclass;
Also because the child class belongs to the parent class, so the subclass object belongs to the parent class
10.2 The concept of polymorphism
(1) The reference character and the object
Object: Object is the object entity that is stored in the heap;
Reference: The reference character holds the address of the object in the heap and is stored in the stack;
There is a previous theory: the subclass object belongs to the parent class, so the parent class reference can point to the subclass object;
And that is to say:
(a) Subclass instances (references, objects) can be assigned to parent class references (Automation transformation)
(b) However, this parent reference cannot invoke a member that is unique to a subclass, and can only invoke a member that inherits from the parent class;
10.3 Application of Polymorphism
1. polymorphic Use
(1) The parent class defines an abstract function, and multiple subclasses rewrite the abstract function respectively;
(2) Design a common section (use the parent class type to define a formal parameter
(3) Produce multiple subclasses, pass the object to the common part, call the same method, get different results
Abstract classVertebrata//(1) The parent class defines an abstract function, and multiple subclasses rewrite the abstract function respectively; { Abstract Public voidShow (); } classMammal:vertebrata { Public Override voidShow () {}}classDog:mammal//(2) Multiple subclasses rewrite this abstract function respectively; { Public Override voidShow () {Console.WriteLine ("Dog"); } } classPeg:mammal { Public Override voidShow () {Console.WriteLine ("Peg"); } } classBrid:mammal { Public Override voidShow () {Console.WriteLine ("Brid"); } } classProgram {Static voidMain (string[] args) {vertebrata[] Animals= {NewDog (),NewPeg (),NewBrid ()}; foreach(Vertebrata VinchAnimals//Design a common section (use the parent class type to define a formal parameter.) produces multiple subclasses and passes the subclass object to the generic part;{v.show (); } console.readkey (); } }
2. In addition to using the Bastract method and override can be polymorphic, can also be implemented with virtual and overrride
But note:
(1) because the virtual function (not the abstract function) must have a method body, so design into empty implementation or random implementation;
(2) must be a overtide rewrite virtual function (can not add overtide rewrite virtual function, but the result is not the same) in the method, otherwise the parent class reference appears to hide the same name in the subclass of the method, the subclass does not agree to the same method of different implementations;
Because you must use override to override a method with the same name in the parent class to implement polymorphism, the method with the same name in the parent class must be decorated with virtual or basetract; (not decorated as virtual or basetract cannot have override overridden or syntax error) ;
classVertebrata { Public Virtual voidShow () {Console.WriteLine ("Vertebrata"); } } classMammal:vertebrata { Public Override voidShow () {}}classDog:mammal { Public Override voidShow () {Console.WriteLine ("Dog"); } } classPeg:mammal { Public Override voidShow () {Console.WriteLine ("Peg"); } } classBrid:mammal { Public Override voidShow () {Console.WriteLine ("Brid"); } } classProgram {Static voidMain (string[] args) {vertebrata[] Animals= {NewDog (),NewPeg (),NewBrid ()}; foreach(Vertebrata Vinchanimals) {v.show (); } //(dog) animals[0]). Show (); //(PEG) animals[1]). Show ();Console.readkey (); } }
3. Assign a subclass instance to a base class reference (automatic upward transformation), the parent class reference cannot use the subclass-specific member, (the latter says the subclass-specific member is not visible to the parent class);
(If you do not override overrides, the parent class reference will hide the child class with the same name method overrides, which is also the subclass overridden by the parent class method, the parent class is not visible (the overridden method is also considered subclass-specific, because the subclass-specific members are not visible to the parent class)
classVertebrata { Public voidShow () {Console.WriteLine ("Vertebrata"); } } classMammal:vertebrata { Public voidShow () {}}classDog:mammal { Public voidShow () {Console.WriteLine ("Dog"); } } classPeg:mammal { Public voidShow () {Console.WriteLine ("Peg"); } } classBrid:mammal { Public voidShow () {Console.WriteLine ("Brid"); } } classProgram {Static voidMain (string[] args) {vertebrata[] Animals= {NewDog (),NewPeg (),NewBrid ()}; foreach(Vertebrata Vinchanimals) {v.show (); //If you do not override overrides, the parent class reference hides a method that has the same name as the child class, and that is, the parent class override method,//the parent class is not visible (this overriding method is also considered subclass-specific, because subclass-specific members are not visible to the parent class) } //(dog) animals[0]). Show (); //(PEG) animals[1]). Show ();Console.readkey (); } }
10.4 is operator
The 1.is operator uses and determines whether each instance belongs to a class, returns True if the instance belongs to the class, or the parent class of the class, otherwise returns false;
2. How to use: Object is class name
classProgram {Static voidMain (string[] args) { inti =New int(); I=5; Console.WriteLine (i is int); Console.WriteLine (i is Object); Console.WriteLine (i isDouble); Console.WriteLine (New int() isDouble); Console.readkey (); } }
10.5 Object Transformation
(1) from low to high level is called upward transformation, upward transformation is automatic,
The conversion of a derived class to a base class is an upward transformation and is automatic, but a base class cannot reference a member that is unique to a subclass object;
classA {}classb:a {voidShow () {Console.WriteLine ("Show"); } } classProgram {Static voidMain (string[] args) {A AA=NewB (); Aa.show ();//error, parent class reference, cannot use subclass-specific Members } }
(2) from high-level to low-grade is called upward transformation, must be stronger conversion;
Note: Only the reference itself is a subtype that can be turned into a subclass, and cannot be converted if the reference itself is originally a parent type or other type
classA {}classb:a { Public voidShow () {Console.WriteLine ("Show"); } } classProgram {Static voidMain (string[] args) {A AA=NewB (); ((B) AA). Show (); //forced downward conversions: Because AA is an instance of a subclass, it can be turned into subclasses;A AAA=NewA ();//error cannot be converted because AA is originally a parent type/or other type((B) AAA). Show (); } }
10.6 interface
Cond....
10.7-Class Relationship view
Note of the C # Portal-Tenth chapter