定义一个父类Animal eat方法 , 定义两个子类 Dog 特有方法keepHome , Cat 特有方法 catchMouse ;并且 重写eat方法?

测试类中写一个方法,参数列表有一个参数Animla类型,
   要求: 调用该方法分别传递Dog对象 和Cat 对象, 使用instanceof进行将转后调用eat,和他们的特有方法

代码示例:

package cn.zxj.com;

public abstract class Animal {

    public abstract void eat();
}
package cn.zxj.com;

public class Dog extends Animal{

    public void keepHome(){
        System.out.println("keepHome");
    }

    @Override
    public void eat() {
        System.out.println("dog eat gt");
    }
}
package cn.zxj.com;

public class Cat extends Animal{

    public void catchMouse(){
        System.out.println("catchMouse");
    }

    @Override
    public void eat() {
        System.out.println("cat eat fish");
    }
}
package cn.zxj.com;

public class Test {
    public static void eatTest(Animal animal){
        if(animal instanceof Dog){
            Dog dog = (Dog)animal;
            dog.eat();
            dog.keepHome();
        }else if(animal instanceof Cat){
            Cat cat = (Cat)animal;
            cat.eat();
            cat.catchMouse();
        }
    }

    public static void main(String[] args){
        Dog dog = new Dog();
        eatTest(dog);
    }


}
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是中文回复: 首先,我们需要定义一个基类作为父类,可以命名为"Animal",其中包含一个方法"eat": ``` class Animal: def eat(self): print("动物在进食") ``` 接着,我们定义一个继承自"Animal"的子类,可以命名为"Cat",并在其中重写"eat"方法: ``` class Cat(Animal): def eat(self): super().eat() print("猫在吃鱼") ``` 在"Cat"类中,我们使用了"super()"关键字来调用父类的"eat"方法,然后再添加了一行代码来输出"猫在吃鱼"。 最后,我们可以测试一下"Cat"类的功能,如下所示: ``` cat = Cat() cat.eat() ``` 输出结果为: ``` 动物在进食 猫在吃鱼 ``` 可以看到,"Cat"类成功地继承了"Animal"类的方法,并且在重写方法时使用了"super()"关键字来调用父类方法,实现了我们的需求。 ### 回答2: 定义一个基类作为父类,可以将其命名为Animal,该类包含一个名为speak()的方法。在speak()方法中,输出一句话 "动物会发出声音"。 ``` class Animal: def speak(self): print("动物会发出声音") ``` 接下来,我们定义一个继承自Animal子类,可以将其命名为Dog子类重写父类的speak()方法,输出一句话 "狗会汪汪叫",并使用super关键字调用父类的speak()方法。 ``` class Dog(Animal): def speak(self): super().speak() print("狗会汪汪叫") ``` 现在我们可以进行功能测试了。创建一个Dog类的实例,并调用其speak()方法。 ``` dog = Dog() dog.speak() ``` 运行程序后的输出结果为: ``` 动物会发出声音 狗会汪汪叫 ``` 可以看到,子类重写父类方法speak(),并在内部使用super关键字调用父类的speak()方法。这样,子类就继承了父类方法,并添加了自己独特的功能。运行后,我们首先可以看到父类方法调用输出了"动物会发出声音"这句话,接着子类方法输出了"狗会汪汪叫"。这证明了子类重写父类方法并使用super关键字调用父类方法的功能是有效的。 ### 回答3: 基类(父类)是面向对象编程中的概念,它是其他类的基础,其他类通过继承父类来获得父类的属性和方法。继承的子类可以对父类方法进行重写或扩展。 如果要定义一个基类作为父类,并定义一个继承父类子类,并在子类重写父类方法,并使用super关键字调用父类方法,可以按照以下步骤进行: 1. 定义基类(父类): ```python class Parent: def __init__(self, name): self.name = name def say_hello(self): print("Hello, " + self.name + "!") ``` 2. 定义子类并继承父类: ```python class Child(Parent): def __init__(self, name, age): super().__init__(name) # 使用super关键字调用父类的初始化方法 self.age = age def say_hello(self): # 重写父类的say_hello方法 super().say_hello() # 使用super关键字调用父类的say_hello方法 print("I'm " + str(self.age) + " years old!") ``` 3. 测试功能: ```python parent = Parent("Tom") parent.say_hello() # 输出:Hello, Tom! child = Child("Jerry", 5) child.say_hello() # 输出:Hello, Jerry! I'm 5 years old! ``` 在这个例子中,基类Parent定义了一个名为say_hello的方法子类Child继承了父类的属性和方法,并对父类的say_hello方法进行了重写。在重写的say_hello方法中,使用了super关键字调用父类的say_hello方法,并添加了自己的输出语句。测试中,分别创建了一个父类对象和一个子类对象,并调用了它们的say_hello方法,可以看到父类对象输出父类定义的输出语句,子类对象输出子类重写的输出语句和父类输出的语句。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值