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

查看:99
本文介绍了为什么我们需要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天全站免登陆