java Java中没有接口的构造函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6201489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:48:06  来源:igfitidea点击:

There is no constructor for interface in Java?

java

提问by olidev

In Java, if I have an interface:

在 Java 中,如果我有一个接口:

public interface MyInterface{
}

Then MyInterfaceimplementation is:

然后MyInterface实现是:

class MyClass implements MyInterface {
    public MyClass(int a) {
    }
}

So what I mean is that if a user wants to do declare a MyInterfaceinstance with constructor:

所以我的意思是,如果用户想MyInterface用构造函数声明一个实例:

MyInterface mine = new MyInterface(2);

then it is not possible right?

那不可能吧?

回答by aioobe

MyInterface mine = new MyInterface(2);

then it is not possible right?

MyInterface mine = new MyInterface(2);

那不可能吧?

That's right. You can never do something like

那就对了。你永远不能做这样的事情

MyInterface mine = new MyInterface(2);

After newyou have to pick a classthat implements the interface(*), such as MyClass:

之后new你必须选择一个实现了接口(*) ,如MyClass

MyInterface mine = new MyClass(2);

Why?
You can think of an interface as a property of a class. An analogy would be an adjective, such as "Red". It makes perfect sense to create, say, a red ball (new RedBall()) or a red car (new RedCar()), but just creating "red" (new Red()) doesn't make sense ("red what??").

为什么?
您可以将接口视为类的属性。类比是形容词,例如“红色”。创建一个红球 ( new RedBall()) 或一辆红色汽车new RedCar()( new Red()) 是完全合理的,但仅仅创建“红色” ( ) 没有意义(“红色什么??”)。

(*) You can create anonymous classes that implement the interface on the fly by doing new MyInterface() { ... }but technically speaking you're still instantiating a class.

(*) 您可以通过这样做来创建动态实现接口的匿名类,new MyInterface() { ... }但从技术上讲,您仍在实例化一个类。

回答by JohnnyO

You are correct. An interface may not specify a constructor, the constructor gets specified in the class that implements the interface.

你是对的。接口不能指定构造函数,构造函数在实现接口的类中指定。

public interface Foo {
    public void doSomething();
}


public class Bar implements Foo() {
    public Bar(int value) {
    }

    @Override
    public void doSomething() {
    }
}

then when you want to use your interface you need to do something like

那么当你想使用你的界面时,你需要做一些类似的事情

Foo foo = new Bar(2);

回答by Justin Niessner

You can't instantiate an interface directly. You need to use an instance of an implementing class:

你不能直接实例化一个接口。您需要使用实现类的实例:

myClass mine = new myClassImpl();

回答by BertNase

It is not possible to define a constructor signature in an java interface.

无法在 java 接口中定义构造函数签名。

If you want to assure a construction of objets using a particular signature your options are the following design patterns:

如果您想确保使用特定签名构建对象,您的选择是以下设计模式:

回答by Waldheinz

True, this is not possible. What you want to do might be accomplished by following the factory pattern. One thing the code you outlined in your question leaves unspecified is whichclass implementing your interface should be instantiated at all. You factory implementation is the place where you express your intentions about this in code. The outline for the factory might be something like this, to get you started:

诚然,这是不可能的。遵循工厂模式可能会完成您想要做的事情。您在问题中概述的代码未指定的一件事是应该实例化实现您的接口的哪个类。您的工厂实现是您在代码中表达对此意图的地方。工厂的大纲可能是这样的,让你开始:

public class FooFactory {
   public myClass createFoo() {
      if (/* some condition */) {
         return new myClassImpl(/* the constructor parameters for that implementation */);
      } else {
         /* return some other implementation; you get the idea */
      }
   }

回答by Eystein Bye

No it is notpossible to declare an instance of myClass since it is an interface.

不,它不是可能的,因为它是一个声明myClass的实例接口

Interfaces do not define a constructor, but the class implementing the interface still has a constructor.

接口不定义构造函数,但实现接口的类仍然具有构造函数。

If you intention is to make it impossible for the user to call the constructor of your class, you can always make the constructor private.

如果您打算让用户无法调用您的类的构造函数,则始终可以将构造函数设为私有。