C# 面向对象练习1

 

using System;

namespace OOP01
{
    public class Monster
    {
        // 创建私有属性
        private string mName;
        private int mLevel;
        private int mHp;
        private int mAttack;
        private int mDefence;
        
        // 创建写入和读取方法
        public string Name
        {
            set { this.mName = value; }
            get { return this.mName; }
        }
        
        public int Level
        {
            set { this.mLevel = value;}
            get
            {
                if (this.mLevel<1)
                {
                    return 1;
                }

                if (this.mLevel>100)
                {
                    return 100;
                }
                return this.mLevel;
            }
        }

        public int Hp
        {
            set
            {
                if (value <= 0)
                {
                    this.mHp = 0;
                    Console.WriteLine("{0}的血量为0,已死亡!",this.Name);
                    return;
                }
                this.mHp = value;
            }
            get { return this.mHp; }
        }

        public int Attack
        {
            set
            {
                this.mAttack = value;
            }
            get
            {
                return this.mAttack;
            }
        }

        public int Defence
        {
            set { this.mDefence = value; }
            get
            {
                if (this.mDefence<0)
                { return 0; }
                return this.mDefence;
            }
        }
        // 创建行为方法
        public void AttackAction()
        {
            Console.WriteLine("我是怪物{0},等级{1},攻击力为{2}",this.Name,this.Level,this.Attack);
        }
        // 创建行为方法
        public void Damage(int val)
        {
            this.Hp = this.Hp - val + this.Defence;
            if (this.Hp > 0)
                Console.WriteLine("{0}说:\"我受伤了,血量为{1},救我!\"",this.Name,this.Hp);
        }
        // 无参构造函数
        public Monster(){}
        // 有参构造函数
        public Monster(string name, int level,int hp, int attack, int defence)
        {
            this.mName = name;
            this.mLevel = level;
            this.mHp = hp;
            this.mAttack = attack;
            this.mDefence = defence;
        }
    }
}

调用类 

using System;

namespace OOP01
{
    class Program
    {
        static void Main(string[] args)
        {
            Monster godzilla1 = new Monster();
            Monster godzilla2 = new Monster("哥斯拉",90,100,30,50);
            
            godzilla2.AttackAction();
            godzilla2.Damage(90);
            godzilla2.Damage(90);
            Console.WriteLine("Finish!");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值