如何创建类的ArrayList? [英] How to create an ArrayList of Classes?
本文介绍了如何创建类的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些类(车
,摩托车
,火车
...等),从扩展类车辆
。
从另一个类我需要创建一个的ArrayList
类的访问只有那些inlude的的ArrayList
。
I have some classes (Car
, Motorcycle
, Train
... etc) that extends from class Vehicle
.
From another Class I need to create an ArrayList
of Classes for access only to those that inlude the ArrayList
.
这个概念是与此类似,但显然它不工作;
The concept is similar to this, but obviously it doesn't work;
ArrayList<Class> vehicleType=new ArrayList<Class>();
vehicleType.add(Class.forName("train"));
我该如何解决呢?
谢谢
How can I solve it ? Thanks
推荐答案
大多数的答案按照你的使用建议的Class.forName()
,虽然这是没有必要的。你可以呼叫的.class
的类型名称。
Most answers follow your suggestion of using Class.forName()
, though that's not necessary. You can "call" .class
on the type name.
看看这个JUnit测试:
Take a look at this JUnit test:
@Test
public void testListOfClasses() {
List<Class<?>> classList = new ArrayList<Class<?>>();
classList.add(Integer.class);
classList.add(String.class);
classList.add(Double.class);
assertTrue("List contains Integer class", classList.contains(Integer.class));
}
我希望您的需求清单将是类型的类和LT ;?延长车辆&GT;
这篇关于如何创建类的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文