添加Lesson18

This commit is contained in:
2025-09-14 15:08:38 +08:00
parent d6767da6da
commit c16355e935
5 changed files with 222 additions and 0 deletions
+12
View File
@@ -35,6 +35,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson17_多态-vob", "Less
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson17_多态-vob练习", "Lesson17_多态-vob练习\Lesson17_多态-vob练习.csproj", "{B9598C23-6AD4-44FB-B16B-6A0A80B8D9CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多态-抽象类和抽象方法", "Lesson18_多态-抽象类和抽象方法\Lesson18_多态-抽象类和抽象方法.csproj", "{4EF4F902-1A00-4518-B21C-E7B6264CAB03}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson18_多态-抽象类和抽象方法练习", "Lesson18_多态-抽象类和抽象方法练习\Lesson18_多态-抽象类和抽象方法练习.csproj", "{7004726B-5B33-4B2A-ADDB-EF22DA593630}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -105,6 +109,14 @@ Global
{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
{4EF4F902-1A00-4518-B21C-E7B6264CAB03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4EF4F902-1A00-4518-B21C-E7B6264CAB03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EF4F902-1A00-4518-B21C-E7B6264CAB03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EF4F902-1A00-4518-B21C-E7B6264CAB03}.Release|Any CPU.Build.0 = Release|Any CPU
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7004726B-5B33-4B2A-ADDB-EF22DA593630}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson18_多态_抽象类和抽象方法</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,77 @@
namespace Lesson18_多态_抽象类和抽象方法
{
#region
//概念
//被抽象关键字abstract修饰的类
//特点:
//1.不能被实例化的类
//2.可以包含抽象方法
//3.继承抽象类必须重写其抽象方法
abstract class Thing
{
//抽象类中封装的知识点都可以用
public string name;
//可以在抽象类中写抽象函数
}
class Water : Thing
{
}
#endregion
#region
//又叫 纯 虚方法
//用abstract关键字修饰的方法
//特点:
//1.只能在抽象类中声明
//2.没有方法体
//3.不能是私有的
//4.继承后必须实现 用override重写
abstract class Fruit
{
public string name;
public abstract void Bad();//抽象方法 是一定不能有函数体的(大括号什么的)并且在子类中必须重写
public virtual void Test()//虚方法可以不重写
{
}
}
class Apple : Fruit
{
public override void Bad()
{
Console.WriteLine();
}
}
class SuperApple : Apple
{
public override void Bad()
{
base.Bad();
}
public override void Test()
{
base.Test();
}
}
#endregion
internal class Program
{
static void Main(string[] args)
{
//抽象类不能被实例化
//Thing t = new Thing();
//遵循里氏替换原则
Thing water = new Water();
}
}
//总结:
//如何选择普通类和抽象类?
//不希望被实例化的对象,比较笼统的对象
//例如:水果,人,事情
//或 父类中的行为不太需要被实现的,只希望子类区定义具体的规则的
//可以选择抽象类
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson18_多态_抽象类和抽象方法练习</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,111 @@
using System.Drawing;
namespace Lesson18_多态_抽象类和抽象方法练习
{
abstract class Animal
{
public abstract void Shout();
}
class Human : Animal
{
public override void Shout()
{
Console.WriteLine("人叫");
}
}
class Dog : Animal
{
public override void Shout()
{
Console.WriteLine("狗叫");
}
}
class Cat : Animal
{
public override void Shout()
{
Console.WriteLine("猫叫");
}
}
abstract class Graph
{
public abstract void Perimeter();
public abstract void Area();
}
class RectAngle : Graph
{
private int x, y;
public RectAngle(int x,int y)
{
this.x = x;
this.y = y;
}
public override void Area()
{
Console.WriteLine("面积为:"+x*y);
}
public override void Perimeter()
{
Console.WriteLine("周长为"+(x+y)*2);
}
}
class Square : Graph
{
private int x;
public Square(int x)
{
this.x = x;
}
public override void Area()
{
Console.WriteLine("面积为:" + x * x);
}
public override void Perimeter()
{
Console.WriteLine("周长为" + x * 4);
}
}
class Round : Graph
{
private 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);
}
}
internal class Program
{
static void Main(string[] args)
{
Animal human = new Human();
human.Shout();
Animal dog = new Dog();
dog.Shout();
Animal cat = new Cat();
cat.Shout();
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();
}
}
}