接口中的构造方法 [英] constructor methods in interfaces

查看:107
本文介绍了接口中的构造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口中的构造方法不好吗?

Are constructor methods in interfaces bad?

推荐答案

为什么人们认为有人要实例化该接口?

Why do people think that anybody wants to instantiate the interface?

我们想要做的就是强制实现者像其他接口方法一样实现构造函数.

What we want to do is to force implementers to implement the constructor, just like other interface methods.

接口就像合同.假设我有一个接口Queue,并且我想确保实现者使用一个参数创建一个构造函数,该参数创建一个单例队列(一个仅包含该元素的新队列).为什么那不应该是合同的一部分?至少具有Java接口,无法指定.

An interface is like a contract. Let's say I have an interface Queue, and I want to make sure that implementers create a constructor with one argument, which creates a singleton queue (A new queue with just that element). Why should that not be part of the contract? With at least Java interfaces, that cannot be specified.

这篇关于接口中的构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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