多态之虚方法

概念:让一个对象可以表现出多种状态(类型)

多态:增加了程序的可扩展性,减少了很多代码量。

实现多态的3种手段:1、虚方法 2、抽象类 3、接口

 

       static voidMain(string[] args)

        {

          

            Chinese cn1=new Chinese("韩梅梅");

            Chinese cn2=new Chinese("李雷");

            Japanese j1=new Japanese("树下君");

            Japanese j2=new Japanese("井边子");

            Korea k1= new Korea("金秀贤");

            Korea k2= new Korea("金贤秀");

            American a1=new American("科比布莱恩特");

            American a2=new American("奥尼尔");

            Person[]pers = { cn1, cn2, j1, j2, k1, k2, a1, a2,new English("格林"),new English("玛利亚") };

 

            for (int i =0; i < pers.Length; i++)

            {

                //if (pers[i] is Chinese)

                //{

                //   ((Chinese)pers[i]).SayHello();

                //}

                //else if (pers[i] is Japanese)

                //{

                //   ((Japanese)pers[i]).SayHello();

                //}

                //else if (pers[i] is Korea)

                //{

                //   ((Korea)pers[i]).SayHello();

                //}

                //else

                //{

                //   ((American)pers[i]).SayHello();

                //}

 

 

                pers[i].SayHello();

            }

            Console.ReadKey();

        }

}

public classPerson

    {

        private string_name;

        public stringName

        {

            get { return_name; }

            set {_name = value; }

        }

 

        publicPerson(string name)

        {

            this.Name = name;

        }

        public virtual voidSayHello()

        {

            Console.WriteLine("我是人类");

        }

 

    }

 

    public class Chinese : Person

    {

        publicChinese(string name)

            : base(name)

        {

 

        }

 

        public override voidSayHello()

        {

            Console.WriteLine("我是中国人,我叫{0}",this.Name);

        }

    }

    public class Japanese : Person

    {

        publicJapanese(string name)

            : base(name)

        { }

 

        public override voidSayHello()

        {

            Console.WriteLine("我是脚盆国人,我叫{0}",this.Name);

        }

    }

    public class Korea : Person

    {

        publicKorea(string name)

            : base(name)

        {

 

        }

 

 

        public override voidSayHello()

        {

            Console.WriteLine("我是棒之思密达,我叫{0}",this.Name);

        }

    }

    public class American : Person

    {

        publicAmerican(string name)

            : base(name)

        {

 

        }

 

        public override void SayHello()

        {

            Console.WriteLine("我叫{0},我是米国人",this.Name);

        }

    }

 

 

    public class English : Person

    {

        publicEnglish(string name)

            : base(name)

        { }

 

        public override voidSayHello()

        {

            Console.WriteLine("我是英国人");

        }

    }

 

 

狗狗会叫 猫也会叫

当父类中的方法不知道如何去实现时,可以考虑将父类写成抽象类,将方法写成抽象方法。否则就用虚方法来实现该多态问题。

抽象类和接口是不允许创建对象的。

抽象方法时不允许有方法体的。

 

class Program

    {

        static voidMain(string[] args)

        {

            //狗狗会叫 猫咪会叫

            //创建子类对象赋值给抽象类Animal;

            Animal a =newCat();//newDog();

            a.Bark();

           

            Console.ReadKey();

        }

    }

 

    public abstract class Animal

    {

 

        public virtual void T()

        {

            Console.WriteLine("动物有声明");

        }

 

        private int_age;

 

        public int Age

        {

            get { return_age; }

            set {_age = value; }

        }

 

        publicAnimal(int age)

        {

            this.Age= age;

        }

        public abstract voidBark();

       //抽象属性

        public abstract stringName

        {

            get;

            set;

        }

 

     //   public abstract string TestString(stringname);

 

 

        publicAnimal()

        {

           

        }

        //public void Test()

        //{

        //    //空实现

        //}

    }

 

 

    public abstract class Test : Animal

    {

       

    }

 

    public class Dog : Animal

    {

       // public abstract void Test();

 

 

        public override voidBark()

        {

            Console.WriteLine("狗狗旺旺的叫");

        }

 

        public override stringName

        {

            get

            {

                throw new NotImplementedException();

            }

            set

            {

                throw new NotImplementedException();

            }

        }

 

        //public override string TestString(string name)

        //{

        //    //throw newNotImplementedException();

        //}

    }

 

    public class Cat : Animal

    {

        public override voidBark()

        {

            Console.WriteLine("猫咪喵喵的叫");

        }

 

        public override stringName

        {

            get

            {

                throw new NotImplementedException();

            }

            set

            {

                throw new NotImplementedException();

            }

        }

}

 

抽象类特点:

1.抽象成员必须标记为abstract,并且不能有任何实现。

2.抽象成员必须在抽象类中。

3.抽象类不能被实例化

4,子类继承抽象类后,必须重写父类中的抽象成员。但是如果子类也是抽象类的话,则可以不用重写父类中的抽象成员

如果一个子类继承了一个父类,那么这个子类中必须要继承这个父类中所有的抽象成员

 

抽象类当中可以写非抽象成员,子类可以不用重写;父类没法创建对象,不能使用父类中的非抽象成员。但是子类继承父类后可以使用父类中的非抽象成员。

 

抽象类和虚方法的区别:一个父类里有实现,一个父类里没有实现。

 

 

8、如果父类的抽象方法中有参数,那么。继承这个抽象父类的子类在重写父类的方法的时候必须传入对应的参数。

 

如果抽象父类的抽象方法中有返回值,那么子类在重写这个抽象方法的时候也必须要传入返回值。

 

======

如果父类中的方法有默认的实现,并且父类需要被实例化,这时可以考虑将父类定义成一个普通类,用虚方法来实现多态。

 

如果父类中的方法没有默认实现,父类也不需要被实例化,则可以将该类定义为抽象类。

 

如果类里面需要抽象成员成员,则写为抽象类,否则普通的就行。抽象类中写虚方法是有意义的。

  #region 抽象类

    class Program

    {

        static voidMain(string[] args)

        {

            //狗狗会叫 猫咪会叫

 

            Animal a =newCat();//newDog();

            a.Bark();

 

            Console.ReadKey();

        }

    }

 

    public abstract class Animal

    {

 

        public virtual void T()

        {

            Console.WriteLine("动物有声明");

        }

 

        private int_age;

 

        public int Age

        {

            get { return_age; }

            set {_age = value; }

        }

 

        publicAnimal(int age)

        {

            this.Age= age;

        }

        public abstract voidBark();

        public abstract stringName

        {

            get;

            set;

        }

 

        //   publicabstract string TestString(string name);

 

 

        publicAnimal()

        {

 

        }

        //public void Test()

        //{

        //    //空实现

        //}

    }

 

 

    public abstract class Test : Animal

    {

 

    }

 

    public class Dog : Animal

    {

        // public abstract void Test();

 

 

        public override voidBark()

        {

            Console.WriteLine("狗狗旺旺的叫");

        }

 

        public override stringName

        {

            get

            {

                throw new NotImplementedException();

            }

            set

            {

                throw new NotImplementedException();

            }

        }

 

        //public override string TestString(string name)

        //{

        //    //throw newNotImplementedException();

        //}

    }

 

    public class Cat : Animal

    {

        public override voidBark()

        {

            Console.WriteLine("猫咪喵喵的叫");

        }

 

        public override stringName

        {

            get

            {

                throw new NotImplementedException();

            }

            set

            {

                throw new NotImplementedException();

            }

        }

    }

    #endregion

 

 

例子:

使用多态求矩形的面积和周长以及圆形的面积和周长

class Program

    {

        static voidMain(string[] args)

        {

            Shapeshape =new Square(5, 6);//newCircle(5);

            doublearea = shape.GetArea();

            doubleperimeter = shape.GetPerimeter();

            Console.WriteLine("这个形状的面积是{0},周长是{1}", area, perimeter);

            Console.ReadKey();

 

        }

    }

 

    public abstract class Shape

    {

        public abstract doubleGetArea();

        public abstract doubleGetPerimeter();

    }

    public class Circle : Shape

    {

 

        private double _r;

        public double R

        {

            get { return _r;}

            set {_r = value; }

        }

 

        publicCircle(double r)

        {

            this.R =r;

        }

        public override doubleGetArea()

        {

            return Math.PI* this.R * this.R;

        }

 

        public override doubleGetPerimeter()

        {

            return 2 *Math.PI *this.R;

        }

    }

    public class Square : Shape

    {

        private double_height;

 

        public doubleHeight

        {

            get { return_height; }

            set {_height = value; }

        }

 

        private double_width;

 

        public doubleWidth

        {

            get { return_width; }

            set {_width = value; }

        }

 

        publicSquare(double height,double width)

        {

            this.Height= height;

            this.Width= width;

        }

 

        public override doubleGetArea()

        {

            return this.Height* this.Width;

        }

 

        public override doubleGetPerimeter()

        {

            return (this.Height+this.Width) * 2;

        }

    }

 

 

例子:

用多态来实现将 移动硬盘或者U盘或者MP3插到电脑上进行读写数据

class Program

    {

        static voidMain(string[] args)

        {

            //用多态来实现 将 移动硬盘或者U盘或者MP3插到电脑上进行读写数据

 

            //MobileDisk md = new MobileDisk();

            //UDisk u = new UDisk();

            //Mp3 mp3 = new Mp3();

            //Computer cpu = new Computer();

            //cpu.CpuRead(u);

            //cpu.CpuWrite(u);

            //Console.ReadKey();

 

            MobileStorage ms=new UDisk();//newMp3();//new MobileDisk();//new UDisk();

            Computer cpu=new Computer();

            cpu.Ms = ms;

            cpu.CpuRead();

            cpu.CpuWrite();

            //Computer cpu = new Computer();

            //cpu.CpuRead(ms);

            //cpu.CpuWrite(ms);

            Console.ReadKey();

 

        }

    }

 

 

    ///<summary>

    ///抽象的父类

    ///</summary>

    public abstract class MobileStorage

    {

        public abstract voidRead();

        public abstract voidWrite();

    }

 

 

    public class MobileDisk : MobileStorage

    {

        public override voidRead()

        {

            Console.WriteLine("移动硬盘在读取数据");

        }

        public override voidWrite()

        {

            Console.WriteLine("移动硬盘在写入数据");

        }

    }

    public class UDisk : MobileStorage

    {

        public override voidRead()

        {

            Console.WriteLine("U盘在读取数据");

        }

 

        public override voidWrite()

        {

            Console.WriteLine("U盘在写入数据");

        }

    }

    public class Mp3 : MobileStorage

    {

        public override voidRead()

        {

            Console.WriteLine("MP3在读取数据");

        }

 

        public override voidWrite()

        {

            Console.WriteLine("Mp3在写入数据");

        }

 

        public voidPlayMusic()

        {

            Console.WriteLine("MP3自己可以播放音乐");

        }

    }

 

 

 

    public class Computer

    {

        private MobileStorage_ms;

 

        public MobileStorage Ms

        {

            get { return_ms; }

            set {_ms = value; }

        }

        public voidCpuRead()

        {

            Ms.Read();

        }

 

        public voidCpuWrite()

        {

            Ms.Write();

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值