为什么我们不能在没有匿名类方法的情况下在java中实例化接口或抽象类? [英] Why can't we instantiate an interface or an abstract class in java without an anonymous class method?

查看:90
本文介绍了为什么我们不能在没有匿名类方法的情况下在java中实例化接口或抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,除了使用匿名类方法之外,我们无法在java中实例化接口或抽象类,但它背后的原因是什么?

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?

推荐答案

您无法实例化接口或抽象类,因为它会违反面向对象的模型。

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中实例化接口或抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆