Simple inheritance
The simplest of three classes
public class Animal {public Animal () { Debug.Log ("Construct animal!");} }
public class Mammal:animal {public mammal () { Debug.Log ("Construct mamal!");} }
public class sheep:mammal{public Sheep () { Debug.Log ("Construct sheep!");} }
Inside the main
Sheep Sheep = new Sheep ();
The constructor of the subclass executes the constructor of the base class in turn, without objection.
It is important to note that, without a special declaration, subclasses automatically look for constructors with no arguments in the parent class, and if not in the base class, they need to be called in the constructor of the subclass, such as:
public class animal{ int age; Public Animal (Int. _age) {age = _age; Debug.Log ("Construct animal!");} }
The constructor for the subclass is written like this
Public mammal (): base (0) { Debug.Log ("Construct mamal!"); }
Adding a function to the base class animal
public void Eat () { Debug.Log ("Animal eat!"); }
Let's talk about the overlay of the method
Overriding the definition eat method in subclass sheep
Public new void Eat () { Debug.Log ("Sheep eat!"); }
The New keyword here represents the method in the hidden base class, which is officially explained as follows:
The new keyword can explicitly hide members that inherit from a base class. When you hide an inherited member, the derived version of the member replaces the base class version. Although you can hide members without using the new modifier, you will receive a compiler warning. If you use new to explicitly hide members, this warning is suppressed.
Test code:
Sheep Sheep = new Sheep (); Animal Animal = new Sheep (); Sheep. Eat (); Animal. Eat ();
The result is this.
There is no polymorphic behavior, the declared base class object, which executes the base class method, declares the subclass object, and executes the subclass method.
In a function of a subclass, there is a base variable, which can be considered a reference to a base class, and a method of base class can be called through base.
Polymorphic
Polymorphic is polymorphic is the parent class refers to the child class object, calling the method will call the implementation of the subclass, not the implementation of the parent class, which is called polymorphism.
Defining functions in the base class animal
public virtual void Birth () { Debug.Log ("Animal birth!");
Sheep overrride a little bit.
public override void Birth () { Debug.Log ("Sheep birth!"); }
Test code
Sheep Sheep = new Sheep (); Animal Animal = new Sheep (); Animal. Birth ();
Execution results
There are two points to note
1. Subclasses calling methods of the parent class
Of course, it's directly using base.
2. Virtual methods in the inheritance chain
Virtual members are always virtual, regardless of how many classes have been declared between the virtual member and the class that originally declared the virtual member. If Class A declares a virtual member, Class B derives from a, Class C derives from Class B, then Class C inherits the virtual member, and optionally overrides it, regardless of whether Class B declares an override for that member.
Usage of sealed keyword
The sealed keyword is used to prevent derived classes from overriding virtual members. or an example.
Defining methods in base class animal
public virtual void Move () { Debug.Log ("Animal move!");}
Sealed override this function in mammal
public sealed override void Move () { Debug.Log ("Mammal move!"); }
This function cannot be overide in sheep, because in sheep's view, the base class move is treated as a normal function, and if you want to define your own move function, add new, like this
Public new void Move () { Debug.Log ("Sheep move!"); }
Write point code to test polymorphism
Animal Animal = new Animal (1); Animal animal1 = new mammal (1); Animal animal2 = new Sheep (); Sheep Sheep = new Sheep (); Animal. Move (); Animal1. Move (); Animal2. Move (); Sheep. Move ();
Run results
The first one has nothing to say, directly executes the animal move function
Second, normal polymorphism.
The third one means, polymorphic to mammal.
The fourth is the normal execution of the member function of the Sheep class.
Use of the abstract keyword
Official explanation: The abstract modifier indicates that the modified content is missing or not fully implemented. The abstract modifier is available for classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can be a base class only for other classes. A member that is marked as abstract or contained in an abstract class must be implemented by a class derived from an abstract class.
Ground gas Interpretation: The abstract class is used to construct the framework of the subclass, and the subclass must implement the abstract method, and the override keyword is used.
The difference between the abstract method and the Vitual method
Abstract method has no function body, the subclass must have the corresponding method implementation.
Vitual method subclasses can either choose the implementation or choose not to implement it.
All two can be used to implement polymorphism.
Abstract classes cannot be instantiated. The purpose of an abstract class is to provide a generic base class definition that can be shared by multiple derived classes. For example, a class library can define an abstract class to use as a parameter to multiple class library functions, and require programmers who use the library to provide their own class implementations by creating derived classes.
Interface
An interface contains a definition of a set of related functions that a class or struct can implement.
For example, using an interface can include behaviors from multiple sources in a class. This feature is important because the C # language does not support multiple inheritance. In addition, if you want to emulate inheritance of structs, you must also use interfaces, because they cannot actually inherit from another struct or class.
Defining a simple interface, only the definition of an interface function, is not implemented.
Public interface Isay { void SayHello (); void Sayfuck ();}
Sheep Inherit Isay interface
public class Sheep:mammal, Isay
The sheep class must have the definition of the interface defined in the Isay
public void SayHello () { Debug.Log ("Isay Hello"); } public void Sayfuck () { Debug.Log ("Isay sayfuck"); }
Test code
Sheep Sheep = new Sheep (); Sheep. SayHello (); Sheep. Sayfuck ();
Execution results
The interface has the following properties
An interface is similar to an abstract base class. Any class or struct that implements an interface must implement all its members.
The interface cannot be instantiated directly. Its members are implemented by any class or struct that implements the interface.
An interface can contain events, indexers, methods, and properties.
An interface does not contain an implementation of a method.
A class or struct can implement multiple interfaces. A class can inherit one base class, and one or more interfaces can be implemented.
Reference
Msdn
Inheritance and polymorphism in C # and interfaces