Basic Object-Oriented Knowledge-encapsulation, inheritance, polymorphism, and basic knowledge Polymorphism
Object-oriented principles:
Multiple combinations and less inheritance; low coupling and High Cohesion
Inheritance focuses more on commonality; polymorphism focuses on Difference
Polymorphism
Through inheritance, a class can be used as multiple types: it can be used as its own type, any base type, or any interface type when implementing an interface. This is known as polymorphism.Every type in C # Is polymorphism.. Types can be used as their own types or as Object instances, becauseObjectAs the base type.
1 public class animal 2 {3 4 public string SayName () 5 {6 return "hello im animal"; 7 8} 9 public string Eat () 10 {11 12 return "this is animal's eat"; 13} 14 public virtual string Call () 15 {16 17 return "this is animal's speak "; 18} 19} 20 public class Bird: animal21 {22 public new string Eat () // Replace the base MEMBER 23 {24 return "this is bird's Eat"; 25} 26 public override string Call () with the new derived Member () // rewrite the virtual Method 27 {28 return "this is Bird's Call"; 29} 30 31}
1/* inherit the eat and call functions of the base class */2/* 3 * result: 4 hello im animal 5 this is bird's Eat 6 this is Bird's Call 7 */8 Bird bb = new Bird (); 9 Response. write (bb. sayName () + "</br>"); 10 Response. write (bb. eat () + "</br>"); 11 Response. write (bb. call () + "</br>"); 12 13/* 14 * result: 15 hello im animal16 this is animal's eat17 this is Bird's Call18 */19 animal AB = new Bird (); 20 Response. write (AB. sayName () + "</br>"); 21 Response. write (AB. eat () + "</br>"); 22 Response. write (AB. call () + "</br> ");
New modifier: when used as a modifier, The new Keyword can explicitly hide members inherited from the base class.
When a derived class overrides a virtual member, the Member is called even if the instance of the derived class is accessed as a base class instance.
Encapsulation
Encapsulation Principle 1: Define a field as private