Files
2025-09-30 16:49:43 +08:00

68 lines
1.6 KiB
C#

namespace Lesson9_封装_拓展方法练习
{
static class Tools
{
public static int IntSqrt(this int i)
{
return i*i;
}
public static void Suicide(this player p)
{
Console.WriteLine("{0}自杀了",p.name);
}
}
class player
{
public string name;
public int hp, atk, def;
public player()
{
name = "null";
hp = 0;
atk = 0;
def = 0;
}
public player(string name,int hp,int atk,int def)
{
this.name =name;
this.hp = hp;
this.atk = atk;
this.def = def;
}
public void Attack()
{
Console.WriteLine("{0}攻击了,攻击值{1}",name,atk);
}
public void Defense()
{
Console.WriteLine("{0}防御了,防御值{1}",name,def);
}
public void Move()
{
Console.WriteLine("{0}移动了",name);
}
public void GetHurt()
{
Console.WriteLine("{0}受伤了,剩余{1}hp",name,hp);
}
}
internal class Program
{
static void Main(string[] args)
{
player p = new player();
p.Attack();
p.Defense();
p.Move();
p.GetHurt();
p.Suicide();
player p2 = new player("HK",100,100,100);
p2.Attack();
p2.Defense();
p2.Move();
p2.GetHurt();
p2.Suicide();
}
}
}