CSharp-Learning/lessons/Lesson06/Program.cs
Rosmontis_Cloud f604d53191 Initial commit: C# 学习笔记和示例代码
- Lesson 01-10: C# 基础语法
- WebView2: 集成示例
- notes/: 详细笔记
2026-07-01 16:31:35 +08:00

56 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}