.Net学习笔记----2015-07-22(C#基础复习10,面向对象2个小练习)

        static void Main(string[] args)
        {
            #region 定义父类Father(姓lastName, 财产property,血型bloodType)
            //儿子Son类(玩游戏PlayGame方法),女儿Daughter类(跳舞Dance方法),
            //调用父类构造函数(:base())给子类字段赋值

            //Son son = new Son("张三", 10m, "AB", "");
            //son.SayHello();
            //son.PlayGame();
            //Daughter dh = new Daughter("张美美", 100m, "O");
            //dh.SayHello();
            //dh.Dance();
            #endregion

            #region 定义汽车类Vehicle属性(brand(品牌),color(颜色)),方法run
            //子类卡车(Truck)属性weight载重,方法拉货,轿车(Car)属性passenger载客数量,方法载客

            Truck t = new Truck("东风","绿",10);
            //t.Brand = "东风";
            //t.Color = "绿";
            //t.Weight = 10;
            t.SayHello();
            t.LaHuo();
            t.Run();

            Car c = new Car("奔驰","",5);
            //c.Brand = "奔驰";
            //c.Color = "黑";
            //c.Passenger = 5;
            c.SayHello();
            c.ZaiKe();
            c.Run();
            
            #endregion
            Console.ReadKey();
        }

第一个练习:
    class Father
    {
        public string LastName { get; set; }
        public decimal Property { get; set; }
        public string BloodType { get; set; }

        public Father(string lastName, decimal property, string bloodType)
        {
            this.LastName = lastName;
            this.Property = property;
            this.BloodType = bloodType;
        }

        public void SayHello()
        {
            Console.WriteLine("我叫{0},我有{1}元,我是{2}型血", this.LastName, this.Property, this.BloodType);
        }
    }
 
 
    class Son : Father
    {
        public string Name { get; set; }
        public Son(string lastName, decimal property, string bloodType, string name)
            : base(lastName, property, bloodType)
        {
            this.Name = name;
        }

        public void PlayGame()
        {
            Console.WriteLine("儿子爱玩游戏");
        }
    }
    class Daughter : Father
    {
        public Daughter(string lastName, decimal property, string bloodType)
            : base(lastName, property, bloodType)
        {

        }

        public void Dance()
        {
            Console.WriteLine("女儿爱跳舞");
        }
    }

 

 
第二个练习:
/// <summary> /// 汽车的父类 /// </summary> class Vehicle { private string _brand; public string Brand { get { return _brand; } set { _brand = value; } } private string _color; public string Color { get { return _color; } set { _color = value; } } public Vehicle(string brand, string color) { this.Brand = brand; this.Color = color; } public void Run() { Console.WriteLine("汽车都会跑"); } //public void SayHello() //{ // //Console.WriteLine("我是{0}车,我是{1}色的", this.Brand, this.Color); //} public virtual void SayHello() { } }
    class Truck : Vehicle
    {
        private int _weight;
        public int Weight
        {
            get { return _weight; }
            set { _weight = value; }
        }
        public Truck(string brand, string color, int weight)
            : base(brand, color)
        {
            this.Weight = weight;
        }
        public override void SayHello()
        {
            Console.WriteLine("我是{0}牌{1}色的大卡车,载重{2}吨", this.Brand, this.Color, this.Weight);
        }
        public void LaHuo()
        {
            Console.WriteLine("{0}牌大卡车能拉很多货哦~", this.Brand);
        }
    }
    class Car : Vehicle
    {
        private int _passenger;

        public int Passenger
        {
            get { return _passenger; }
            set { _passenger = value; }
        }
        public Car(string brand, string color, int passenger)
            : base(brand, color)
        {
            this.Passenger = passenger;
        }
        public override void SayHello()
        {
            Console.WriteLine("我是{0}牌{1}色的轿车,载客数量是{2}人", this.Brand, this.Color, this.Passenger);
        }
        public void ZaiKe()
        {
            Console.WriteLine("{0}牌小轿车坐着很舒服,客人都爱坐", this.Brand);
        }
    }

 

你可能感兴趣的:(.net)