JACK_C#_虚方法和抽象方法

#region 模拟微信打飞机里面敌机受到攻击时受到伤害的情景
    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值