Java抽象类和接口方法的实现
如果您有一个具有 3 个方法(x()、y() 和 z())的接口(Position)并且还有一个抽象类,我们将其称为 Shape。
Shape 实现了 Position,只给 x() 和 y() 提供代码。编译器是否隐式猜测 z() 是一个抽象方法?
If you have an interface (Position) with 3 methods (x(), y() and z()) and also have an abstract class, lets call it Shape.
Shape implements Position and only gives code to x() and y(). Does the compiler implicitly guess that z() is an abstract method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。只要 Shape 是抽象的,就不需要实现 Position 的所有方法。这是任何具体类别所需要的。
Yes. So long as Shape is abstract it is not required to implement all methods of Position. That will be required of any concrete class.
java编译器在接口方法之前添加public和abstract关键字,在数据成员之前添加public、static和final关键字。
The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
是的,因为您无法实例化抽象类(Shape),编译器知道 z() 将由(Shape 的)其他子类实现。
yes, because you wont be able to instantiate the abstract class (Shape), compiler knows the z() will be implemented by some other child class (of Shape).
抽象类不需要实现所有方法。这是他们的具体类/实现的责任。在这种情况下,是的
z()
将被视为Shape
的抽象方法。Abstract classes need not implement all methods. That's the responsibility of their concrete class/implementations. In this case, yes
z()
will be treated as abstract method ofShape
.每个非抽象类都必须为其任何抽象超类或接口中定义的所有方法提供实现。编译器足够聪明,可以检查类的整个层次结构,以确定您忘记实现您的类声称为其提供实现的某些内容。
every non-abstract class will have to provide implementation for all of the methods defined in any of it's abstract superclasses or interfaces. Compiler is clever enough to check the whole hierarchy of classes to determine that you forgot to implement something that your class claims to provide implementation for.