class Monster
{
public string name;
public int hp;
public int atk;
public Monster(string name, int atk, int hp)
{
this.hp = hp;
this.name = name;
this.atk = atk;
}
/// <summary>
/// 怪的攻击方法
/// 攻击outman
/// </summary>
public virtual void Attack(OutMan outMan)
{
//:调用OutMan的受伤方法
outMan.Damager(atk);
Console.WriteLine("{0}对{1},发起攻击,{2}血量为:{3}", name, outMan.name, outMan.name, outMan.hp);
}
/// <summary>
/// 默认受伤方法
/// </summary>
/// <param name="atk"></param>
public virtual void Damager(int atk)
{
hp -= atk;
Console.WriteLine("{0}受伤了,血量为:{1}", name, hp);
}
}
/// <summary>
/// 哥布林类攻击方法就是父类中的默认攻击方法就够了,那么久不需要重写
/// </summary>
class Goblin : Monster
{
public int def;
public Goblin(string name, int atk, int hp, int def) : base(name, atk, hp)
{
this.def = def;
}
public override void Damager(int atk)
{
if (atk - def > 0)
{
hp = hp - (atk - def);
Console.WriteLine("{0}受伤了,血量为:{1}", name, hp);
}
else
{
Console.WriteLine("怪{0}闪避", name);
}
}
}
/// <summary>
/// 哥斯拉这个类攻击方法和父类不一样:有概率翻倍
/// 但是没有防御力,受伤方法可以直接用父类中的
/// </summary>
class Gesla : Monster
{
Random r;
public Gesla(string name, int atk
C#面向对象案例之奥特曼打小怪兽_02(奥特曼可挂载装备)
本文通过C#面向对象编程,详细介绍了如何构建一个奥特曼角色,该角色能够挂载不同装备来提升能力,以此为例阐述了面向对象设计的基本思想和实现方法。
摘要由CSDN通过智能技术生成