1.虚方法
将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。

点击(此处)折叠或打开

  1. namespace _10多态练习
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             RealDuck rd = new RealDuck();
  8.             MuDuck md = new MuDuck();
  9.             XPDuck xd = new XPDuck();
  10.             RealDuck[] ducks = { rd, md, xd };
  11.             for (int i = 0; i < ducks.Length; i++)
  12.             {
  13.                 ducks[i].Bark();
  14.             }
  15.             Console.ReadKey();

  16.         }
  17.     }
  18.     public class RealDuck
  19.     {
  20.         public virtual void Bark()
  21.         {
  22.             Console.WriteLine("真的鸭子嘎嘎叫");
  23.         }
  24.     }

  25.     public class MuDuck : RealDuck
  26.     {
  27.         public override void Bark()
  28.         {
  29.             Console.WriteLine("木头鸭子吱吱叫");
  30.         }
  31.     }

  32.     public class XPDuck : RealDuck
  33.     {
  34.         public override void Bark()
  35.         {
  36.             Console.WriteLine("橡皮鸭子唧唧叫");
  37.         }
  38.     }
  39. }

2.抽象类
当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。

点击(此处)折叠或打开

  1. namespace _12_抽象类
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             //使用多态求矩形的面积和周长以及圆形的面积和周长
  8.             Shape shape = new Square(5, 6); //new Circle(5);
  9.             double area = shape.GetArea();
  10.             double perimeter = shape.GetPerimeter();
  11.             Console.WriteLine("这个形状的面积是{0},周长是{1}", area, perimeter);
  12.             Console.ReadKey();

  13.         }
  14.     }

  15.     public abstract class Shape
  16.     {
  17.         public abstract double GetArea();
  18.         public abstract double GetPerimeter();
  19.     }
  20.     public class Circle : Shape
  21.     {

  22.         private double _r;
  23.         public double R
  24.         {
  25.             get { return _r; }
  26.             set { _r = value; }
  27.         }

  28.         public Circle(double r)
  29.         {
  30.             this.R = r;
  31.         }
  32.         public override double GetArea()
  33.         {
  34.             return Math.PI * this.R * this.R;
  35.         }

  36.         public override double GetPerimeter()
  37.         {
  38.             return 2 * Math.PI * this.R;
  39.         }
  40.     }
  41.     public class Square : Shape
  42.     {
  43.         private double _height;

  44.         public double Height
  45.         {
  46.             get { return _height; }
  47.             set { _height = value; }
  48.         }

  49.         private double _width;

  50.         public double Width
  51.         {
  52.             get { return _width; }
  53.             set { _width = value; }
  54.         }

  55.         public Square(double height, double width)
  56.         {
  57.             this.Height = height;
  58.             this.Width = width;
  59.         }

  60.         public override double GetArea()
  61.         {
  62.             return this.Height * this.Width;
  63.         }

  64.         public override double GetPerimeter()
  65.         {
  66.             return (this.Height + this.Width) * 2;
  67.         }
  68.     }

  69. }


10-03 08:21