我曾多次看到有人提到这一点,但我不清楚这是什么意思。你什么时候,为什么要这么做?
我知道接口是做什么的,但我不清楚这一点的事实使我认为我错过了正确使用它们。
如果你要这样做
IInterface classRef = new ObjectWhatever()
你可以使用任何实现IInterface的类吗?你什么时候需要这样做?我能想到的唯一一件事是,如果你有一个方法,你不确定什么对象将被传递,除了它实现IInterface。我不知道你需要多久做一次。
另外,如何编写一个方法来接受实现接口的对象呢?这可能吗?
问:——……“你能使用任何实现接口的类吗?”
A:是的。
问:——……“你什么时候需要这样做?”
答:-每次你都需要一个实现接口的类。
注意:我们不能实例化一个没有被类实现的接口- True。
为什么?
因为接口只有方法原型,没有定义(只有函数名,没有它们的逻辑)
AnIntf anInst = new class();
//我们可以这样做,只有当类实现AnIntf。
// anInst将有类引用。
注意:现在我们可以理解如果Bclass和Cclass实现相同的Dintf会发生什么。
Dintf bInst = new Bclass();
// now we could call all Dintf functions implemented (defined) in Bclass.
Dintf cInst = new Cclass();
// now we could call all Dintf functions implemented (defined) in Cclass.
我们拥有:相同的接口原型(接口中的函数名),调用不同的实现。
参考书目:
原型——维基百科
即使当我们不依赖于抽象时,面向接口编程也是有利的。
接口编程迫使我们使用对象的上下文适当的子集。这很有用,因为它:
防止我们做一些不合时宜的事,还有
让我们在将来安全地更改实现。
例如,考虑实现Friend和Employee接口的Person类。
class Person implements AbstractEmployee, AbstractFriend {
}
在这个人的生日的情况下,我们编程到朋友界面,以防止像对待员工一样对待这个人。
function party() {
const friend: Friend = new Person("Kathryn");
friend.HaveFun();
}
在这个人的工作环境中,我们对雇员界面进行编程,以防止模糊工作场所的边界。
function workplace() {
const employee: Employee = new Person("Kathryn");
employee.DoWork();
}
太好了。我们在不同的环境中都有适当的表现,我们的软件运行良好。
在遥远的未来,如果我们的业务改变为与狗打交道,我们可以相当容易地更改软件。首先,我们创建一个实现Friend和Employee的Dog类。然后,我们安全地将新的Person()更改为新的Dog()。即使两个函数都有数千行代码,这个简单的编辑也可以工作,因为我们知道以下是正确的:
Function party只使用Person的Friend子集。
函数workplace只使用Person的Employee子集。
类Dog实现了Friend和Employee接口。
另一方面,如果任何一方或工作场所都针对Person进行编程,就会有同时拥有特定于Person的代码的风险。从“人”改为“狗”需要我们梳理代码,删除任何“狗”不支持的“人”特定代码。
寓意:为接口编程可以帮助我们的代码适当地运行,并为更改做好准备。它还使我们的代码能够依赖于抽象,这带来了更多的好处。
面向接口而不是实现的代码与Java无关,也与它的接口构造无关。
这个概念是在模式/四人帮的书中突出的,但很可能在那之前就已经存在了。这个概念在Java出现之前就已经存在了。
Java Interface构造的创建就是为了帮助实现这一想法(以及其他一些事情),人们过于关注作为意义中心的构造,而不是最初的意图。然而,这也是为什么我们在Java、c++、c#等语言中有公共和私有方法和属性的原因。
It means just interact with an object or system's public interface. Don't worry or even anticipate how it does what it does internally. Don't worry about how it is implemented. In object-oriented code, it is why we have public vs. private methods/attributes. We are intended to use the public methods because the private methods are there only for use internally, within the class. They make up the implementation of the class and can be changed as required without changing the public interface. Assume that regarding functionality, a method on a class will perform the same operation with the same expected result every time you call it with the same parameters. It allows the author to change how the class works, its implementation, without breaking how people interact with it.
And you can program to the interface, not the implementation without ever using an Interface construct. You can program to the interface not the implementation in C++, which does not have an Interface construct. You can integrate two massive enterprise systems much more robustly as long as they interact through public interfaces (contracts) rather than calling methods on objects internal to the systems. The interfaces are expected to always react the same expected way given the same input parameters; if implemented to the interface and not the implementation. The concept works in many places.
不要认为Java接口与“面向接口编程,而不是面向实现”的概念有什么关系。它们可以帮助应用概念,但它们不是概念。
我曾经给学生的一个具体例子是他们应该写作
List myList = new ArrayList(); // programming to the List interface
而不是
ArrayList myList = new ArrayList(); // this is bad
在一个短程序中,它们看起来完全相同,但如果在程序中继续使用myList 100次,就会开始看到区别。第一个声明确保只调用myList上由List接口定义的方法(因此没有ArrayList特定的方法)。如果您以这种方式对接口进行了编程,那么稍后您就可以确定您确实需要
List myList = new TreeList();
你只需要在这一点上修改代码。您已经知道,其余的代码不会因为更改实现而被破坏,因为您对接口进行了编程。
当您谈论方法参数和返回值时,好处甚至更明显(我认为)。举个例子:
public ArrayList doSomething(HashMap map);
该方法声明将您绑定到两个具体实现(ArrayList和HashMap)。一旦从其他代码调用该方法,对这些类型的任何更改都可能意味着您将不得不更改调用代码。最好是根据接口进行编程。
public List doSomething(Map map);
现在,不管您返回什么样的List,或者作为参数传入什么样的Map。在doSomething方法中所做的更改不会强制您更改调用代码。