添加Lesson22
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson22_面向对象相关_万物之父中的方法练习</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,80 @@
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Lesson22_面向对象相关_万物之父中的方法练习
|
||||
{
|
||||
class Player
|
||||
{
|
||||
private string name;
|
||||
private int hp, atk, def, dodge;
|
||||
|
||||
public Player(string name, int hp, int atk, int def, int dodge)
|
||||
{
|
||||
this.name = name;
|
||||
this.hp = hp;
|
||||
this.atk = atk;
|
||||
this.def = def;
|
||||
this.dodge = dodge;
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("玩家{0},血量{1},攻击力{2},防御力{3},闪避{4}", name, hp, atk, def, dodge);
|
||||
}
|
||||
}
|
||||
class Monster
|
||||
{
|
||||
//private int atk, def, hp, skill;//不用成员变量
|
||||
public Monster(int atk, int def, int hp, int skill)
|
||||
{
|
||||
Atk = atk;
|
||||
Def = def;
|
||||
Hp = hp;
|
||||
Skill = skill;
|
||||
}
|
||||
public int Atk//用属性
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public int Def
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public int Hp
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public int Skill
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public Monster Clone()
|
||||
{
|
||||
return MemberwiseClone() as Monster;
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("ATK{0},DEF{1},HP{2},SKILL{3}",Atk,Def,Hp,Skill);
|
||||
}
|
||||
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Player p = new Player("张三",100,20,10,5);
|
||||
Console.WriteLine(p);
|
||||
Monster m = new Monster(90,50,100,1);
|
||||
Monster m2 = m.Clone();
|
||||
Console.WriteLine(m);
|
||||
m2.Atk = 200;
|
||||
m2.Skill = 20;
|
||||
Console.WriteLine(m);
|
||||
Console.WriteLine(m2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user