为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我只要加上dog.eat()就不能同时输出 animal类被输出和 dog类被输出?

public class Animal {

public int age=10;

public String color; 

    public void eat() {

System.out.println("动物会吃东西!");

}

    public Animal() {

    System.out.println("Animal类执行了!");

    age=20;

    }


}



public class Dog extends Animal {

public Dog() {

System.out.println("Dog被执行了!");

}

}


public class Initial {

public static void main(String[] args) {

// TODO Auto-generated method stub

  Dog dog=new Dog();

   dog.eat();    

}

}


输出结果是:

Dog被执行了!

动物会吃东西!


正在回答

1 回答

正确的输出结果为:

Animal类执行了!

Dog被执行了!

动物会吃东西!

我怀疑你少复制了第一行

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我只要加上dog.eat()就不能同时输出 animal类被输出和 dog类被输出?

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号