抽象类
学习:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //狗狗会叫 猫咪会叫 6 Animal a = new Cat();//new Dog(); 7 a.Bark(); 8 9 Console.ReadKey(); 10 } 11 }
1 public abstract class Animal 2 { 3 public virtual void T() 4 { 5 Console.WriteLine("动物有声明"); 6 } 7 8 private int _age; 9 10 public int Age 11 { 12 get { return _age; } 13 set { _age = value; } 14 } 15 16 public Animal(int age) 17 { 18 this.Age = age; 19 } 20 21 public abstract void Bark(); 22 23 public abstract string Name 24 { 25 get; 26 set; 27 } 28 29 //public abstract string TestString(string name); 30 31 public Animal() 32 { 33 34 } 35 36 public void test() 37 { 38 //空实现 39 } 40 }
1 public abstract class Test : Animal 2 { 3 4 }
1 public class Dog : Animal 2 { 3 //public abstract void Test(); 4 5 public override void Bark() 6 { 7 Console.WriteLine("狗狗旺旺的叫"); 8 } 9 10 public override string Name 11 { 12 get 13 { 14 throw new NotImplementedException(); 15 } 16 set 17 { 18 throw new NotImplementedException(); 19 } 20 } 21 22 //public override string TestString(string name) 23 //{ 24 // //throw new NotImplementedException(); 25 //} 26 }
1 public class Cat : Animal 2 { 3 public override void Bark() 4 { 5 Console.WriteLine("猫咪喵喵的叫"); 6 } 7 8 public override string Name 9 { 10 get 11 { 12 throw new NotImplementedException(); 13 } 14 set 15 { 16 throw new NotImplementedException(); 17 } 18 } 19 }

浙公网安备 33010602011771号