2.1 Implementing Inheritance and Interface inheritance
Implementation inheritance: A type is derived from a base type, which is used for all member fields and functions of the base type.
Class BaseClass {public string UserName = "C #"; public static string GetUserName () { return ""; } } Class Impleteclass:baseclass {public string printname () { return getusername (); Return UserName }
Impleteclass class inherits BaseClass, it can use all the member fields in the base class such as username,getusername ();
Interface inheritance indicates that a type inherits only the signature of a function and does not inherit any implementation code. When you need to develop this type with some features available, it's best
Use this type of inheritance.
2.2 Multiple Inheritance
C # does not support multiple implementation inheritance. But C # allows a type to derive from multiple interfaces---multi-interface inheritance. That means
C # classes can derive from another class and any number of interfaces.
2.3 Virtual keyword
The virtual keyword is used to decorate a method in a base class. There are several things you can do with virtual:
1, if the method is not virtual, the compiler will use the declared class corresponding to the type, that is, not virtual, at compile time, it is fixed. For example: The methods of subclasses are not executed, all of them are methods of the parent class. The result of the run is
Father
Boy
Girl
public class Classfather {public string S1; public void Virfun () { Console.WriteLine (S1); } } public class Classboy:classfather {public new void Virfun () { base. Virfun (); } } public class Classgirl:classfather {public new void Virfun () { base. Virfun (); Console.WriteLine (S1); } } public class Test {public static void Main () { Classfather a = new Classfather (); A.S1 = "Father"; A.virfun (); Classfather B = new Classboy (); B.S1 = "Boy"; B.virfun (); Classfather C = new Classgirl (); C.S1 = "Girl"; C.virfun (); Console.readkey (); }
2, if the method is virtual and then the subclass uses override, the compiler produces the code. Then, at run time, check to see which class the object belongs to, and then call the method of the class. The result of the run is
Father
Boy
Girl
Girl
public class Classfather {public string S1; public virtual void Virfun () { Console.WriteLine (S1); } } public class Classboy:classfather {public override void Virfun () { base. Virfun (); } } public class Classgirl:classfather {public override void Virfun () { base. Virfun (); Console.WriteLine (S1); } } public class Test {public static void Main () { Classfather a = new Classfather (); A.S1 = "Father"; A.virfun (); Classfather B = new Classboy (); B.S1 = "Boy"; B.virfun (); Classfather C = new Classgirl (); C.S1 = "Girl"; C.virfun (); Console.readkey (); }
3, if the method of a parent class is virtual, the subclass is not overridden by override, but is overwritten with new, then it is the method of executing the declared class when the subclass is run. For example, the Girl class object is.
Father
Boy
Girl
public class Classfather {public string S1; public virtual void Virfun () { Console.WriteLine (S1); } } public class Classboy:classfather {public new void Virfun () { base. Virfun (); } } public class Classgirl:classfather {public new void Virfun () { base. Virfun (); Console.WriteLine (S1); } } public class Test {public static void Main () { Classfather a = new Classfather (); A.S1 = "Father"; A.virfun (); Classfather B = new Classboy (); B.S1 = "Boy"; B.virfun (); Classfather C = new Classgirl (); C.S1 = "Girl"; C.virfun (); Console.readkey (); }
Drifting over to learn C # series (2)------inheritance