C# 类的多态:虚方法,抽象类,接口

多态的作用

消除子类之间的差异性。

实现多态的方法

  1. 虚方法:
    将父类的方法定义为 virtual,子类的方法定义为 override,父类方法可以被子类重写。
//父类
class Person{
	//虚方法,可以被子类的同名方法覆盖
	public virtual void M(){ Console.WriteLine("我是M"); }
}
//子类
class Student:Person{
	//子类方法想覆盖父类方法,需要加上override
	public override void M1(){ Console.WriteLine("我是M1"); }
}
//子类
class Student:Person{
	//子类方法想覆盖父类方法,需要加上override
	public override void M2(){ Console.WriteLine("我是M2"); }
}
	//多态的应用
	class Program
    {
        static void Main(string[] args)
        {
            M[] ms = { new M(), new M1(), new M2() };
            foreach (M m in ms)
            {
                n.Call();
            }
        }
    }
  1. 抽象类:
    1. 关键词 abstract
    2. 父类创建一个抽象方法,抽象方法为空不执行代码,只用于被子类重写;
    3. 抽象类不允许创建对象;
    4. 子类必须重写父类中所有的抽象成员(抽象方法);
    5. 子类重写方法的返回值和参数,类型必须和抽象成员一样
	abstract class Shape   //图形抽象类
    {
        public abstract double GetArea();   //抽象方法,计算面积
        public abstract double GetPrimiter();   //抽象方法计算周长
    }
    
    class Circle : Shape   //圆形类,继承图形类
    {
        public Circle(int r)
        {
            this.R = r;
        }
        
        private int _r;
        public int R { get => _r; set => _r = value; }

        public override double GetArea()
        {
            return Math.PI * this.R * this.R;
        }

        public override double GetPrimiter()
        {
            return Math.PI * this.R * 2;
        }
    }
    
    class Square : Shape   //方形类,继承图形类
    {
        public Square(int l,int w)
        {
            this.L = l;
            this.W = w;
        }
        
        private int _l;
        private int _w;
        public int L { get => _l; set => _l = value; }
        public int W { get => _w; set => _w = value; }

        public override double GetArea()
        {
            return this.L * this.W;
        }

        public override double GetPrimiter()
        {
            return (this.L + this.W) * 2;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
        	//同一个方法,赋不同的对象,调用不同的方法
            Shape s = new Square(5,6); //new Circle(5);
            Console.WriteLine("{0:0.00}", s.GetArea());
            Console.WriteLine("{0:0.00}", s.GetPrimiter());
        }
    }
  1. 接口:
    1. 关键词: interface
    2. 接口成员:方法、自动属性、索引器、事件(都是方法)。
    3. 接口成员要求:1.不能有访问修饰符;2.接口成员不能写方法体;
    4. 继承接口的子类必须实现接口的成员。
    5. 接口不能被实例化。
    6. 接口与接口之间可以继承和多继承。
    7. 接口不能继承类,类可以继承接口。
	//接口,游泳能力
	public interface ISwimming
    {
        void Swimming();
    }
    //真鸭子,继承游泳能力
	 class Duck : ISwimming
    {
        public void Swimming()
        {
            Console.WriteLine("我是真鸭子,我靠脚游泳。");
        }
    }
    //橡皮鸭子,继承游泳能力
    class RubberDuck : ISwimming
    {
        public void Swimming()
        {
            Console.WriteLine("我是橡皮鸭子,我靠浮力游泳。");
        }
    }
    //木鸭子不会游泳
    class WoodDuck
    {

    }
    
    class Program
    {
        static void Main(string[] args)
        {
        	//调用游泳接口
            ISwimming swim = new RubberDuck(); //new Duck();
            swim.Swimming();
        }
    }

你可能感兴趣的:(C#)