是否可以创建
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/