java中接口的实际用途是什么? [英] what is the actual use of interface in java?

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

问题描述


可能重复:

抽象类和接口类?

Java:接口/抽象类/抽象方法

在Java中,无论如何接口的使用由抽象类实现。我知道接口的一个优点是,如果我们实现一个接口,那么我们也可以扩展另一个类。 Java中是否有任何其他用途或优势?

In Java, whatever use of interface is fulfilled by abstract class. I know one advantage of interfaces is that if we implement an interface then we can also extend another class. Is there any other use or advantage of interface in Java?

推荐答案

您喜欢什么:一个抽象类中的数千个抽象方法并继承此类或为特定抽象方法创建尽可能多的接口并通过继承所需数量的接口来使用你想要的那些......

What you like : thousands of abstract methods in one Abstract Class and inherit this class OR make as many interfaces for specific abstract methods and use those only you want by inheriting as many interfaces as needed...

abstract class A
{
 //thousands of abstract method();
 abstract methodA();
 abstract methodB();
 abstract methodC();
}

//OR
interface ForOnlymethodA
{
 void methodA();
}
interface FormethodBandmethodC
{
 void methodB();
 void methodC();
}

因此,只使用继承特定接口所需的方法,如果你继承了抽象类,那么你不必要地继承一个类中不需要的所有方法,而在其他一些类中可能需要这些方法。

So, use that method only what you just need by inheriting particular interface, if you are inheriting Abstract classes then you are unnecessarily inheriting all methods that you don't need in one class and may be needed in some other classes..

这篇关于java中接口的实际用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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