英文:
Need of method with body in abstract class?
问题
抽象类内部包含方法体或实现的需要是什么?它是否需要由扩展类实现。基本上,抽象类中的方法体的主要用途是什么?就像这段代码一样。(对Java不熟悉)
public abstract class Animal {
protected int age;
public void eat() {
System.out.println("Animal is eating");
}
public abstract String getName();
}
class Swan extends Animal {
public String getName() {
return "Swan";
}
}
我尝试实现这个方法,但不起作用,那么抽象类中方法体的需要是什么?
英文:
What is the need for a method which has a body or implementation inside the abstract class ?, does it need to be implemented by an extended class. Basically, what is the main use of the method body in abstract class? Like this code. (New to Java)
public abstract class Animal {
protected int age;
public void eat() {
System.out.println("Animal is eating");
}
public abstract String getName();
}
class Swan extends Animal {
public String getName() {
return "Swan";
}
I am tried implementing the method but doesn't work then what's the need of method body in abstract class?
答案1
得分: 2
破解你的示例
public void eat() {
System.out.println("动物正在进食");
}
实现了“普通”方法,可以被扩展类重写
public abstract String getName();
抽象方法 - 没有提供具体实现。这个方法必须在扩展类中被实现(有点像重写)
原因是你可能在基类中有一个问题的通用实现,但需要一些细节必须由扩展类提供。你可以在你的抽象类中调用抽象方法,这在你的示例中没有展示,例如:
public abstract String getName();
public void eat(){
System.out.print(this.getName() + "正在进食");
}
实现类将被编译器强制提供getName()
的实现。
英文:
Breaking up your example
> public void eat() {
> System.out.println("Animal is eating");
> }
implemented "normal" method, can be overriden by extending class
> public abstract String getName();
abstract method - no body provided. This method MUST BE implemented (kind of overriden) in extending class
The reson for this is that you may have an general implementation of a problem in a base class that requires some details that must be provided by exending class. You can call abstract method in your abstract class which is not presented in your examplie, eg
public abstract String getName();
public void eat(){
System.out.print(this.getName() + " is eating")
}
and the implementing class will be forced by the compiler to provide getName()
implementation.
答案2
得分: 0
一个抽象类可以同时拥有抽象方法(没有具体实现)和具体方法(有具体实现)。抽象类中的具体方法提供了可以在所有子类之间共享的默认行为。
Animal类中的eat()
是一个具体方法。Animal的所有子类都将拥有这个方法,并且可以在不需要自己实现它的情况下使用它。
现在,如果一个特定的子类(比如章鱼)需要以不同的方式进食,它可以覆盖eat()
方法以提供自己的行为。如果不覆盖它,将使用Animal类中的默认行为。
getName()
是一个抽象方法,必须由扩展Animal的任何类来实现。
英文:
An Abstract Class can have both abstract methods (having no bodies) and concrete methods (with bodies). The concrete methods in the abstract class provide default behavior that can be shared across all subclasses.
The eat()
in Animal class is a concrete method. All subclasses of Animal will have this method and can use it without needing to have their own implementation of it.
Now, if a specific subclass (like Octopus) needs to eat in another way, it can override eat()
method to provide its own behavior. If it doesn't override it, the default behavior from the Animal class is used.
The getName()
is an abstract method and must be implemented by any class that extends Animal.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论