英文:
How to print the words using the abstract class?
问题
我想要的结果是...
Baby : crawl
Baby : babble
Dog : bark
Dog : walk on all fours
但上述代码的结果是...
crawl
babble
bark
walk on all fours
我认为 "Baby : " 和 "Dog : " 也在使用抽象类,但我不知道上述代码中的具体代码。
如何修复这段代码?
英文:
The result I want is...
Baby : crawl
Baby : babble
Dog : bark
Dog : walk on all fours
But the result of the above code is...
crawl
babble
bark
walk on all fours
I think "Baby : ", "Dog : " are using the abstract class too, but I don't know what code inside above code.
How can I fix this code?
答案1
得分: 1
需要按照以下方式修改Baby
和Dog
类中的run()
和sound()
方法:
run() {
System.out.println(super.getType() + " : " + "用四肢行走");
}
sound() {
System.out.println(super.getType() + " : " + "吠叫");
}
英文:
Need to modify your run()
and sound()
methods in following way for both Baby and Dog classes:
run() {
System.out.println(super.getType() + " : " + "walk on all fours");
}
sound() {
System.out.println(super.getType() + " : " + "bark");
}
答案2
得分: 0
你只需在所有的打印语句中添加 'type' 即可。
例如:
> System.out.println(super.getType()+":爬行");
你不能在上述语句中使用 this.type
,因为你在两个子类的构造函数中将 type
赋值给了超类变量,而不是子类变量。
英文:
All you have to do is add 'type' in your all print statement
For example:
> System.out.println(super.getType()+": crawl");
You cannot use this.type
in the above statement because you have not assigned type
to the child class variable but to the superclass variable in constructors of both child classes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论