Java 中的接口 - 它们是做什么用的? [英] Interfaces in Java - what are they for?
问题描述
可能的重复:
接口的目的继续
我不久前才开始学习 Java.
我遇到过 Interfaces
,我知道如何使用它,但仍然不能完全理解它的概念.
据我了解,interfaces
通常由类实现,然后类必须实现接口中声明的方法.
问题是——这到底是什么意思?将接口中的方法作为普通类方法实现不是更容易吗?使用接口到底有什么好处?
I just started learning Java not so long ago.
I've come across Interfaces
which I know how to use but still can't quite grasp the idea of it.
As I understand it, interfaces
are usually implemented by classes, which then have to implement the methods declared in the interface.
The problem is - what exactly is the point? Wouldn't it be easier to just implement the methods from the interface as normal class methods? What exactly are the advantages of using interfaces?
我试过在谷歌上寻找答案.有很多,但我仍然无法理解它的意义.我还阅读了这个问题及其答案,但整个合同的事情只会让它变得更加复杂......
I've tried looking for an answer on google. There were many but I still couldn't understand the point of it. I also read this question and its answers, but the whole contract thing just makes it more complicated...
希望有人能把它简化得足够好!:)
提前致谢!
Hopefully someone could simplify it good enough! :)
Thanks in advance!
推荐答案
一个接口允许你在运行时提供不同的实现,注入依赖,分离关注点,使用不同的实现进行测试.
An interface allows you to provide a different implementation at runtime, inject dependencies, separate concerns, use different implementations for testing.
只需将接口视为类保证实现的契约即可.实现接口的具体类是无关紧要的.不知道这是否有帮助.
Just think of an interface as a contract that a class guarantees to implement. The concrete class that implements the interface is irrelevant. Don't know if this helps.
想一些代码可能会有所帮助.这并不能解释接口的所有内容,请继续阅读,但我希望这可以帮助您入门.这里的要点是你可以改变实现......
Figured some code might help. This doesn't explain everything there is more to interfaces, keep reading but I hope this gets you started. Point here is that you can vary the implementation...
package stack.overflow.example;
public interface IExampleService {
void callExpensiveService();
}
public class TestService implements IExampleService {
@Override
public void callExpensiveService() {
// This is a mock service, we run this as many
// times as we like for free to test our software
}
}
public class ExpensiveService implements IExampleService {
@Override
public void callExpensiveService() {
// This performs some really expensive service,
// Ideally this will only happen in the field
// We'd rather test as much of our software for
// free if possible.
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// In a test program I might write
IExampleService testService = new TestService();
testService.callExpensiveService();
// Alternatively, in a real program I might write
IExampleService testService = new ExpensiveService();
testService.callExpensiveService();
// The difference above, is that we can vary the concrete
// class which is instantiated. In reality we can use a
// service locator, or use dependency injection to determine
// at runtime which class to implement.
// So in the above example my testing can be done for free, but
// real world users would still be charged. Point is that the interface
// provides a contract that we know will always be fulfilled, regardless
// of the implementation.
}
}
这篇关于Java 中的接口 - 它们是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!