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

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

问题描述

是否可以创建ArrayListlist = new 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>();

vehicles 列表将接受任何实现 IVehicle 的对象.

The vehicles list will accept any object that implements IVehicle.

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

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