C#笔记,十——类的应用——打僵尸作业

课后题:

第一题 定义一个打印机类(printer)。需求:

1. 采用单例模式

2. 定义成员变量:打印机IP地址(静态变量),打印数量,纸张类型(数值为枚举类型)

3. 定义方法:根据打印机IP地址连接打印机;打印功能;打印失败提醒功能

第二题 模拟打僵尸。需求:

定义僵尸类:

公共成员变量:类型、总血量、每次失血量

方法:初始化方法(设置僵尸种类,总血量)、被打击失血(抽象方法)、死亡(抽象方法)

定义普通僵尸类继承于僵尸类

定义有防具(路障)僵尸类继承于僵尸类:

特有成员变量:防具类型

特有方法:防具被打烂

定义铁桶僵尸类继承于有防具僵尸:

特有成员变量:弱点

特有方法:防具被磁铁吸走

1、创建普通僵尸对象,设置总血量50,每次失血量为 3

2、创建路障僵尸对象,设置总血量80,有路障时,每次失血量为 2

3、创建铁桶僵尸对象,设置总血量120,有铁桶时,每次失血量为 1

4、选作:

在Main方法里面里用循环语句模拟攻击:

三个僵尸对象同时被攻击:

(1)、普通僵尸被打击时:每次失血3.

(2)、路障僵尸被打击时:有路障时,每次失血2,血量剩余一半时,防具被打拦,之后每次失血3.

(3)、铁桶僵尸被打击时:有铁桶时,每次失血1,血量剩余1/3时,铁桶被打拦,之后每次失血3.

循环攻击过程中:每个僵尸被攻击时,输出本次丢失血量,剩余血量。失去道具时,输出丢失的道具。僵尸死亡时,输出已死亡。

最后一个僵尸死亡时,攻击停止,循环结束。输出总攻击次数。

/*------------------------------------------------------*/

打僵尸

using System;

namespace Zombie

{

//创建僵尸的基类,把共有的字段和方法写在基类里面

//子类就可以直接调用或者重写方法

class Zombie

{

//血量

public int HP { get; set; }

//失血量

public int LP { get; set; }

//虚方法,由子类重写,如果子类不重写,则调用父类

//里面的方法,如普通僵尸就是调用的父类里面的方法

public virtual void BeAttacked()

{

//打印血量的值

Console.WriteLine("僵尸的血量hp:" + HP);

//血量减去失血量

HP -= LP;

//如果血量小于或者等于0

if (HP <= 0)

{

HP = 0;

//调用死亡的方法

Die();

}

}

//虚方法,当僵尸的血量小于或者等于0时调用该方法

public virtual void Die()

{

Console.WriteLine("僵尸死亡" );

}

}

//普通僵尸继承僵尸的基类,所有的方法都是调用的基类的

class Normal : Zombie

{

//给普通僵尸字段初始化

public Normal(int hp, int lp)

: base()

{

this.HP = hp;

this.LP = lp;

}

}

//定义路障僵尸类继承于僵尸类

class RoadBlock : Zombie

{

public RoadBlock()

{

}

//初始化赋值,用带参数的构造方法

public RoadBlock(int hp, int lp, bool haveRoad)

: base()

{

this.HP = hp;

this.LP = lp;

this.haveRoadBlock = haveRoad;

}

//该字段是受保护的,只能在子类和父类内部使用

//字段的含义是是否有路障,初始值为true

protected bool haveRoadBlock;

//重写被攻击的方法

public override void BeAttacked()

{

Console.WriteLine("路障僵尸血量:" + this.HP);

//血量减去失血量

this.HP -= this .LP;

//当血量小于或者等于总血量的一半时

if (this .HP <= 40)

{

//路障丢失,调用路障丢失的方法

DefenceLost();

}

//如果没有路障,则把失血量改为3

if (haveRoadBlock == false )

{

this.LP = 3;

}

//如果血量小于0,调用死亡的方法

if (this .HP <= 0)

{

this.HP = 0;

Die();

}

}

//虚方法,子类进行重写,丢失路障的方法

public virtual void DefenceLost()

{

haveRoadBlock = false;

Console.WriteLine("丢失路障" );

}

//重写了父类里面的虚方法

public override void Die()

{

Console.WriteLine("路障僵尸死亡" );

}

}

//铁桶僵尸继承路障僵尸

class Bucket : RoadBlock

{

//初始化

public Bucket(int hp, int lp, bool haveBucket)

: base()

{

this.HP = hp;

this.LP = lp;

this.haveRoadBlock = haveBucket;

}

//重写了受到攻击的方法

public override void BeAttacked()

{

Console.WriteLine("铁桶僵尸的血量:" + this.HP);

this.HP -= this .LP;

if (this .HP <= 40)

{

DefenceLost();

}

if (haveRoadBlock == false )

{

this.LP = 3;

}

if (this .HP <= 0)

{

this.HP = 0;

Die();

}

}

public override void Die()

{

Console.WriteLine("铁桶僵尸死亡" );

}

}

class MainClass

{

public static void Main( string[] args)

{

Normal normal = new Normal(50, 3);

RoadBlock road = new RoadBlock(80, 2, true);

Bucket bucket = new Bucket(120, 1, true);

int num = 1;

do

{

Console.WriteLine("第{0}回合" , num);

num++;

normal.BeAttacked();

road.BeAttacked();

bucket.BeAttacked();

//如果三种僵尸的血量都小于0,跳出循环

if (normal.HP <= 0 && road.HP <= 0 && bucket.HP <= 0)

{

break;

}

} while (true );

Console.ReadKey();

}

}

}