gpt4 book ai didi

java - 接口(interface)完整引用

转载 作者:行者123 更新时间:2023-11-29 06:44:11 25 4
gpt4 key购买 nike

我正在浏览完整引用中定义的接口(interface)。我只是想了解下一段提到的内容。

"Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. This requirement by itself makes for a static and nonextensible classing environment. Inevitably in a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be available to more and more subclasses. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy. Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement"

那么,作者所说的“通常情况下,为了将一个方法从一个类调用到另一个类”是什么意思?请发表您的看法。

最佳答案

这意味着如果你不使用接口(interface),你尝试做这样的事情:

someObject.doSomething();

...(换句话说,调用其他类的方法),其他类必须在编译时可用,否则编译器将不知道如何编译对 的方法调用doSomething 方法。

另一方面,如果您对 someObject 的了解只是它实现了一个接口(interface),该接口(interface)上有一个 doSomething 方法,那么只需要接口(interface)可用于编译器来完成它的工作。然后其他人可以调用您的已编译代码,传入包中的一些对象,这些对象在编译时对您不可用,只要它实现了您的接口(interface)即可。

关于java - 接口(interface)完整引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7884655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com