java对象方法,求高手详细解释
public class Test {
int age = 20;
public void change(Test people){
people = new Test();
people.age = 50;
}
public static void main(String[] args) {
Test t = new Test();
int age = t.age;
System.out.println(age);
t.change(t);
int age2 = t.age;
System.out.println(age2);
}
}
方法中的,people = new Test(),, 能不能解释一下 这个形式参数也可以生成实例?通常new一个对象前面加类名,为什么这个没有加呢?还有就是t.change(t); 这个不太理解,希望高人解释一下
追问:people不是形式参数吗?怎么可以创建对象啊?
答案:people = new Test(); 这句的意思新建一个Test对象并且把对象赋值给传进来的参数people
t.change(t);t这个对象调用它的change方法,传入的参数是他本身;
上一个:java 怎样转换字符串中字符位置
下一个:java设计模式 哪个模式提供了一个一致的接口 来应对 麻烦而复杂的代码