一、有这么一道题:
一个Java 类 User 的定义如下 :
public class User {
private String name;
private String emailAddress;
private boolean male;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String address) {
this.emailAddress = address;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User( String name, String emailAddress, boolean male ) {
this.name = name;
this.emailAddress = emailAddress;
this.male = male;
}
}
假设在 Velocity 的 runtime context 中已经存在一个 Java 容器类 ArrayList 的实例变量 $user 。
$user 的初始化方法为:
java.util.List user = new java.util.ArrayList(5);
user.add( new User( "John ", "john@some.domain ", true ) );
user.add( new User( " Alice ", "alice@other.domain ", false ) );
user.add( new User( "Tom ", "tom@other.domain ", true ) );
user.add( new User( "Elisa ", "elisa@some.domain ", false ) );
user.add( new User( "Zine ", "zine@other.domain ", true ) );