添加Lesson17
This commit is contained in:
@@ -31,6 +31,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类",
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类练习", "Lesson16_继承-密封类练习\Lesson16_继承-密封类练习.csproj", "{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类练习", "Lesson16_继承-密封类练习\Lesson16_继承-密封类练习.csproj", "{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}"
|
||||||
EndProject
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -55,7 +55,8 @@
|
|||||||
//父类的父类的构造->...父类构造->...->子类构造
|
//父类的父类的构造->...父类构造->...->子类构造
|
||||||
class GameObject
|
class GameObject
|
||||||
{
|
{
|
||||||
public GameObject() {
|
public GameObject()
|
||||||
|
{
|
||||||
Console.WriteLine("gameobject的构造函数");
|
Console.WriteLine("gameobject的构造函数");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user