C#学习笔记(十五):抽象方法、抽象类、多态和接口

时间:2024-04-12 17:36:42

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

C#学习笔记(十五):抽象方法、抽象类、多态和接口

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
}
}
}