public class Plane
{
public string name;
protected int hp = 50;
public virtual int HP{
get{
return hp;
}
set{
hp = value;
}
}
public virtual void Hurt(){
}
public Plane (string name,int HP)
{
this.name = name;
this.HP = HP;
}
}
//小飞机
public class SmallPlane:Plane {
public override int HP{
get{
return hp;
}
set{
hp = value;
}
}
public override void Hurt(){
HP -= 1;
}
public SmallPlane(string name,int HP):base(name,HP){
}
}
//大飞机
public class BigPlane:Plane {
public override int HP{
get{
return hp;
}
set{
hp = value;
}
}
public override void Hurt(){
HP -= 2;
}
public BigPlane(string name,int HP):base(name,HP){
}
}
//玩家
public class Player{
public void Attack(Plane plane){
if (plane is SmallPlane) {
((SmallPlane)plane).Hurt ();
} else {
((BigPlane)plane).Hurt();
}
Console.WriteLine (plane.name + "受伤,血量为{0}",plane.HP);
}
}
#endregion
#region 写出一个形状的父类,里面包含自定义构造,及一个获取面积和一个获取周长的抽象方法,写出两个子类(矩形、圆形),实现抽象方法和虚方法。
public abstract class Shape
{
public float length;
public float width;
public float diameter;
//抽象方法
public abstract float Area();
public abstract float Circumference();
//虚方法
public virtual void Name(){
Console.WriteLine ("Shape");
}
public Shape (float length, float width)
{
this.length = length;
this.width = width;
}
public Shape (float diameter)
{
this.diameter = diameter;
}
}
//矩形
public class Rectangle:Shape{
public override void Name(){
Console.WriteLine ("Rectangle");
}
public override float Area ()
{
return length * width;
}
public override float Circumference ()
{
return 2 * (length + width);
}
public Rectangle(float length,float width):base(length,width){}
}
//圆
public class Circle:Shape{
// public override void Name(){
// Console.WriteLine ("Circle");
// }
public override float Area ()
{
return diameter * diameter * (float)(Math.PI) / 4;
}
public override float Circumference ()
{
return diameter * (float)(Math.PI);
}
public Circle (float diameter):base(diameter){}
}
#endregion