.Net中的多态知识点

多态,不同的对象对同一个方法有不同的响应。
目的,为了解决继承中父类访问子类方法要转型的问题。
还是继承的问题,父类实例化子类,被看成父类类型。

条件:
多态 (3种方式)
1, 基于继承 重写 父类实例子类化(父类的引用指向子类的对象)
2, 抽象类和抽象方法
3, 接口实现

本文主要讲
抽象父类多态(abstract),和虚方法多态(virtual)。

1,抽象多态。
注意点 :
a,抽象方法没有方法体,并且用abstract修饰。
b,抽象方法必须放在抽象类中,类也用abstract修饰。
c,抽象类不能被实例化。
d,抽象类具有强制性。
父类有抽象方法,非抽象子类也必须有。
但是子类也是抽象类,可不必实现。
这样,方便代码规范,管理,及命名标准。

using System;namespace 多态
{class Program{static void Main(string[] args){Console.WriteLine("Hello World!");Person[] p = new Person[2];p[0] = new Student();p[1] = new Hoker();//p 可以作为父类的超级容器, 像窗体 form 一样, 里面可以装很多控件。for (int i = 0; i < p.Length; i++){p[i].Study();//只能访问抽象过的重写方法。对其它方法...字段不行。}//这样父类可以访问子类被重写的方法, 但是要转型才可访问非重写方法。Student x = new 小学生();x.Show();Student s = new Student();s.Study();}}abstract class Person{public abstract void Study();}class Student : Person {string Write;public string learing;public string Write1 { get => Write; set => Write = value; }public override void Study(){Console.WriteLine("我是学生,我要在知识的海洋里遨游。");}public virtual void Show(){Console.WriteLine("学生的虚方法");}}class Hoker : Person {public override void Study(){Console.WriteLine("我是hoker,我要睡便全球。");}}class 小学生 : Student{//多态的强制性只对父子类。public override void Show(){Console.WriteLine("小学生 ,虚方法子类。");}}}/*
结果:
我是学生,我要在知识的海洋里遨游。
我是hoker,我要睡便全球。小学生 ,虚方法子类。
我是学生,我要在知识的海洋里遨游。
*//*
以上代码分析:
person 是抽象类, 父类。里面有抽象方法study() , 子类必须实现。
父类对象, 和子类对象便可实现这个方法,形成多态的一种。student继承了person 重写了study 多态便有了意义。student还有virtual show方法。 子类 小学生 里面的show方法也重写了student的show方法。虚方法也可实现多态。
virtual 没有强制性, 可以不必实现, 实现了就不用转型。hoker类 也继承了person 也重写了 study 方法。p[0] 和 p[1] 
x 和 s 
都分别对study 和 show 有了不同的反应。
*/

2,虚方法多态。
a,虚方法所在的类,可以不抽象,当成普通类使用。
b,子类可以选择性的实现虚方法,或者不实现,直接继承。
c,虚方法和抽象都解决了父类实例化子类对象需要判断,转型的问题。
d,虚方法没有强制性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421212.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

框架

http://kohanaframework.org/转载于:https://www.cnblogs.com/gaohuag/p/3549237.html

leetcode53 dp and 分治

leetcode53 1.dp动态规划 class Solution { public:int maxSubArray(vector<int>& nums) {int lennums.size();if(len0) return 0;if(len1) return nums[0];vector<int>dp(len,0);dp[0]nums[0];int max_numdp[0];int i1;for(;i!len;i){if(dp[i-1]>0)dp[i]d…

centos Ipython安装

1&#xff0c;直接 sudo yum install ipython可能出现问题&#xff0c;不一定能成&#xff1b; 2&#xff0c;可先安装下列东西&#xff0c;然后如上安装ipython sudo yum install python-matplotlib # 2D 绘图库 sudo yum install PyQt4 # Qt4 的 Python 绑定 sudo yum ins…

.Net 中接口应用的知识点(排序)

接口 接口可以看作是多态的一种。它打破了里氏替换原则。即不是共同的生物&#xff0c;比如动物&#xff08;狗&#xff09;和人&#xff08;老师&#xff09;&#xff0c;这两个类 却都有吃这种方法。但是继承里不能把老师和狗归为一个类。所以这时用接口来解决这种问题。 语法…

struts-config message-resources配置问题总结

问题&#xff1a;我的app无法读取配置好的ApplicationResources.properties中的内容 解答&#xff1a;文件目录为 /webapp /WEB-INF /classes ApplicationResources.properties /xxx /yyy SomeOther.properties struts-config.xml的内容是 <message-resources parameter&quo…

leetcode 88

leetcode 88 简简单单 class Solution { public:void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {int j0;for(int im;i!mn;i){nums1[i]nums2[j];}sort(nums1.begin(),nums1.end());} };END

linux下memcache安装

安装配置 1. 安装libevent # tar zxf libevent-1.4.6-stable.tar.gz # cd libevent-1.4.6-stable # ./configure # make && make install 2. 安装memcached # tar zxvf memcached-1.2.6.tar.gz # cd memcached-1.2.6 # ./configure --prefix/usr/local/memcached-1.2.6…

集合与泛型集合与键值对集合

1&#xff0c;集合 &#xff08;Connections&#xff09; ArrayList arr new ArrayList();//可以add arr.Add("Hello girls!");//支持添加object类型,但不能用foreach &#xff0c;因为ArrayList是不确定类型。//Advantages&#xff1a;//1,object 类型 作为集合的类…

leetcode350C++

leetcode 350 哈希做法 没学过哈希&#xff0c;啥时候学一下 class Solution { public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {unordered_map<int,int>m;vector<int>ret;for(int n:nums1){if(m.find(n)m.end…

leetcode 121

leetcode 121 dp? my answer class Solution { public:int maxProfit(vector<int>& prices) {int bdprices[0],sd0;int max_num0;for(int i1;i!prices.size();i){sdprices[i];if(sd<bd) bdsd;else if(sd-bd>max_num)max_numsd-bd;}return max_num;} };END

statcounter统计的浏览器市场占有率

Source: StatCounter Global Stats - Browser Market Share 转载于:https://www.cnblogs.com/Wayou/p/browser_market_share.html

1008. Elevator (20)

1008. Elevator (20) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueThe highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator wil…

.net中的硬盘操作(针对Windows )

1&#xff0c;文件与文件夹的读取&#xff08;针对Windows &#xff09; 这是程序员的基本功。 做这种操作首先要引入一个命名空间 using System.IO I input 输入 O output 输出File.Create("C:\1.txt"); file是个静态类&#xff0c;里面有很多方法&#xff0c;多是…

1084. Broken Keyboard (20)

1084. Broken Keyboard (20) 时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueOn a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on scr…

ubuntu 10.4非法关机后上不了网

用的好好的ubuntu 10.4&#xff0c;非法关机后居然上不了网&#xff0c;右上角的网络图标也不见了&#xff0c;还以为是网卡问题&#xff0c;进入xp&#xff0c;发现一切正常&#xff0c;心里不断地诅咒ubuntu&#xff0c;该死的ubuntu&#xff0c;我windows还天天非法关机呢&a…

.net关于app.config的使用 对于自己的类库

类库中有 ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;则app.config 中 <connectionStrings><add name "conStr" connectionString " Data Source .;Initial Catalog ***;Integrated Security True"/>…

优秀设计师是如何炼成的,看搜狐如何做设计

《设计之下》终于上市了&#xff0c;回忆一下整个成书的过程&#xff0c;不得不佩服作者&#xff0c;从最开始的干净利落&#xff0c;到最终的细节的调整&#xff0c;作为编辑我对他们的认识逐渐加深&#xff0c;也慢慢了解了优秀设计师是如何炼成的。 这本书源于2012年12月份的…

1092. To Buy or Not to Buy (20)

1092. To Buy or Not to Buy (20) 时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueEva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings …

.net多层结构 sql注入

1&#xff0c;功能单一型 解释&#xff1a;比如一个软件是由 界面部分 User Interface 代码逻辑 Business Logic Layer 数据库部分 Data Access Layer 组成。 但是为什么一个常见的软件要搞得这么复杂&#xff1f;为什么不能把以上三种融为一体。这样不就避免了 【中间件】的学…

Java中文字符所占的字节数

本文由广州疯狂软件java培训为你整理&#xff1a; Java语言中&#xff0c;中文字符所占的字节数取决于字符的编码方式&#xff0c;一般情况下&#xff0c;采用ISO8859-1编码方式时&#xff0c;一个中文字符与一个英文字符一样只占1个字节;采用GB2312或GBK编码方式时&#xff0c…