当前位置:网站首页>Class C design questions

Class C design questions

2022-07-07 23:35:00 Bobo in summer

 Insert picture description here

class Worker// Design staff (Worker) And its subclasses manager class (Manager), The Employee class contains private fields name,salary;
    // And set its properties Name,Salary; The manager class also has its own private member field bonus, And its corresponding attributes Bonus;
    {
    
        string name;

        public string Name
        {
    
            get {
     return name; }
            set {
     name = value; }
        }
        int salary;

        public int Salary
        {
    
            get {
     return salary; }
            set {
     salary = value; }
        }
        public Worker(){
     }// The employee class 、 Managers should have their own non participation 、 Parametric construction method ;
        public Worker(string name, int salary)
        {
    
            this.name = name;
            this.salary = salary;
        } 
        
    }
 class Manager:Worker
    {
    
        int bonus;

        public int Bonus
        {
    
            get {
     return bonus; }
            set {
     bonus = value; }
        }
        public Manager() {
     }
        public Manager(string name, int salary, int bonus)
            : base(name, salary)
        {
    
            this.bonus = bonus;
        }
    }
     // stay main Create an employee array in ( Managers as an element ), And assign values to each element of the array ,
    // It is required to print out the name and salary information of the employee array .
                Worker[] wkLD = new Worker[2];// Create an employee array 
                Worker wkWang = new Worker(" Xiao Wang ", 4000);// To be honest, the employee object is Xiao Wang 
                Manager mnLi = new Manager(" Manager Li ",10000,3000);
                wkLD[0] = wkWang;// Employee Wang is the first element of the array 
                wkLD[1] = mnLi;
                for (int i = 0; i < wkLD.Length; i++) // Cycle out names and salaries 
                {
    
                    Console.WriteLine(" full name :{0}, salary :{1},",wkLD[i].Name,wkLD[i].Salary);
                }

 Insert picture description here
 Insert picture description here

 class Student
        {
    
            string name;
            public string Name
            {
    
                get {
     return name; }
                set {
     name = value; }
            }
            int credit;
            public int Credit
            {
    
                get {
     return credit; }
                set {
     credit = value; }
            }
            public Student() {
     }
            public Student(string name, int credit)
            {
    
                this.name = name;
                this.credit = credit;
            }
        } 
 class Graduate : Student
        {
    
            int postCredit;
           public int PostCredit
            {
    
                get {
     return postCredit; }
                set {
     postCredit = value; }
            }
            public Graduate() {
     }
            public Graduate(string name, int credit, int postCredit) : base(name, credit)
            {
    
                this.postCredit = postCredit;
            }
        }
            Student[] stld = new Student[2];
            Student st1 = new Student(" Xia Bo ",90);
            Graduate st2= new Graduate(" satisfied ",80,50);
            stld[0] = st1;
            stld[1] = st2;
            Console.WriteLine(st2.PostCredit);
            for (int i = 0; i < stld.Length; i++)
            {
    
                Console.WriteLine(" full name {0}, credits {1}",stld[i].Name,stld[i].Credit);
            }

 Insert picture description here
 Insert picture description here

 class Vehicles // Define a name Vehicles The basic class of vehicles :
        {
    
            string brand, color;

            public string Color
            {
    
                get {
     return color; }
                set {
     color = value; }
            }

            public string Brand
            {
    
                get {
     return brand; }
                set {
     brand = value; }
            }
            public Vehicles() {
     }// Complete the parameterless and parameterized construction method of the base class , Parameter free constructor , If a class has a residual constructor defined , Then the system does not provide a parameterless constructor , Need to be manually added 
            public Vehicles(string brand, string color)
            {
    
                this.brand = brand;
                this.color = color;
            } //  This class contains the private member field trademark and color , And set its corresponding public attributes ;


            public virtual void run()
            {
    
                Console.WriteLine(" I've started .");
            } //  Class contains member methods Run To simulate the start of transportation , This method only outputs “ I've started ” Information ;
            public void showInfo()
            {
    
                Console.WriteLine(" trademark :{0}, Color :{1}", this.brand, this.color);
            } //  Class contains member methods ShowInfo To display information , This method outputs the display trademark and color ;
        }
 class Car : Vehicles //  To write Car Cars are inherited from Vehicles class , For such :
        {
    
            int seat;
            public int Seat
            {
    
                get {
     return seat; }
                set {
     seat = value; }
            }//  Add member field seats , And set its corresponding public attributes ;
            public Car() {
     }    //  Complete the nonparametric parametric constructor of car class 
            public Car(string brand, string color, int seat)
                : base(brand, color)
            {
    
                this.seat = seat;

            }
            public void showCar()
            {
    
                Console.WriteLine(" Car brand :{0}, Color :{1}, Number of seats :{2}", this.Brand, this.Color, this.seat);
            }   //  Add member method ShowCar, The output shows the information of the car ;
            public override void run()
            {
    
                Console.WriteLine(" The car started ");
            }        //  Covering the parent class Run Method , The output shows “ Information that the car has started ”;
        }
static void Main(string[] args)
        {
    
            Vehicles v1 = new Vehicles(" ferrari ", " white ");
            Car c1 = new Car(" Koniseg ", " violet ", 5);
            c1.showCar();
            v1.showInfo();
            c1.run();
            v1.run();
            Console.ReadKey();
        }

 Insert picture description here
 Insert picture description here

 class Vehicles 
        {
    
            string brand, color;

            public string Color
            {
    
                get {
     return color; }
                set {
     color = value; }
            }

            public string Brand
            {
    
                get {
     return brand; }
                set {
     brand = value; }
            }
            public Vehicles() {
     }
            public Vehicles(string brand, string color)
            {
    
                this.brand = brand;
                this.color = color;
            } 


            public virtual void run()
            {
    
                Console.WriteLine(" I've started .");
            } 
            public void showInfo()
            {
    
                Console.WriteLine(" trademark :{0}, Color :{1}", this.brand, this.color);
            }
        }
 class Truck : Vehicles 
        {
    
            int load;
            public int Load
            {
    
                get {
     return load; }
                set {
     load = value; }
            }
            public Truck() {
     }    
            public Truck(string brand, string color, int load)
                : base(brand, color)
            {
    
                this.load = load;

            }
            public void showTrunk()
            {
    
                Console.WriteLine(" Car brand :{0}, Color :{1}, load :{2}", this.Brand, this.Color, this.load);
            }   
            public override void run()
            {
    
                Console.WriteLine(" The car started ");
            }      
        }
            Vehicles v1 = new Vehicles(" The public ", " white ");
            Truck c1 = new Truck(" zhongtai ", " black ", 5000);
            c1.showTrunk();
            v1.showInfo();
            c1.run();
            v1.run();
            Console.ReadKey();

 Insert picture description here
 Insert picture description here

 interface IVehicle// Create a name IVehicle The interface of : Add two methods to the interface Start() and Stop() Used to describe the start and stop of the vehicle .
    {
    
        void start();
        void stop();
    }
class Bike:IVehicle// establish Bike Bicycles :
    {
    
        int wheel;// This class contains private member fields wheel Number of wheels , And set its corresponding public attributes ;
        public int Wheel
        {
    
            get {
     return wheel; }
            set {
     wheel = value; }
        }
        public Bike() {
     }// Complete the parameterless and parametric construction method of this class ;
        public Bike(int wheel)
        {
    
            this.wheel = wheel;
        }
        public void start()// Realization IVehicle Two methods of interface ;
        {
    
            Console.WriteLine(" I am a {0} A bicycle , I'm going to start ",this.wheel);
        }
        public void stop()
        {
    
            Console.WriteLine(" Bicycle station , brake ");
        }
    }
 class Bus:IVehicle// establish Bus Bus class :
    {
    
        int seat;// This class contains private member fields seat Number of seats , And set its corresponding public attributes ;
      public int Seat
    {
    
      get {
     return seat; }
      set {
     seat = value; }  
     }
        public Bus() {
     }// Complete the parameterless and parametric construction method of this class ;
        public Bus(int seat)
        {
    
            this.seat = seat;
        }
        public void start() // Realization IVehicle Two methods of interface ;
        {
    
            Console.WriteLine(" I am a {0} Seat car , I'm going to start ", this.seat);
        }
        public void stop()
        {
    
            Console.WriteLine(" The bus arrives at the station , brake ");
        }
    }
                IVehicle[] iv = new IVehicle[2];  // stay main The method determines IVehicle Array , And store Bike,Bus Object to test the above 
                Bike bkBlack = new Bike(2);
                Bus busWhilt = new Bus(24);
                iv[0] = bkBlack;
                iv[1] = busWhilt;
                iv[0].start();
                iv[0].stop();
                iv[1].start();
                iv[1].stop();

 Insert picture description here
 Insert picture description here

  class Pet //Pet Define a pet class (Pet):
       {
    
        string name; // Define private member fields in this class name Name and age Age , And set its corresponding public attributes ;
        public string Name
        {
    
            get {
     return name; }
            set {
     name = value; }
        }
        int age;
        public int Age
        {
    
            get {
     return age; }
            set {
     age = value; }
        }
        public virtual void cry()// This class includes two methods : It's called Cry(), Eat something Eat();
        {
    
            Console.WriteLine(" I'm a pet , My name is {0}, I {1} Year old ", this.name, this.age);
        }
        public virtual void eat()
        {
    
            Console.WriteLine(" I'm a favorite pet ");
        }
        public Pet() {
     } // Complete the parameterless and parametric construction method of this class ;
        public Pet(string name, int age)
        {
    
            this.name = name;
            this.age = age;
        }
    }
  class Dog : Pet// Define the subclass of a pet dog (Dog):
    {
    
        public override void cry() // Covering the parent class Cry(),Eat() Method ; Add ways to watch the door GuardEntrance()
        {
    
            Console.WriteLine(" I'm a dog , My name is {0}, I {1} Year old , My name is Wangwang ", this.Name, this.Age);
        }
        public override void eat()
        {
    
            Console.WriteLine(" I like eating bones ");
        }
        public void GuardEntrance()
        {
    
            Console.WriteLine(" I watch the door for my master ");
        }
        public Dog() {
     } // Complete the parameterless and parametric construction method of this class ;
        public Dog(string name, int age) : base(name, age) {
     }
    }
 class Cat : Pet // Define the subclass of pet cats (Cat):
    {
    
        public override void cry() // Covering the parent class Cry(),Eat() Method ;
        {
    
            Console.WriteLine(" I'm a cat , My name is {0}, I {1} Year old , My name is Miaomiao ", this.Name, this.Age);
        }
        public override void eat()
        {
    
            Console.WriteLine(" I like fish ");
        }
         public void HuntMice()// Increase the cat's own unique method of catching mice HuntMice();
        {
    
            Console.WriteLine(" I like catching mice ");
        }
        public Cat() {
     }// Complete the parameterless and parameterized construction of this class 
        public Cat(string name, int age) : base(name, age) {
     }
    }
        Pet pet1 = new Dog(" Xiao Huang ", 3);
        pet1.cry();
        pet1.eat();
        ((Dog)pet1).GuardEntrance();

        Pet pet2 = new Cat(" The small white ", 2);
        pet2.cry();
        pet2.eat();
        ((Cat)pet2).HuntMice();
原网站

版权声明
本文为[Bobo in summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130558211901.html