博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象之多态的三种方式
阅读量:4364 次
发布时间:2019-06-07

本文共 5213 字,大约阅读时间需要 17 分钟。

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

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     ///  45     /// 将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。 46     ///  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     ///  87     /// 1.抽象类中的抽象方法不能有方法体,但是非抽象方法可以有方法体 88     /// 2.抽象类中的抽象成员必须在子类中全部实现 89     /// 3.抽象成员的访问修饰符不能是private 90     /// 4.抽象类不能被实例化 91     /// 5.抽象类可以有构造函数,接口则没有 92     ///  93     public abstract class Animal 94     { 95         private int age; 96  97         public int Age 98         { 99             get100             {101                 return age;102             }103 104             set105             {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 : Animal124     {125         public override void Bark()126         {127             Console.WriteLine("狗狗有声明。。。");128         }129         public override string Name130         {131             get132             {133                 throw new NotImplementedException();134             }135 136             set137             {138                 throw new NotImplementedException();139             }140         }141     }142     public class Cat : Animal143     {144         public override string Name145         {146             get147             {148                 throw new NotImplementedException();149             }150 151             set152             {153                 throw new NotImplementedException();154             }155         }156 157         public override void Bark()158         {159             Console.WriteLine("猫咪有声明。。。");160         }161     }162     #endregion163 164     #region interface165     /// 166     /// 1.接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符为public,不能修改。167     /// 2.接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。168     /// 3.一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口IA,那么语法上A必须写在IA的前面。169     /// 170     public interface IFly171     {172         void Fly();//不允许有访问修饰符,默认是public173     }174     public class Bird : IFly175     {176         public void Fly()177         {178             Console.WriteLine("鸟儿在飞。。。");179         }180     }181     public class Student : IFly182     {183         public void Fly()184         {185             Console.WriteLine("人类在飞。。。");186         }187     }188     #endregion
View Code

 

转载于:https://www.cnblogs.com/AngryShoes/p/5730976.html

你可能感兴趣的文章
【原创】MapReduce编程系列之表连接
查看>>
IOS开发之Swift学习笔记
查看>>
【Java基础】用LinkedList实现一个简单栈的功能
查看>>
线段树C-A Simple Problem with Integers(树懒线段树)
查看>>
Ferguson游戏
查看>>
PHPExcel
查看>>
create your own github repository and build link to your local project
查看>>
Leetcode-Convert Sorted Array to BST
查看>>
form表单,submit,ajax提交
查看>>
三大平衡树(Treap + Splay + SBT)总结+模板
查看>>
关于数据库名、实例名
查看>>
多线程不安全的函数列表
查看>>
Codeforces Round #318 (Div. 2) B Bear and Three Musketeers (暴力)
查看>>
SVN+AnkhSVN端配置
查看>>
mysql慢查询工具
查看>>
socket代码
查看>>
HTML5 之 简单汇总
查看>>
C#中结构体定义并转换字节数组
查看>>
前端进阶路线图
查看>>
血淋淋的事实告诉你:你为什么不应该在JS文件中保存敏感信息
查看>>