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

查看:24
本文介绍了为什么我们不能在没有匿名类方法的情况下在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天全站免登陆