简体   繁体   English

返回接口类型的对象的方法

[英]Methods That Return an Object of an Interface Type

All beginners like myself, always get confused to see a method returns an object of an interface type, because interfaces have abstract methods, thus cannot be instantiated. 像我这样的所有初学者,总是很困惑地看到方法返回接口类型的对象,因为接口具有抽象方法,因此无法实例化。

I finally figured out a way to understand this: 我终于想出了一种方法来理解这一点:

((when we say that a method returns an object of an interface type, we are actually implicitly saying that the method in fact returns an object/instance of some class that implements that interface, but in most cases that class is unknown because it is declared as anonymous in the implementation of the method. Thus, we refer to the returned object as being of that interface type.)). (((当我们说一个方法返回一个接口类型的对象时,我们实际上是在隐式地说,该方法实际上返回了实现该接口的某个类的对象/实例,但是在大多数情况下,该类是未知的,因为在该方法的实现中声明为匿名。因此,我们将返回的对象称为该接口类型。))。

Is this explanation correct ? 这个解释正确吗?

"...when we say that a method returns an object of an interface type, we are actually implicitly saying that the method in fact returns an object/instance of some class that implements that interface..." - It is correct, but we are saying it explicitly . “……当我们说某个方法返回接口类型的对象时,我们实际上是在隐式地说该方法实际上返回实现该接口的某个类的对象/实例……”-正确,但是我们在明确地说。

The second part of your definition is quite not correct, as @Jon Skeet pointed out. 正如@Jon Skeet所指出的,定义的第二部分是非常不正确的。 Applying anonymous class in the implementation is a very specific case. 在实现中应用匿名类是一个非常具体的情况。 Generally, returning an interface gives you more freedom: 通常,返回接口会给您更多自由:

  • It is possible to change implementation of the method to return another object that implements the same interface, without changing code that uses this method . 可以更改方法的实现,以返回实现相同接口的另一个对象, 而无需更改使用此方法的代码
  • You leave possibility for extending classes to override the method, so that it returns another object that implements the same interface. 您可以扩展类来覆盖该方法,以便它返回实现相同接口的另一个对象。 Here you can actually change the return type. 在这里,您实际上可以更改返回类型。 If method of the base class returned concrete class, eg, ArrayList , overridden method would have to also return ArrayList or its subclass. 如果基类的方法返回了具体类,例如ArrayList ,则重写的方法还必须返回ArrayList或其子类。

The rule of thumb is the following. 经验法则如下。 If concrete class implements an interface and there is no benefit in returning a concrete class object, eg, ArrayList , return an interface - List , Collection . 如果具体类实现接口,则返回具体类对象(例如ArrayList )没有好处,返回接口ListCollection This will enhance maintainability of your code, ie, the code will be easier to change in future. 这将增强代码的可维护性,即将来将更易于更改代码。

It's my birthday at the end of this month, so I've added a new method to all my friends and family: 这是我本月底的生日,所以我为我的所有朋友和家人添加了一种新方法:

public Present givePresent{
    //code to select an appropriate and sufficiently expensive present
    return present;
}

There's two things I could do here. 我可以在这里做两件事。 I could write a Present class and ensure that all possible presents extend it. 我可以编写一个Present类,并确保所有可能的礼物都对其进行扩展。 But we could run in to all sorts of problems here: BatmanComic already inherits from ComicBook for example, so we'd have to move further and further up the tree until Present is basically indistinguishable from Object . 但是我们这里可能会遇到各种各样的问题:例如BatmanComic已经从ComicBook继承ComicBook了,因此我们必须不断往前走,直到PresentObject基本上没有区别。 The other way is to look at what is actually happening here. 另一种方法是查看此处实际发生的情况。 I'm looking to receive something that fits in to a specific category and, in short, Java has two ways of doing that. 我希望收到适合特定类别的内容,简而言之,Java有两种方法可以做到这一点。 Inheritance and Interfaces. 继承和接口。 Creating Present as an interface is achieving exactly the same goal as creating it as an abstract superclass but avoids all problems of multiple inheritance. Present创建为接口与将其创建为抽象超类的目的完全相同,但是避免了多重继承的所有问题。 This way all I have to do is write an interface: 这样,我要做的就是编写一个接口:

public interface Present{
}

and make sure all the socks and books and whatever implement that. 并确保所有袜子和书本以及其他能够实现的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM