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

查看:32
本文介绍了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();
}

因此,通过继承特定接口仅使用您只需要的方法,如果您继承了 Abstract 类,那么您将不必要地继承在一个类中不需要的所有方法,并且可能是在其他一些课程中需要..

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天全站免登陆