在<<JAVA 编程思想>>一书中也是说传递的引用,可是我总觉得不对,不因该是传递的引用!
求教高手一个小程序让我理解下好不?
举个很简单的例子吧
我定义一个类叫Person,
[CODE]class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String s){
name=s;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public String toString(){
return "名字:"+name+",年龄:"+age;
}
}
public class Test{
public static void main(String[] args){
Person p1=new Person("王二小",15);
Person p2=new Person("刘胡兰",20);
changeName(p1);
System.out.println(p1);
changePerson(p2);
System.out.println(p2);
}
private static void changeName(Person p){
p.setName("新的名字");
System.out.println(p);
}
private static void changePerson(Person p){
p=new Person("全新",25);
System.out.println(p);
}
}[/CODE]
好好体会一下输出就知道了