为什么我们需要 Java 中的接口? [英] Why do we need interfaces in Java?

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

问题描述

在 Java 中,我们使用接口来实现多重继承.它是唯一使用的接口吗?如果是,Java 中接口的主要用途是什么?为什么我们需要 Java 中的接口?

In Java to implement multiple inheritance we use interfaces. Is it the only use of interfaces? If yes, what is the main use of interface in Java? Why do we need interfaces in Java?

推荐答案

我想说的主要用途是多态性,或者说能够对许多不同的对象执行相同的操作.例如,如果不同的对象都实现相同的接口并具有相同的方法,则可以将所有这些对象存储在一个 Vector 中,例如,并遍历 Vector 对每个对象调用该方法.

I would say the main use is polymorphism, or the ability to perform the same operation on a number of different objects. If different objects all implement the same interface and have the same method, you can store all of those objects in a Vector, for example, and iterate through the Vector calling that method on each one.

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

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