zoukankan      html  css  js  c++  java
  • 面向对象之多态的三种方式

    多态是面向对象最重要的特征之一,它能使得单一的类变得更多种类。简单来说多态主要有三种形式,分别是虚方法、接口和抽象类,三者各有特点,下面是代码对他们的描述:

      1  class Program
      2     {
      3         static void Main(string[] args)
      4         {
      5             //1.virtual method
      6             Chinese ch1 = new Chinese("周润发");
      7             Chinese ch2 = new Chinese("刘德华");
      8             American am1 = new American("奥巴马");
      9             American am2 = new American("克林顿");
     10             Person[] persons = { ch1, ch2, am1, am2 };
     11             for (int i = 0; i < persons.Length; i++)
     12             {
     13                 //if (persons[i] is Chinese)
     14                 //{
     15                 //    ((Chinese)persons[i]).SayHello();
     16                 //}
     17                 //else if (persons[i] is American)
     18                 //{
     19                 //    ((American)persons[i]).SayHello();
     20                 //}
     21                 //如果在父类中不用virtual方法,子类中不用override方法,我们需要上面的一段来实现,如果有更多的子类的话,
     22                 //那上面的判断可能会更多,相比之下下面的一句代码显得更为简洁。
     23                 persons[i].SayHello();
     24             }
     25 
     26             //2.abstract class
     27             Animal a = new Dog();
     28             Animal b = new Cat();
     29             //Animal c = new Animal();抽象类不能被实例化
     30             a.Bark();
     31             b.Bark();
     32 
     33             //3.interface
     34             IFly bird = new Bird();
     35             IFly student = new Student();
     36             bird.Fly();
     37             student.Fly();
     38             Console.ReadLine();
     39         }
     40     }
     41     ///如果父类中的方法有默认的实现,并且父类需要被实例化,这时可以考虑将父类定义成一个普通类,用虚方法来实现多态。
     42     ///如果父类中的方法没有默认实现,父类也不需要被实例化,则可以将该类定义为抽象类。
     43     #region virtual method
     44     /// <summary>
     45     /// 将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。
     46     /// </summary>
     47     public class Person
     48     {
     49         private string _name;
     50         public string Name
     51         {
     52             get { return _name; }
     53             set { _name = value; }
     54         }
     55         public Person(string name)
     56         {
     57 
     58             this.Name = name;
     59         }
     60         public virtual void SayHello()
     61         {
     62             Console.WriteLine("你好,我是人类。");
     63         }
     64     }
     65     public class Chinese : Person
     66     {
     67         public Chinese(string name) : base(name) { }
     68         public override void SayHello()
     69         {
     70             //base.SayHello();
     71             Console.WriteLine("你好,我是中国人,我叫{0}.", this.Name);
     72         }
     73     }
     74     public class American : Person
     75     {
     76         public American(string name) : base(name) { }
     77         public override void SayHello()
     78         {
     79             //base.SayHello();
     80             Console.WriteLine("你好,我是美国人,我叫{0}.", this.Name);
     81         }
     82     }
     83     #endregion
     84 
     85     #region abstract class
     86     /// <summary>
     87     /// 1.抽象类中的抽象方法不能有方法体,但是非抽象方法可以有方法体
     88     /// 2.抽象类中的抽象成员必须在子类中全部实现
     89     /// 3.抽象成员的访问修饰符不能是private
     90     /// 4.抽象类不能被实例化
     91     /// 5.抽象类可以有构造函数,接口则没有
     92     /// </summary>
     93     public abstract class Animal
     94     {
     95         private int age;
     96 
     97         public int Age
     98         {
     99             get
    100             {
    101                 return age;
    102             }
    103 
    104             set
    105             {
    106                 age = value;
    107             }
    108         }
    109         public Animal(int age)
    110         {
    111             this.Age = age;
    112         }
    113         public abstract string Name { get; set; }
    114         public virtual void T()
    115         {
    116             Console.WriteLine("动物有声明。。。");
    117         }
    118         public abstract void Bark();
    119         public void Eat() { }
    120         public Animal() { }
    121 
    122     }
    123     public class Dog : Animal
    124     {
    125         public override void Bark()
    126         {
    127             Console.WriteLine("狗狗有声明。。。");
    128         }
    129         public override string Name
    130         {
    131             get
    132             {
    133                 throw new NotImplementedException();
    134             }
    135 
    136             set
    137             {
    138                 throw new NotImplementedException();
    139             }
    140         }
    141     }
    142     public class Cat : Animal
    143     {
    144         public override string Name
    145         {
    146             get
    147             {
    148                 throw new NotImplementedException();
    149             }
    150 
    151             set
    152             {
    153                 throw new NotImplementedException();
    154             }
    155         }
    156 
    157         public override void Bark()
    158         {
    159             Console.WriteLine("猫咪有声明。。。");
    160         }
    161     }
    162     #endregion
    163 
    164     #region interface
    165     /// <summary>
    166     /// 1.接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符为public,不能修改。
    167     /// 2.接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。
    168     /// 3.一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口IA,那么语法上A必须写在IA的前面。
    169     /// </summary>
    170     public interface IFly
    171     {
    172         void Fly();//不允许有访问修饰符,默认是public
    173     }
    174     public class Bird : IFly
    175     {
    176         public void Fly()
    177         {
    178             Console.WriteLine("鸟儿在飞。。。");
    179         }
    180     }
    181     public class Student : IFly
    182     {
    183         public void Fly()
    184         {
    185             Console.WriteLine("人类在飞。。。");
    186         }
    187     }
    188     #endregion
    View Code
  • 相关阅读:
    日志配置
    Mybaties核心配置文件
    配置3
    写了两个数据获得方式----费劲周折
    applicationContext
    配置2
    Django-缓存的配置
    RabbitMQ的工作模式
    centos下保留python2安装python3
    python位运算
  • 原文地址:https://www.cnblogs.com/AngryShoes/p/5730976.html
Copyright © 2011-2022 走看看