java ArrayList包含不同的对象

标签 java object arraylist

是否可以创建 ArrayList<Object type car,Object type bus> list = new ArrayList<Object type car,Object type bus>() ;

我的意思是将来自不同类的对象添加到一个数组列表中?

谢谢。

最佳答案

是的,这是可能的:

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 的对象。

关于java ArrayList包含不同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13566983/

相关文章:

java - 搜索具有特定范围内的属性值的对象的 ArrayList

java - DateFormatter 那个不挑剔?

java - 事务隔离级别

javascript - 使所有函数成为严格模式文字对象?

ruby-on-rails - 如何通过 form_for rails 传递对象?

Java ArrayList indexOf 泛型类型

java - 如何在 Thymeleaf 片段中重复 Spring 模型

java - 如何向 jMusic 添加 "Arabian"或 "Microtone"或 "Quarter-tone"支持?

PHP OOP 类设计

c# - 为什么 C# ArrayList 没有 Resize 方法?