2016年11月27日--面向对象:多态、类库、委托、is和as运算符、泛型集合
1、虚方法 virtual
重写 override
父类中的方法,在子类中并不适用,那么子类需要自主更改继承的方法或者是属性,那父类中加了virtual关键字的方法才可以被子类重写,子类重写父类的方法使用的是override关键字
例:
我们都知道,喜鹊(Magpie)、老鹰(Eagle)、企鹅(Penguin)都是属于鸟类,我们可以根据这三者的共有特性提取出鸟类(Bird)做为父类,喜鹊喜欢吃虫子,老鹰喜欢吃肉,企鹅喜欢吃鱼。
创建基类Bird如下,添加一个虚方法Eat():
/// <summary>
/// 鸟类:父类
/// </summary>
public class Bird
{
/// <summary>
/// 吃:虚方法
/// </summary>
public virtual void Eat()
{
Console.WriteLine("我是一只小小鸟,我喜欢吃虫子~");
}
}
Bird
创建子类Magpie如下,继承父类Bird,重写父类Bird中的虚方法Eat():
/// <summary>
/// 喜鹊:子类
/// </summary>
public class Magpie:Bird
{
/// <summary>
/// 重写父类中Eat方法
/// </summary>
public override void Eat()
{
Console.WriteLine("我是一只喜鹊,我喜欢吃虫子~");
}
}
Magpie
创建一个子类Eagle如下,继承父类Bird,重写父类Bird中的虚方法Eat():
/// <summary>
/// 老鹰:子类
/// </summary>
public class Eagle:Bird
{
/// <summary>
/// 重写父类中Eat方法
/// </summary>
public override void Eat()
{
Console.WriteLine("我是一只老鹰,我喜欢吃肉~");
}
}
Eagle
创建一个子类Penguin如下,继承父类Bird,重写父类Bird中的虚方法Eat():
/// <summary>
/// 企鹅:子类
/// </summary>
public class Penguin:Bird
{
/// <summary>
/// 重写父类中Eat方法
/// </summary>
public override void Eat()
{
Console.WriteLine("我是一只小企鹅,我喜欢吃鱼~");
}
}
Penguin
到此,一个基类,三个子类已经创建完毕,接下来我们在主函数中来看下多态是怎样体现的。
static void Main(string[] args)
{
//创建一个Bird基类数组,添加基类Bird对象,Magpie对象,Eagle对象,Penguin对象
Bird[] birds = {
new Bird(),
new Magpie(),
new Eagle(),
new Penguin()
};
//遍历一下birds数组
foreach (Bird bird in birds)
{
bird.Eat();
}
Console.ReadKey();
}
运行结果:

2、抽象类
是因为普通的多态虚方法没有人用本身的方法主体,那么...
抽象类就是来当亲爹,abstract
抽象类中可以有抽象方法:abstract
抽象方法一定在抽象类中,但是抽象类中不一定只有抽象方法,也可以有普通方法
例:
我们把Bird父类改成抽象类,Eat()方法改成抽象方法。代码如下:
/// <summary>
/// 鸟类:基类
/// </summary>
public abstract class Bird
{
/// <summary>
/// 吃:抽象方法
/// </summary>
public abstract void Eat();
}
Bird
抽象类Bird内添加一个Eat()抽象方法,没有方法体。也不能实例化。
其他类Magpie,Eagle,Penguin代码不变,子类也是用override关键字来重写父类中抽象方法。
Main主函数中Bird就不能创建对象了,代码稍微修改如下:
static void Main(string[] args)
{
//创建一个Bird基类数组,添加 Magpie对象,Eagle对象,Penguin对象
Bird[] birds = {
new Magpie(),
new Eagle(),
new Penguin()
};
//遍历一下birds数组
foreach (Bird bird in birds)
{
bird.Eat();
}
Console.ReadKey();
}
运行结果:

3、接口
接口就是出来做 干爹的,接口不是类 interface
编写方法格式: string 方法名();
实现接口的方法 不要override,直接正常编写就可以了
例:
添加一个接口IFlyable,代码如下:
/// <summary>
/// 飞 接口
/// </summary>
public interface IFlyable
{
void Fly();
}
IFlyable
喜鹊Magpie实现IFlyable接口,代码如下:
/// <summary>
/// 喜鹊:子类,实现IFlyable接口
/// </summary>
public class Magpie:Bird,IFlyable
{
/// <summary>
/// 重写父类Bird中Eat方法
/// </summary>
public override void Eat()
{
Console.WriteLine("我是一只喜鹊,我喜欢吃虫子~");
}
/// <summary>
/// 实现 IFlyable接口方法
/// </summary>
public void Fly()
{
Console.WriteLine("我是一只喜鹊,我可以飞哦~~");
}
}
Magpie
老鹰Eagle实现IFlyable接口,代码如下:
/// <summary>
/// 老鹰:子类实现飞接口
/// </summary>
public class Eagle:Bird,IFlyable
{
/// <summary>
/// 重写父类Bird中Eat方法
/// </summary>
public override void Eat()
{
Console.WriteLine("我是一只老鹰,我喜欢吃肉~");
}
/// <summary>
/// 实现 IFlyable接口方法
/// </summary>
public void Fly()
{
Console.WriteLine("我是一只老鹰,我可以飞哦~~");
}
}
Eagle
在Main主函数中,创建一个IFlyable接口数组,代码实现如下:
static void Main(string[] args)
{
//创建一个IFlyable接口数组,添加 Magpie对象,Eagle对象
IFlyable[] flys = {
new Magpie(),
new Eagle()
};
//遍历一下flys数组
foreach (IFlyable fly in flys)
{
fly.Fly();
}
Console.ReadKey();
}
执行结果:

拓展:链接
类库:
其实就是一堆类文件,只不过你看不到这些类的源代码,保密性好。
优点:保密性好
缺点:如果这个方法不好用,使用者无法自己去更改它。
委托:
委托可以理解为:函数的指针
关键词:delegate
声明:
public delegate int FirstDel(int a, int b);
创建委托变量:
FirstDel 名字 = 与这个委托类型相同的方法;
is和as运算符:
is : 判断某一个对象是否是某一种类型
对象 is 类型名 是这种类型返回 true 不是返回 false
as : 将某一个对象转换成某一种类型,如果转换不成功,那么会给这个对象赋一个null,不会抛出异常
泛型集合 List<T>
综合了集合与数组的优点,
固定数据类型,不限制长度的一种集合
2016年11月27日--面向对象:多态、类库、委托、is和as运算符、泛型集合的更多相关文章
- 2016年11月27日 星期日 --出埃及记 Exodus 20:18
2016年11月27日 星期日 --出埃及记 Exodus 20:18 When the people saw the thunder and lightning and heard the trum ...
- 2016年11月24日--面向对象、C#小复习
面对对象就是:把数据及对数据的操作方法放在一起,作为一个相互依存的整体——对象.对同类对象抽象出其共性,形成类.类中的大多数数据,只能用本类的方法进行处理.类通过一个简单的外部接口与外界发生关系,对象 ...
- 我的Python成长之路---第七天---Python基础(22)---2016年2月27日(晴)
socket网络编程 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ...
- 2016年12月27日 星期二 --出埃及记 Exodus 21:22
2016年12月27日 星期二 --出埃及记 Exodus 21:22 "If men who are fighting hit a pregnant woman and she gives ...
- 2016年11月30日 星期三 --出埃及记 Exodus 20:21
2016年11月30日 星期三 --出埃及记 Exodus 20:21 The people remained at a distance, while Moses approached the th ...
- 2016年11月29日 星期二 --出埃及记 Exodus 20:20
2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...
- 2016年11月28日 星期一 --出埃及记 Exodus 20:19
2016年11月28日 星期一 --出埃及记 Exodus 20:19 and said to Moses, "Speak to us yourself and we will listen ...
- 2016年11月26日 星期六 --出埃及记 Exodus 20:17
2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...
- 2016年11月25日 星期五 --出埃及记 Exodus 20:16
2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...
随机推荐
- HAOI2015 泛做
T1 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两之间的 ...
- Centos6安装Gitlab
安装参考 https://about.gitlab.com/downloads/ 可以从清华的镜像下载安装包, 注意区分自己用的是哪个发行版 https://mirror.tuna.tsinghua. ...
- dwarf格式解析
debug_line中包含的是地址和源文件行之间的关系 我今天想搞清楚的是文件的C代码和汇编代码之间的关系: 对这块之前一直是迷迷糊糊的,发现这个问题已经严重影响到bug的定位了. 之前感觉C和汇编不 ...
- 关于NODE NPM 输入命令后没反应的问题
输入NPM 命令 如 install config help都没有反应,光标在下面一直闪,只有 -v 有反应,查了下,是npm config set prefix 改包的路径出问题了 解决办法就是删 ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- myeclipse 注释模板
选中你要加注释的方法或类,按 Alt + shift + J.
- 51nod DP 最大子段和
#include<iostream> #include<algorithm> #include<cstdio> #define MAXN 50000 using n ...
- CURL
基本语法: function curl($url){ $ch=curl_init(); //初始化 curl_setopt($ch, CURLOPT_URL, $url); //核心 curl_se ...
- .Net Core Linux centos7行—hyper-v安装linux系统和.net core sdk
下载linux系统,选择安装centos7 下载地址:https://www.centos.org/download/ 安装centos7 hyper-v选择新建虚拟机 根据向导一路next,虚拟机代 ...
- gdb调试常用实用命令和core dump文件的生成
1.生成core dump文件的方法: $ ulimit -c //查看是否为0 如果为0 $ ulimit -c unlimited 这样在程序崩溃以后会在当前目录生成一个core.xxx ...