Java的ArrayList包含不同的对象 [英] java ArrayList contains different objects

查看:248
本文介绍了Java的ArrayList包含不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能创建
的ArrayList<对象类型的汽车,对象类型总线>名单=新的ArrayList<对象类型的汽车,对象类型总线>();

我的意思是添加不同类的对象一个ArrayList的?

I mean add objects from different classes to one arraylist?

感谢。

推荐答案

是的,这是可能的:

public interface IVehicle { /* declare all common methods here */ }
public class Car implements IVehicle { /* ... */ }
public class Bus implements IVehicle { /* ... */ }

List<IVehicle> vehicles = new ArrayList<IVehicle>();

名单将接受任何对象实现 IVehicle

The vehicles list will accept any object that implements IVehicle.

这篇关于Java的ArrayList包含不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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