添加Lesson19
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
using Lesson19_多态_接口练习;
|
||||
|
||||
namespace Lesson19_多态_接口练习
|
||||
{
|
||||
interface ICheck
|
||||
{
|
||||
public void Check();
|
||||
}
|
||||
class Person : ICheck
|
||||
{
|
||||
public void Check()
|
||||
{
|
||||
Console.WriteLine("派出所登记");
|
||||
}
|
||||
}
|
||||
class Car
|
||||
{
|
||||
public void Check()
|
||||
{
|
||||
Console.WriteLine("车管所登记");
|
||||
}
|
||||
}
|
||||
class House
|
||||
{
|
||||
public void Check()
|
||||
{
|
||||
Console.WriteLine("房管局登记");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface IFly
|
||||
{
|
||||
public void Fly();
|
||||
}
|
||||
interface IWalk
|
||||
{
|
||||
public void Walk();
|
||||
}
|
||||
interface ISwim
|
||||
{
|
||||
public void Swim();
|
||||
}
|
||||
|
||||
class Sparrow : IFly, IWalk
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("麻雀飞");
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("麻雀走");
|
||||
}
|
||||
}
|
||||
class Ostrich : IWalk
|
||||
{
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("鸵鸟走");
|
||||
}
|
||||
}
|
||||
class Penguin : IWalk, ISwim
|
||||
{
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("企鹅走");
|
||||
}
|
||||
public void Swim()
|
||||
{
|
||||
Console.WriteLine("企鹅游泳");
|
||||
}
|
||||
}
|
||||
class Parrot : IFly, IWalk
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("鹦鹉飞");
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("走");
|
||||
}
|
||||
}
|
||||
class Helicopter : IFly
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("直升机飞");
|
||||
}
|
||||
}
|
||||
class Swan : IFly, IWalk, ISwim
|
||||
{
|
||||
public void Fly()
|
||||
{
|
||||
Console.WriteLine("天鹅飞");
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("天鹅走");
|
||||
}
|
||||
public void Swim()
|
||||
{
|
||||
Console.WriteLine("天鹅游泳");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface IUSB
|
||||
{
|
||||
void ReadData();
|
||||
}
|
||||
|
||||
class StorageDevice : IUSB
|
||||
{
|
||||
public string name;
|
||||
public StorageDevice(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
public void ReadData()
|
||||
{
|
||||
Console.WriteLine("USB{0}传输数据",name);
|
||||
}
|
||||
}
|
||||
|
||||
class MP3 : IUSB
|
||||
{
|
||||
public void ReadData()
|
||||
{
|
||||
Console.WriteLine("MP3传输数据");
|
||||
}
|
||||
}
|
||||
class Computer
|
||||
{
|
||||
public IUSB usb1;
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Person p1= new Person();
|
||||
p1.Check();
|
||||
Car c1 = new Car();
|
||||
c1.Check();
|
||||
House h1 = new House();
|
||||
h1.Check();
|
||||
|
||||
Sparrow s1 = new Sparrow();
|
||||
s1.Walk();
|
||||
s1.Fly();
|
||||
Ostrich o1 = new Ostrich();
|
||||
o1.Walk();
|
||||
Penguin p2 = new Penguin();
|
||||
p2.Swim();
|
||||
p2.Walk();
|
||||
Parrot p3 = new Parrot();
|
||||
p3.Walk();
|
||||
p3.Fly();
|
||||
Helicopter h2 = new Helicopter();
|
||||
h2.Fly();
|
||||
Swan s3 = new Swan();
|
||||
s3.Walk();
|
||||
s3.Fly();
|
||||
s3.Swim();
|
||||
|
||||
StorageDevice hd = new StorageDevice("移动硬盘");
|
||||
StorageDevice ud = new StorageDevice("U盘");
|
||||
MP3 mp3 = new MP3();
|
||||
Computer computer = new Computer();
|
||||
|
||||
computer.usb1 = hd;
|
||||
computer.usb1.ReadData();
|
||||
|
||||
computer.usb1 = ud;
|
||||
computer.usb1.ReadData();
|
||||
|
||||
computer.usb1 = mp3;
|
||||
computer.usb1.ReadData();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user