当前位置:网站首页>C cat and dog

C cat and dog

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

 Insert picture description here

 class //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 ",this.Name,this.Age);
        }
        public override void eat()
        {
    
            Console.WriteLine(" I like eating ");
        }
        public void GuardEntrance()
        {
    
            Console.WriteLine(" I like to watch the door , ha-ha ");
        }
        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 ",this.Name,this.Age);
        }
        public override void eat()
        {
    
            Console.WriteLine(" I like it ");
        }


        public void GuardEntrance()// Increase the cat's own unique method of catching mice HuntMice();
        {
    
            Console.WriteLine(" I like reading ,");
        }
        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).GuardEntrance();
原网站

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