public void setNames(String names) {
this.names = names;
}
}
上面的setNames()就定义了一个可变参数.调用时,可以怎么写:
Group g = new Group();
g.setNames("xiaoming","xiaohong","xiaojun");
g.setNames("xiaoming","xiaohong");
g.setNames("xiaoming");
完全可以把可变参数改写成String[]类型:
class Group{
private String[] names;
public void setNames(String[] names){
this.names = names;
public class main{
public static void main(String[] args){
Person p = new Person();
String bob = "Bob";
p.setName(bob);
System.out.println(p.getName));
bob = "Alice"; // bob还是Alice
System.out.println(p.getName());
}
}
class Person{
private String name;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}