Initial commit: C# 学习笔记和示例代码
- Lesson 01-10: C# 基础语法 - WebView2: 集成示例 - notes/: 详细笔记
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson06.Animals
|
||||
{
|
||||
class Animal
|
||||
{
|
||||
protected string name;
|
||||
|
||||
public Animal(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void Eat()
|
||||
{
|
||||
Console.WriteLine($"{name} 在吃东西");
|
||||
}
|
||||
|
||||
// virtual 允许子类重写
|
||||
public virtual void Speak()
|
||||
{
|
||||
Console.WriteLine($"{name} 发出了声音");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson06.Animals
|
||||
{
|
||||
class Cat : Animal
|
||||
{
|
||||
public Cat(string name) : base(name)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Speak()
|
||||
{
|
||||
Console.WriteLine($"{name} 喵喵喵~");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson06.Animals
|
||||
{
|
||||
class Dog : Animal
|
||||
{
|
||||
private string breed;
|
||||
|
||||
// base(name) 调用父类的构造函数
|
||||
public Dog(string name, string breed) : base(name)
|
||||
{
|
||||
this.breed = breed;
|
||||
}
|
||||
|
||||
public void Info()
|
||||
{
|
||||
Console.WriteLine($"名字: {name}, 品种: {breed}");
|
||||
}
|
||||
|
||||
// 重写父类方法
|
||||
public override void Speak()
|
||||
{
|
||||
base.Speak(); // 先调用父类方法
|
||||
Console.WriteLine($"{name} 汪汪汪!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson06.Devices
|
||||
{
|
||||
class Camera : IDevice
|
||||
{
|
||||
private string name;
|
||||
public bool IsConnected { get; private set; }
|
||||
|
||||
public Camera(string name)
|
||||
{
|
||||
this.name = name;
|
||||
this.IsConnected = false;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (!IsConnected)
|
||||
{
|
||||
IsConnected = true;
|
||||
Console.WriteLine($"[{name}] 摄像头已连接");
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
IsConnected = false;
|
||||
Console.WriteLine($"[{name}] 摄像头已断开");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Lesson06.Devices
|
||||
{
|
||||
// 接口:定义契约
|
||||
interface IDevice
|
||||
{
|
||||
void Connect();
|
||||
void Disconnect();
|
||||
bool IsConnected { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson06.Devices
|
||||
{
|
||||
class Microphone : IDevice
|
||||
{
|
||||
private string name;
|
||||
public bool IsConnected { get; private set; }
|
||||
|
||||
public Microphone(string name)
|
||||
{
|
||||
this.name = name;
|
||||
this.IsConnected = false;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (!IsConnected)
|
||||
{
|
||||
IsConnected = true;
|
||||
Console.WriteLine($"[{name}] 麦克风已连接");
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
IsConnected = false;
|
||||
Console.WriteLine($"[{name}] 麦克风已断开");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,55 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user