面向对象类的基础练习

namespace HomeWork
{
    class Firearms
    {
        private string type;//成员变量 品牌
        
        //属性 品牌
        public string Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }
      
        //属性 重量 没有对应的成员变量
        public string Weight
        {
            get;
            set;
        }

        private static string nature = "Kill People!";
        //静态属性 枪械的本质 属于所有枪械的属性 所有实例属性不可更改 
        public static string Nature
        {
            get
            {
                return nature;
            }

         }
        //初始子弹数
        private int bulletNumber = 0;
        public int BulletNumber
        {
            get
            {
                return bulletNumber;
            }
            set
            {
                if (value < FullBulletNumber)
                    bulletNumber = value;
                else
                {
                    Console.WriteLine("初始子弹数量超弹夹容量;弹夹容量已满");
                    bulletNumber = FullBulletNumber;
                }
            }
        }

        //弹夹容量
        private int fullBulletNumber = 30;
        public int FullBulletNumber
        {
            get
            {
                return fullBulletNumber;
            }
            set
            {
                fullBulletNumber = value;
            }
        }

       

       

        /// <summary>
        /// 计算装弹后弹夹内子弹数目
        /// </summary>
        /// <param name="val">装弹数目</param>
        public void Load(int val)
        {
            int number = BulletNumber + val;
            
            if (number < FullBulletNumber)
                Console.WriteLine("此枪械弹夹容量为{0},您装入{1}发子弹,现在弹夹内子弹数为:{2}",FullBulletNumber, val, number);
            else
                Console.WriteLine("您装入{0}发子弹,此枪械弹夹容量已满为:{1}", val, FullBulletNumber);
        }
    }
}

  

namespace HomeWork
{
    class Gun : Firearms
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Gun()
        {

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Type">枪械型号</param>
        /// <param name="FullBulletNumber">弹夹容量</param>
        /// <param name="BulletNumber">初始弹夹子弹数</param>
        public Gun(string Type, int FullBulletNumber, int BulletNumber)
        {
            this.Type = Type;
            this.FullBulletNumber = FullBulletNumber;
            this.BulletNumber = BulletNumber;
        }


    }
}

  

namespace HomeWork
{
    class Program
    {
        static void Main(string[] args)
        {
            Gun gun1 = new Gun();
            gun1.FullBulletNumber = 24;
            gun1.BulletNumber = 10;
            gun1.Load(13);
            Console.WriteLine(Firearms.Nature);

            Gun gun2 = new Gun("M1911", 7, 8);
            gun2.Load(3);
            Console.WriteLine("手。枪的本质是" + Gun.Nature);
            Console.ReadLine();
        }
    }
}

  

转载于:https://www.cnblogs.com/whytohow/p/4835209.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值