添加Lesson17

This commit is contained in:
2025-09-11 15:07:37 +08:00
parent aca44bc248
commit d6767da6da
6 changed files with 325 additions and 2 deletions
+12
View File
@@ -31,6 +31,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类练习", "Lesson16_继承-密封类练习\Lesson16_继承-密封类练习.csproj", "{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson17_多态-vob", "Lesson17_多态-vob\Lesson17_多态-vob.csproj", "{9B334A2E-3B5C-49AC-B1E8-2CE2FDC04158}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson17_多态-vob练习", "Lesson17_多态-vob练习\Lesson17_多态-vob练习.csproj", "{B9598C23-6AD4-44FB-B16B-6A0A80B8D9CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -93,6 +97,14 @@ Global
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Release|Any CPU.Build.0 = Release|Any CPU
{9B334A2E-3B5C-49AC-B1E8-2CE2FDC04158}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B334A2E-3B5C-49AC-B1E8-2CE2FDC04158}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B334A2E-3B5C-49AC-B1E8-2CE2FDC04158}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B334A2E-3B5C-49AC-B1E8-2CE2FDC04158}.Release|Any CPU.Build.0 = Release|Any CPU
{B9598C23-6AD4-44FB-B16B-6A0A80B8D9CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9598C23-6AD4-44FB-B16B-6A0A80B8D9CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9598C23-6AD4-44FB-B16B-6A0A80B8D9CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9598C23-6AD4-44FB-B16B-6A0A80B8D9CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -55,7 +55,8 @@
//父类的父类的构造->...父类构造->...->子类构造
class GameObject
{
public GameObject() {
public GameObject()
{
Console.WriteLine("gameobject的构造函数");
}
}
@@ -104,7 +105,7 @@
class Son : Father
{
public Son(int i):base(i)//用base来传给父类构造函数一个值
public Son(int i) : base(i)//用base来传给父类构造函数一个值
{
Console.WriteLine("son 1,参数i的值{0}", i);
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson17_多态_vob</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+134
View File
@@ -0,0 +1,134 @@
namespace Lesson17_多态_vob
{
#region
#region
class
{
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
#region 使
#endregion
#region 使
#endregion
#region
#endregion
}
#region
#endregion
#region
#endregion
#endregion
#region
#endregion
#endregion
#region
// 多态按字面意思就是“多种状态”
// 让继承同一父类的子类们 在执行相同方法时有不同的表现(状态)
// 主要目的
// 同一父类的对象 执行相同行为(方法)有不同的表现
// 解决的问题
// 让同一个对象有位移行为的特征
#endregion
#region
class Father
{
public void Speak()
{
Console.WriteLine("Father Speak");
}
}
class Son : Father
{
public void Speak()
{
Console.WriteLine("Son Speak");
}
}
#endregion
#region
//我们目前已经学过的多态
//编译时多态——函数重载,开始就写好的
//我们将学习的:
//运行时多态(vob、抽象函数、接口)
//我们今天学习 vob
//v: virtual(虚函数)
//o: override(重写)
//b: base(父类)
class GameObject
{
public string name;
public GameObject(string name)
{
this.name = name;
}
public virtual void Atk()//虚函数
{
Console.WriteLine("游戏对象攻击");
}
}
class Player : GameObject
{
public Player(string name):base(name)
{
}
//代表着重写虚函数
public override void Atk()
{
//base来保留父类的函数
base.Atk();//执行父类(base)的Atk方法
Console.WriteLine("玩家对象攻击");
}
}
#endregion
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
#region
Father f = new Son();
f.Speak();
(f as Son).Speak();
#endregion
#region 使
GameObject p = new Player("123");
p.Atk();//虽然是父类对象装载的p,但是p是他的子类player类型的
//如果不用override来重写,那么调用的atk将会是父类的atk
//既然有了虚函数和重写那么调用的就是子类的atk
#endregion
}
}
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson17_多态_vob练习</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,154 @@
using System.Threading.Channels;
namespace Lesson17_多态_vob练习
{
#region
class Duck
{
public virtual void Speak()
{
Console.WriteLine("嘎嘎叫");
}
}
class WoodDuck : Duck
{
public override void Speak()
{
Console.WriteLine("吱吱叫");
}
}
class RubberDuck : Duck
{
public override void Speak()
{
Console.WriteLine("叽叽叫");
}
}
#endregion
#region
class Staff
{
public virtual void Clockin()
{
Console.WriteLine("员工9点打卡");
}
}
class Boss: Staff
{
public override void Clockin()
{
Console.WriteLine("老板11点打卡");
}
}
class Programer : Staff
{
public override void Clockin()
{
Console.WriteLine("程序员无需打卡");
}
}
#endregion
#region
class Graph
{
public virtual void Perimeter()
{
Console.WriteLine("未初始化图形无法计算");
}
public virtual void Area()
{
Console.WriteLine("未初始化图形无法计算");
}
}
class Rectangle : Graph
{
public int x, y;
public Rectangle(int x, int y)
{
this.x = x;
this.y = y;
}
public override void Perimeter()
{
Console.WriteLine("矩形周长为{0}", (x + y) * 2);
}
public override void Area()
{
Console.WriteLine("矩形面积为{0}", x * y);
}
}
class Square : Graph
{
public int x;
public Square(int x)
{
this.x = x;
}
public override void Perimeter()
{
Console.WriteLine("方的周长为{0}", x * 4);
}
public override void Area()
{
Console.WriteLine("方的面积为{0}", x * x);
}
}
class Round : Graph
{
public double PI = 3.1415926;
int r;
public Round(int r)
{
this.r = r;
}
public override void Perimeter()
{
double perimeter = 2 * PI * r;
Console.WriteLine("圆的周长{0}",perimeter);
}
public override void Area()
{
double areas = PI * r * r;
Console.WriteLine("圆的面积{0}",areas);
}
}
#endregion
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Duck realDuck = new Duck();
realDuck.Speak();
Duck woodDuck = new WoodDuck();
woodDuck.Speak();
Duck rubberDuck = new RubberDuck();
rubberDuck.Speak();
Programer p = new Programer();
p.Clockin();
Boss b = new Boss();
b.Clockin();
Staff s = new Staff();
s.Clockin();
Graph rectangle = new Rectangle(2,3);
rectangle.Perimeter();
rectangle.Area();
Graph square = new Square(2);
square.Perimeter();
square.Area();
Graph round = new Round(3);
round.Perimeter();
round.Area();
}
}
}