Java 接口。为什么在这种情况下是有用的
我读了很多关于java接口的书。我知道你可以实现多态性和其他伟大的东西(函数指针等等)。我有理论知识,但实际知识很少,甚至没有。我一直在使用很多已经制作好的接口,比如“Runnable”或“Listeners”。但我还是不能百分之百地理解他们。如果有人回答以下问题,也许我会更好地理解: 所以最近我在学习LibGdx,我遇到了一个叫做“一次性”的界面。它有一个名为“dispose()”的方法,该方法的文档说明: 释放此对象的所有资源 所以我假设这个接口是这样声明的:Java 接口。为什么在这种情况下是有用的,java,interface,Java,Interface,我读了很多关于java接口的书。我知道你可以实现多态性和其他伟大的东西(函数指针等等)。我有理论知识,但实际知识很少,甚至没有。我一直在使用很多已经制作好的接口,比如“Runnable”或“Listeners”。但我还是不能百分之百地理解他们。如果有人回答以下问题,也许我会更好地理解: 所以最近我在学习LibGdx,我遇到了一个叫做“一次性”的界面。它有一个名为“dispose()”的方法,该方法的文档说明: 释放此对象的所有资源 所以我假设这个接口是这样声明的: public interfac
public interface Disposable {
public void dispose();
}
我有一个实现这个接口的类
public class Main implements Disposable {
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
There is a term in Java "Coding to Interfaces".
Check the link:
http://stackoverflow.com/questions/1970806/coding-to-interfaces
Coding to an interface rather than to implementation. This makes your software/application easier to extend. In other words, your code will work with all the interface’s subclasses, even ones that have not been created yet.
Anytime if you are writing code that interacts with subclasses, you have two choices either; your code directly interacts with subclasses or interacts with interface. Like this situation, you should always favor/prefer to coding to an interface, not the implementation.
To use the interface one can simply call the methods on an instance of the concrete class.
One would call the methods on a reference of the type interface, which happens to use the concrete class as implementation:
List<String> l = new ArrayList<String>();
l.add("foo");
l.add("bar");
If you decided to switch to another List implementation, the client code works without change:
List<String> l = new LinkedList<String>();
This is especially useful for hiding implementation details, auto generating proxies, etc.
Advantages
App/Software is easier to extend
Adds Flexibility to your App.
Helps to maintain the loose coupling of code.
问题是:如果这个方法是空的,那么它在被调用时怎么做?它不能处理任何东西
我可以在这个类中拥有自己的方法来处理对象。为什么我们需要这个接口
这只是一个例子。我遇到过很多类似的界面
我真的无法理解像这样的接口。您的用例的可能原因:
dispose
。这在垃圾收集器模式中很有用,因此您可以执行garbageCollector.collect(一次性)
。如果接口是某个GarbageCollector
包的一部分,那么这是有意义的关闭
,并且需要一个自动关闭
,以进行比较原因是,如果您有另一个对象的方法采用一次性类型,那么它期望(实际上需要)接口指定的方法存在。可能是因为它会在某个地方调用该方法
通过这种方式,您可以拥有多个实现一次性的类(每个类都以自己的方式),然后您可以通过其一次性接口传递该类的实例,该接口将公开接口指定的任何方法。获取一次性实例的类可能取决于该方法的存在。问题是,您可能有许多不同类型的对象,所有这些对象都需要处理。现在,您可以编写一系列漂亮的
if-else
语句,尝试确定给定的对象是否是其他类型对象的实例,这样您就可以确定应该如何处理它(以及触发它需要什么方法),或者您可以,正如您在这里所做的,定义一个通用的接口
,所有希望在某个时间处理的对象都可以使用该接口
这意味着您可以基于这个通用的接口
引用每个不同的对象,允许它们都以实例的形式出现一次性的
,这是多态性的基础
这意味着只需使用一次性
对象列表,就可以快速轻松地调用每个对象的dispose
方法。调用方不关心如何实现它,只关心在调用它时,执行所需的约定…库通常提供接口,以便您可以扩展接口,而不是更改内部代码。这将保持使用库的代码的兼容性
提供空的实现取决于开发人员,但在接口文档中,他们提供了实现接口的具体类的实际实现。Java中有一个术语“接口编码”。
public class Main implements Disposable {
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
There is a term in Java "Coding to Interfaces".
Check the link:
http://stackoverflow.com/questions/1970806/coding-to-interfaces
Coding to an interface rather than to implementation. This makes your software/application easier to extend. In other words, your code will work with all the interface’s subclasses, even ones that have not been created yet.
Anytime if you are writing code that interacts with subclasses, you have two choices either; your code directly interacts with subclasses or interacts with interface. Like this situation, you should always favor/prefer to coding to an interface, not the implementation.
To use the interface one can simply call the methods on an instance of the concrete class.
One would call the methods on a reference of the type interface, which happens to use the concrete class as implementation:
List<String> l = new ArrayList<String>();
l.add("foo");
l.add("bar");
If you decided to switch to another List implementation, the client code works without change:
List<String> l = new LinkedList<String>();
This is especially useful for hiding implementation details, auto generating proxies, etc.
Advantages
App/Software is easier to extend
Adds Flexibility to your App.
Helps to maintain the loose coupling of code.
检查链接:
http://stackoverflow.com/questions/1970806/coding-to-interfaces
编码到接口而不是实现。这使您的软件/应用程序更易于扩展。换句话说,您的代码将处理所有接口的子类,即使是尚未创建的子类。
无论何时,如果您正在编写与子类交互的代码,您都有两种选择:;您的代码直接与子类交互或与接口交互。像这种情况一样,您应该总是喜欢/更喜欢将代码编写到接口,而不是实现。
要使用接口,只需在具体类的实例上调用方法即可。
我们可以在类型接口的引用上调用方法,该接口恰好使用具体类作为实现:
列表l=新的ArrayList();
l、 添加(“foo”);
l、 添加(“酒吧”);
如果您决定切换到另一个列表实现,则客户端代码不会发生更改:
列表l=新的LinkedList();
这对于隐藏实现细节、自动生成代理等特别有用。
优势
应用程序/软件更易于扩展
增加应用程序的灵活性。
有助于保持代码的松散耦合。
对于您的类来说,界面就像原型一样。它的目的是为类提供一个结构
如果您的类正在实现任何接口,这意味着该类必须声明接口中定义的所有属性(即方法)。对你的班级来说,它可以被看作是一个形容词
比如说,如果你有两门课,即鹰和鹦鹉。然后您应该创建一个名为canFly的接口。现在老鹰和鹦鹉都能飞了,但它们的飞行方式可能会有所不同。这就是接口没有声明的原因