Java中的接口 - 它们用于什么? [英] Interfaces in Java - what are they for?

查看:122
本文介绍了Java中的接口 - 它们用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

接口的目的继续

我刚开始学习Java不久前。

我遇到了接口我知道如何使用但仍然无法理解它。< br>
据我所知, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆