问题描述
我知道,除了使用匿名类方法之外,我们不能在java中实例化一个接口或一个抽象类,但是后面有什么是 reason ?
谢谢!!
I know, we can not instantiate either an interface or an abstract class in java except using anonymous class method but what is the reason behind it?Thank you !!
推荐答案
您不能实例化接口或抽象类,因为它会违反对象面向对象的模型。
You can't instantiate an interface or an abstract class because it would defy the object oriented model.
接口代表契约 - 接口的实现者将能够做所有这些事情,履行合同。
Interfaces represent contracts - the promise that the implementer of an interface will be able to do all these things, fulfill the contract.
抽象类是一个类似的想法,因为它们表示未履行的契约,能够做事情的承诺,除了不同的接口,他们有一些定义的函数或字段,但需要填充可以使用。
Abstract classes are a similar idea, in that they represent an unfulfilled contract, a promise to be able to do things, except unlike interfaces they have some of their functions or fields defined but need filling in before they can used.
简单地说,在一个好的面向对象的程序中,你不应该实例化一个抽象类或接口。如果你这样做,设计可能是错误的。
Simply, in a good object oriented program, you should never want to instantiate an abstract class or interface. If you do, the design is probably wrong.
(匿名类实际上是非抽象实例化,只是他们不需要给一个名字,看起来是原始接口,但它们实际上是没有名称的接口的实现,这是我的理解,至少。)
(Anonymous classes are actually non-abstract instantiations, just that they don't need to be given a name, so they appear to be 'raw interfaces' but they're actually an implementation of the interface that has no name. That's my understanding, at least.)
这篇关于我知道,我们不能在java中实例化一个接口或抽象类,除非使用匿名类方法,但是背后的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!