添加Lesson22

This commit is contained in:
2025-09-17 09:49:32 +08:00
parent 4cf9bb6c97
commit 839ff77b59
5 changed files with 231 additions and 0 deletions
@@ -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);
}
}
}