接口与抽象类
我有点熟悉抽象类和接口类之间的区别,但是 你认为下面这句话的意思是什么?
接口只能定义常量,而抽象类可以有字段。
I am a little bit familiar with the difference between Abstract and Interface classes but
What do you think is the meaning of the sentence below?
An interface can only define constants while abstract class can have fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
接口中的字段是隐式的
public
、static
、final
,这与抽象类不同
your field from interface is by implicitly
public
,static
,final
which isn't case with abstract class
常量 - 静态,不改变(
static final
)字段 - 特定于实例、不断变化
由于接口无法实例化,因此只能具有静态且不可变的属性。另一方面,抽象类可以扩展,并且它们的子类可以实例化,因此您可以拥有特定于实例的、不断变化的属性。
constants - static, not changing (
static final
)fields - instance-specific, changing
Since interfaces cannot be instantiated, you can only have static and not-changing properties. On the other hand abstract classes can be be extended, and their subclasses - instantiated, so you can have instance-specific, changing properties.
嗯,这个说法在技术上是不正确的,他们指的是接口上的所有变量都必须声明为静态,而抽象类没有这样的限制。
该声明是不正确的,因为 Java 没有常量,只有
final
仍然是可修改的,因此不是常量。Well the statment is technically incorrect what they are refering to is that all variables on an interface must be declared static whereas abstract classes have no such limitation.
The statement is incorrect since Java does not have constants only
final
which are still modifiable and thus not constant.对 Jigar Joshi 答案的补充。我们可以实现任意数量的接口,但只能扩展一个抽象类。
Additional to Jigar Joshi answer. We can implements any number of interface but we can extend only one abstract class.