C # Study Notes-inherit basic knowledge,
1 namespace Inheritance 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Dog dog = new Dog(); 8 dog.Age = 10; 9 dog.Bite();10 dog.GetAge();11 dog.BiteMan();12 Dog oldDog = new Dog(10);13 14 15 Animal dog2 = new Dog();16 dog2.Bite();17 dog2.BiteMan();18 ((Dog)(dog2)).BiteMan(); //casts19 Console.ReadLine();20 }21 }22 23 public class Animal24 {25 public int Age26 {27 get;28 set;29 }30 public Animal() //constructor31 {32 Console.WriteLine("Animal constructure");33 }34 35 public Animal(int age)36 {37 Console.WriteLine("Old Animal");38 this.Age = age;39 }40 41 public virtual void Bite() //virtual can be override when it be inherited42 {43 Console.WriteLine( "Animal bite");44 }45 public virtual void GetAge()46 {47 Console.WriteLine(Age);48 }49 50 public void BiteMan() //can't be overrided without virtual51 {52 Console.WriteLine("Animal bite man");53 }54 }55 public sealed class Dog : Animal //sealed:can't be inherited56 {57 public Dog() //constructor58 {59 Console.WriteLine("Dog constructure");60 }61 public Dog(int age):base(age) //call specific function 62 {63 Console.WriteLine("Old Dog");64 this.Age = age;65 }66 public override void Bite() //override the parent class method 67 {68 Console.WriteLine("Dog bite");69 }70 71 public new void BiteMan() //new:new method which is as same as the parent class method,not override72 {73 Console.WriteLine("Dog bite man");74 }75 }76 }