C#-面向对象的多态思想 ---ShinePans

来源:互联网 发布:深圳unity3d培训 编辑:程序博客网 时间:2024/06/05 06:07

总结: 多态是面向对象的核心.---------可以理解为一个方法,多种实现,

在这里可以用虚方法,抽象类,接口可以实现多态


1.首先利用接口来实现多态:

接口相当于"功能,"接口可以实现多继承,分为 显式实现接口和隐式实现接口 关键字为interface

格式:

    interface  接口名

     string ......

     int ....

     void 方法名();  //定义方法

}

代码示例:(显示实现接口)

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Interface_test{    class Program    {        static void Main(string[] args)        {            Car c = new Car();            c.Color = "黑色";            c.Run();            c.Stop();            System.Console.ReadLine();        }    }    interface Icar    {        void Run();        void Stop();    }    interface ICarColor    {        string Color { get; set; }    }    class Car:Icar,ICarColor          //定义Car类并继承了多个接口    {        private string color;        public string Color        {            get            {                return color;            }            set            {                color = value;            }        }        public void Run()    //实现接口方法        {            System.Console.WriteLine("{0}汽车启动!", color);        }        public void Stop()        {            System.Console.WriteLine("{0}汽车停下!", color);        }    }}


代码示例:(隐式实现接口)

class program

     static void Main()

       {

              Car c=new Car();

              c.show();    //调用Car的show();

              Icar ic=c;   //将Car 转换为 Icar接口

              ic.show()

              System.Console.ReadLine();

        }

     interface Icar

           {

                  void show();

           }

    class Car:ICar

{

         public void show()

               {

                         System.Console.WriteLine("执行show()");

               }

}

      }

2.其次利用抽象类实现多态:

抽象类是不具体的,定义方法时只需要定义抽象方法,抽象类不能实例化

格式:

public abstract class类名

{

      public string Name; //定义字段

      public void show() 

        {

         }        //定义方法

     public abstract string Color { get;set}  //定义抽象属性

     public abstract void Run(); //定义抽象方法

     public abstrat void Stop();

}

代码示例:


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Abstract_test{    class Program    {        static void Main(string[] args)        {            Car c = new BMW();            c.Name = "宝马";            c.Color = "红色";            c.show();            c.Run();            c.Stop();            System.Console.ReadLine();        }    }    public abstract class Car    {        public string Name;        public void show()        {            System.Console.WriteLine("show方法:这是一辆"+Name);        }        public abstract string Color { get; set; } //定义抽象属性        public abstract void Run();        public abstract void Stop();    }    public class BMW :Car{        private string color;        public override string Color        {            get            {                   return color;            }            set            {                color=value;            }        }        public override void Run()  //实现抽象类的抽象方法        {            System.Console.WriteLine("实现Run()方法:{0}{1}启动", color, Name);        }        public override void Stop()        {            System.Console.WriteLine("实现Stop()方法:{0}{1}停止", color, Name);        }}}



抽象类总结: 抽象类中可以写方法,也可以定义抽象方法,在实现这个抽象类的类时,抽象方法需要实现


3.最后可以利用虚方法来实现


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Virtual_Test{    class Program    {        static void Main(string[] args)        {            //虚方法:可以被重写 关键字: virtual            //定义方式 public virtual string GetStrig(string name)            //{    return  name +"您好";       }            //利用override重写虚方法            Car c = new Car();            c.Name = "BMW";            c.run();            NewCar n = new NewCar();            n.Name = "BMW";            n.run();            Console.ReadLine();        }        class Car        {            private string name;            public string Name        {            get { return name; }            set { name = value.Length > 0 ? value : name; }        }            public virtual void run()            {                Console.WriteLine("{0}汽车跑起来", name);            }        }        class NewCar:Car  //NewCar 继承自Car        {            public override void run()   //重写虚方法            {                Console.WriteLine("新的{0}汽车跑起来", Name);            }        }    }}




8 0
原创粉丝点击