如何使用抽象类打印单词?

huangapple go评论64阅读模式
英文:

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

需要按照以下方式修改BabyDog类中的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.

huangapple
  • 本文由 发表于 2020年10月10日 14:48:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64290796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定