基础问题

[复制链接]
查看11 | 回复2 | 2008-10-2 08:56:24 | 显示全部楼层 |阅读模式
public class Petcount3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Vector pets=new Vector();
Class[]petTypes={
pet.class,
Dog.class,
Pug.class,
Cat.class,
Rodent.class,
Gerbil.class,
Hamster.class,
};
try{
for(int i=0;i<15;i++){
int rnd=1+(int)(Math.random()*(petTypes.length-1));
pets.addElement(petTypes[rnd].newInstance());
}
}
catch(InstantationException e){

}
有谁能帮我解释一下for里面的东东?谢谢!

回复

使用道具 举报

千问 | 2008-10-2 08:56:24 | 显示全部楼层
随机的产生一个数(1+(int)(Math.random()*(petTypes.length-1))),根据这个数去查找数组petTypes中的相应内容(petTypes[rnd]),根据内容实例化一个对象(petTypes[rnd].newInstance())并插进pets中(pets.addElement())...
回复

使用道具 举报

千问 | 2008-10-2 08:56:24 | 显示全部楼层
随机的往pets中添加15个对象,来自:(pet.class, Dog.class, Pug.class, Cat.class, Rodent.class, Gerbil.class, Hamster.class, )...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行