在方法中,使参数引用指向一个新的对象
外面的引用是指向原来的对象?还是新的对象?
答:原来的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Hero {

String name; //姓名

float hp; //血量

float armor; //护甲

int moveSpeed; //移动速度

public Hero(){

}

public Hero(String name,float hp){
this.name = name;
this.hp = hp;
}

//复活
public void revive(Hero h){
h = new Hero("提莫",383);
System.out.println("复活函数"+h.hp);
}

public static void main(String[] args) {
Hero teemo = new Hero("提莫",383);

//受到400伤害,挂了
teemo.hp = teemo.hp - 400;

teemo.revive(teemo);

System.out.println("主函数"+teemo.hp);


}

}

运行图