人.java
public class Person {
public String firstName, lastName;
public Person(String firstName,
String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return(firstName + " " + lastName);
}
}
PersonTest.java
public class PersonTest {
public static void main(String[] args) {
Person[] people = new Person[20]; //this line .
for(int i=0; i<people.length; i++) {
people[i] =
new Person(NameUtils.randomFirstName(),
NameUtils.randomLastName()); //this line
}
for(Person person: people) {
System.out.println("Person's full name: " +
person.getFullName());
}
}
}
在上面的代码中,我们使用了两次“new”。这段代码是正确的还是错误的?第一个用于分配数组。但为什么是第二个?来自讲义。