面向对象类的基础练习

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();
        }
    }
}

  

posted @ 2015-09-24 14:18  WhyToHow  阅读(132)  评论(0编辑  收藏  举报