56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using System;
|
||
using Lesson06.Animals;
|
||
using Lesson06.Devices;
|
||
|
||
namespace Lesson06
|
||
{
|
||
class Program
|
||
{
|
||
static void Main(string[] args)
|
||
{
|
||
Console.WriteLine("=== 继承演示 ===\n");
|
||
|
||
// 继承:Dog 继承了 Animal
|
||
Dog dog = new Dog("旺财", "金毛");
|
||
dog.Eat();
|
||
dog.Speak();
|
||
dog.Info();
|
||
|
||
Console.WriteLine("\n=== 方法重写 ===\n");
|
||
|
||
// 多态:Cat 和 Dog 重写了 Speak
|
||
Animal cat = new Cat("小猫");
|
||
Animal dog2 = new Dog("二狗", "哈士奇");
|
||
|
||
cat.Speak();
|
||
dog2.Speak();
|
||
|
||
Console.WriteLine("\n=== 接口演示 ===\n");
|
||
|
||
// 使用接口
|
||
Microphone mic = new Microphone("USB麦克风");
|
||
Camera cam = new Camera("RTSP摄像头");
|
||
|
||
mic.Connect();
|
||
mic.Disconnect();
|
||
|
||
Console.WriteLine();
|
||
|
||
cam.Connect();
|
||
cam.Disconnect();
|
||
|
||
Console.WriteLine("\n=== 接口多态 ===\n");
|
||
|
||
// 接口类型可以指向任何实现它的类
|
||
IDevice device1 = new Microphone("设备A");
|
||
IDevice device2 = new Camera("设备B");
|
||
|
||
device1.Connect();
|
||
device2.Connect();
|
||
|
||
Console.WriteLine("\n=== 完成 ===");
|
||
Console.ReadLine();
|
||
}
|
||
}
|
||
}
|