using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w4d1_abstract
{
//抽象函数、抽象类
//多态实现 写一个动物的 抽象类,写两个子类狗狗叫,猫猫叫
//Animal类Cry方法里写具体实现的问题:写什么都不合适
//实例化 一个 animal的对象 他指代现实中 哪种对象 无法解释
//如果有以上情况,我们可以用抽象函数,以便管理,以提高代码可读性
//抽象函数
//抽象函数用abstract关键字修饰
//抽象函数只能存在于抽象类中
//抽象函数不允许你实现,不需要写函数体,用空语句代替
//抽象类
//一个用abstract关键字修饰过的类,我们叫抽象类
//抽象类中可以有抽象成员,也可以有普通成员
//继承了抽象类的派生类,必须实现抽象类所有的抽象成员
//抽象类不允许我们实例化,但是有构造函数并且可以重载
//所以我们在写一个程序结构或者框架的时候会用到抽象类
class Animal
{
public virtual void Cry()
{
Console.WriteLine("");
}
}
class Cat : Animal
{
public override void Cry()
{
Console.WriteLine("喵喵");
}
}
class Dog : Animal
{
public override void Cry()
{
Console.WriteLine("汪汪");
}
}
//矩形:图形 和 圆形:图形
//多态实现求面积(area)和周长(perimeter)
abstract class Sharp
{
public abstract void Area();
public abstract void Perimeter();
}
class Circle : Sharp
{
public float r;
public override void Area()
{
Console.WriteLine("面积:{0}", 3.14f * r * r);
}
public override void Perimeter()
{
Console.WriteLine("周长:{0}", 3.14f * * r);
}
}
class Rect: Sharp
{
public float width;
public float high;
public override void Area()
{
Console.WriteLine("面积:{0}", width * high);
}
public override void Perimeter()
{
Console.WriteLine("周长:{0}", (width + high) * );
}
}
class Program
{
static void Main(string[] args)
{
//多态实现 写一个动物的 抽象类,写两个子类狗狗叫,猫猫叫
Random roll = new Random();
Animal[] animals = new Animal[];
for (int i = ; i < animals.Length; i++)
{
int num = roll.Next(, );
if (num==)
{
animals[i] = new Cat();
}
else
{
animals[i] = new Dog();
}
}
foreach (var item in animals)
{
item.Cry();
}
//矩形:图形 和 圆形:图形
//多态实现求面积(area)和周长(perimeter)
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w4d1_interface
{
#region 接口的定义和练习1
//鱼会游泳,人走,游泳,船会游泳,乌龟走 会游泳,天鹅走 飞 会游泳
//多态实现
//批量管理 某一功能
//游泳,走,飞
//不同对象 可以使用同一方法 表现不同行为
//这些对象要同一个结构(数组)里 管理结构(容器)
//
//如果多个接口出现了同名的成员,在实现的时候,默认是共用的
//如果你想区分不同接口的不同方法,我们可以使用接口的显示实现
//接口名.成员名
//显示实现的成员必须由接口类型调用
//1,多继承
//2,多态
//和类大体一致,关键字interface
//成员没有实现,函数是没有函数体,用空语句,属相必须是自动属性
//成员只能是属性,函数,事件,索引器
//成员必须是public,不用写,也不能写
//访问修饰 关键字 接口名{}
//接口名 命名 一般以 I 为前缀
public interface ISwim
{
void Swim();
}
public interface IWalk
{
void Walk();
}
public interface IFly
{
void Fly();
}
public interface IBirdMan
{
void Swim();
}
//接口使用:
//我们可以用一个类来继承已经定义好的接口:一对一,一对多,接口之间可以相互继承
//如果在继承关系中,这个类有继承其它类,这个基类要放在继承的第一位
//继承一个接口必须实现这个接口所有成员
//能让我们把物品按特定的功能统一管理
class Animal
{
public Animal(string name)
{
this.name = name;
}
protected string name;
}
class Machine
{
public Machine(string name)
{
this.name = name;
}
protected string name;
}
class GameObject
{
public GameObject(string name)
{
this.name = name;
}
public string name;
}
class Fish : Animal, ISwim
{
public Fish() : base("小鱼")
{
Program.swims[Program.swimsCount++] = this;
}
public void Swim()
{
Console.WriteLine("{0}在游泳", name);
}
}
class Person : GameObject, ISwim, IWalk
{
public Person() : base("人类")
{
Program.swims[Program.swimsCount++] = this;
Program.walks[Program.walksCount++] = this;
}
public void Swim()
{
Console.WriteLine("{0}在游泳", name);
}
public void Walk()
{
Console.WriteLine("{0}在行走", name);
}
}
class Ship : Machine, ISwim
{
public Ship() : base("轮船")
{
Program.swims[Program.swimsCount++] = this;
}
public void Swim()
{
Console.WriteLine("{0}在游泳", name);
}
}
class Swan : Animal, ISwim, IWalk, IFly
{
public Swan() : base("天鹅")//写public,表明继承和实现该接口成员,不写则会认为是一个私有的新的方法
{
Program.swims[Program.swimsCount++] = this;
Program.walks[Program.walksCount++] = this;
Program.flys[Program.flysCount++] = this;
}
public void Swim()
{
Console.WriteLine("{0}在游泳", name);
}
public void Walk()
{
Console.WriteLine("{0}行走", name);
}
public void Fly()
{
Console.WriteLine("{0}在飞行", name);
}
}
class BirdPerson : Person, IFly, IBirdMan
{
public BirdPerson() : base()
{
this.name = "鸟人";
Program.flys[Program.flysCount++] = this;
}
public void Fly()
{
Console.WriteLine("{0}在飞行", name);
}
void IBirdMan.Swim()
{
Console.WriteLine("{0}在潜水", name);
}
}
#endregion
#region 接口的练习2
//接口实现
//人口登记(Person)
//汽车登记(Car )
//房子也要登记(House )
//都需要有注册(Register)方法
public interface IRegister
{
void Register();
}
class Name
{
public Name(string name)
{
this.name = name;
}
protected string name;
}
class Person1 : Name, IRegister
{
public Person1() : base("小明")
{
Program.registers[Program.registersCount++] = this;
}
public void Register()
{
Console.WriteLine("{人口登记:{0}已登记", name);
}
}
class Car : Name, IRegister
{
public Car() : base("小车")
{
Program.registers[Program.registersCount++] = this;
}
public void Register()
{
Console.WriteLine("汽车登记:{0}已登记", name);
}
}
class House : Name, IRegister
{
public House() : base("小房")
{
Program.registers[Program.registersCount++] = this;
}
public void Register()
{
Console.WriteLine("小房登记:{0}已登记", name);
}
}
#endregion
#region 接口的作业
//多态来模拟移动硬盘、U盘、MP3插到电脑上读取数据
public abstract class MobileStorage
{
public abstract void Read();
public abstract void Write();
}
public class MobileDisk : MobileStorage
{
public override void Read()//override可以重写抽象方法和虚方法
{
Console.WriteLine("移动硬盘在读取数据");
}
public override void Write()
{
Console.WriteLine("移动硬盘在写入数据");
}
}
public class UDisk : MobileStorage
{
public override void Read()
{
Console.WriteLine("U盘在读取数据");
}
public override void Write()
{
Console.WriteLine("U盘在写入数据");
}
}
public class Mp3 : MobileStorage
{
public override void Read()
{
Console.WriteLine("Mp3在读取数据");
}
public override void Write()
{
Console.WriteLine("Mp3在写入数据");
}
public void PlayMusic()
{
Console.WriteLine("MP3自己可以播放音乐");
}
}
public class Computer
{
//方法2,拿字段存传入的父类参数
private MobileStorage _ms;
public MobileStorage Ms//字段封装属性
{
get { return _ms; }
set { _ms = value; }
}
public void CpuRead()//方法1,想拿到父类,传参数进来就行,可以传子类进来
{
Ms.Read();//方法2,通过属性拿到父类,在外面不用传参数,采用赋值
}
public void CpuWrite()
{
Ms.Write();
}
//public void CpuRead(MobileStorage ms)//方法1,想拿到父类,传参数进来就行,可以传子类进来
//{
// ms.Read();
//}
//public void CpuWrite(MobileStorage ms)
//{
// ms.Write();
//}
}
#endregion
class Program
{
//static 修饰可以将类型,成员变成静态
//资源共享
//唯一
//程序结束的时候才会释放 public static ISwim[] swims = new ISwim[];
public static int swimsCount = ;
public static IWalk[] walks = new IWalk[];
public static int walksCount = ;
public static IFly[] flys = new IFly[];
public static int flysCount = ;
public static IRegister[] registers = new IRegister[];
public static int registersCount = ;
static void Main(string[] args)
{
#region 接口的定义和练习1
//Swan swan = new Swan();
//swan.Swim();
//Ship ship = new Ship();
//ship.Swim();
//Person P = new Person();
//P.Swim();
//ISwim[] swims = new ISwim[10];
//swims[0] = swan;
//swims[1] = ship;
//Console.WriteLine();
Fish f = new Fish();
Person P = new Person();
Ship ship = new Ship();
Swan swan = new Swan();
BirdPerson birdperson = new BirdPerson();
for (int i = ; i < swimsCount; i++)
{
swims[i].Swim();
}
for (int i = ; i < walksCount; i++)
{
walks[i].Walk();
}
for (int i = ; i < flysCount; i++)
{
flys[i].Fly();
}
#endregion
#region 接口的练习2
//接口实现
//人口登记(Person)
//汽车登记(Car )
//房子也要登记(House )
//都需要有注册(Register)方法
Person1 p1 = new Person1();
Car car = new Car();
House house = new House();
for (int i = ; i < registersCount; i++)
{
registers[i].Register();
}
#endregion
#region 接口的显示实现
//Person person = new Person();
//ISwim iswim = person;
//iswim.Swim();
//IBirdMan birdMen = person;
//birdMen.Swim();
#endregion
#region 接口的作业
//多态来模拟移动硬盘、U盘、MP3插到电脑上读取数据
//多态 模拟 移动硬盘 插入电脑 读写
//MobileDisk md = new MobileDisk();//父类抽象类不能实例化,所以实例化子类
//UDisk ud = new UDisk();
//Mp3 mp = new Mp3();
MobileStorage mp = new Mp3();
Computer cpu = new Computer();//通过属性拿到父类,在外面不能传参数,采用赋值
cpu.Ms = mp;//表示把插入的MP3赋值给电脑里面的Ms属性
//cpu.CpuRead(mp);
//cpu.CpuWrite(mp);
cpu.CpuRead();//采用属性赋值形式,不用传参数
cpu.CpuWrite();
Console.ReadKey();
#endregion
}
}
}

C#学习笔记(十五):抽象方法、抽象类、多态和接口的更多相关文章

  1. python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...

  2. Java基础学习笔记十五 集合、迭代器、泛型

    Collection 集合,集合是java中提供的一种容器,可以用来存储多个数据. 在前面的学习中,我们知道数据多了,可以使用数组存放或者使用ArrayList集合进行存放数据.那么,集合和数组既然都 ...

  3. (转载)西门子PLC学习笔记十五-(数据块及数据访问方式)

    一.数据块 数据块是在S7 CPU的存储器中定义的,用户可以定义多了数据块,但是CPU对数据块数量及数据总量是有限制的. 数据块与临时数据不同,当逻辑块执行结束或数据块关闭,数据块中的数据是会保留住的 ...

  4. (C/C++学习笔记) 十八. 继承和多态

    十八. 继承和多态 ● 继承的概念 继承(inheritance): 以旧类为基础创建新类, 新类包含了旧类的数据成员和成员函数(除了构造函数和析构函数), 并且可以派生类中定义新成员. 形式: cl ...

  5. (C/C++学习笔记) 十五. 构造数据类型

    十五. 构造数据类型 ● 构造数据类型概念 Structured data types 构造数据类型 结构体(structure), 联合体/共用体 (union), 枚举类型(enumeration ...

  6. MySQL学习笔记十五:优化(2)

    一.数据库性能评测关键指标 1.IOPS:每秒处理的IO请求次数,这跟磁盘硬件相关,DBA不能左右,但推荐使用SSD. 2.QPS:每秒查询次数,可以使用show status或mysqladmin ...

  7. Java笔记(十五)……面向对象IV多态(polymorphism)

    概述 定义:某一类事物的多种存在形态. 例:动物中猫,狗. 猫这个对象对应的类型是猫类型 猫 x = new 猫(); 同时猫也是动物中的一种,也可以把猫称为动物. 动物 y = new 猫(); 动 ...

  8. angular学习笔记(十五)-module里的'服务'

    本篇介绍angular中的模块:module 在笔记(二)http://www.cnblogs.com/liulangmao/p/3711047.html里已经讲到过模块,这篇主要讲模块的 '服务' ...

  9. Java学习笔记十五:Java中的成员变量和局部变量

    Java中的成员变量和局部变量 一:成员变量: 成员变量在类中定义,用来描述对象将要有什么 成员变量可以被本类的方法使用,也可以被其他类的方法使用,成员变量的作用域在整个类内部都是可见的 二:局部变量 ...

  10. MYSQL进阶学习笔记十五:MySQL 的账号权限赋予!(视频序号:进阶_33,34)

    知识点十六:MySQL的账号权限赋予(33) 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执 ...

随机推荐

  1. 【Fiddler】杂乱基础学习

    1.过滤fiddler筛选 打开fiddler>Tools>Fiddler Options>HTTPS>...from remote clients only,勾选这个选项就可 ...

  2. js-jquery-对象与JSON字符串互相转换

    1:jQuery插件支持的转换方式 代码如下: String→Object$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转 ...

  3. phpQuery中文手册(更新中)

    示例 phpQuery::newDocumentFileXHTML('my-xhtml.html')->find('p'); $ul = pq('ul'); 载入文档 phpQuery::new ...

  4. PAT 1067 Sort with Swap[难]

    1067 Sort with Swap(0,*) (25)(25 分) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is e ...

  5. mongodb部署

    windows版本 http://dl.mongodb.org/dl/win32/x86_64 安装教程 https://docs.mongodb.org/manual/tutorial/instal ...

  6. label--input

    .form-group { margin-bottom: 15px; position: relative;}.control-label{ float: left; width: 15%; text ...

  7. Assets.xcassets 应用

    1.应用 Assets.xcassets :用来存放图像资源文件 给项目添加 AppIcon 时图标要用 png 格式的,不要用其他格式.当是其它图片格式时 ,不要仅仅修改其后缀名,若仅仅修改后缀名, ...

  8. Visual Studio 2013旗舰版KEY

    Visual Studio 2013旗舰版KEY:BWG7X-J98B3-W34RT-33B3R-JVYW9

  9. sql when null 判断

    Sql Server 中使用case when then 判断某字段是否为null,和判断是否为字符或数字时的写法不一样,如果不注意,很容易搞错 错误方法: CASE columnName WHEN ...

  10. linux常用命令:chgrp 命令

    在 lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令变更文件与目录所属群组,这种方式采用群组名称或群组识别 码都可以.chgrp命令就是change group ...