关于JAVA中的一个超简单的函数问题!!

来源:百度知道 编辑:UC知道 时间:2024/06/27 18:04:49
首先定义一个类test, 类test中定义一个带3个参数的void函数:shuxing
public class test {
int Hp;
int Mp;
int Exp;

public void shuxing(int h, int m, int e) {
h = Hp;
m = Mp;
e = Exp;
System.out.println("HP:"+h+" Mp:"+m+" Exp:"+e);
}
}
然后在main函数中调用shuxing()
public class main {
public static void main(String[] args) {
test t = new test();
t.shuxing(350, 250, 100);
}
}
就这样,按理说显示的应该是:HP:350 Mp:250 Exp:100
但实际结果却都是0: HP:0 Mp:0 Exp:0
谁能告诉我这是WHY 急啊~~~~~~

因为一下三句又给h m e 重新赋值了,
h = Hp;
m = Mp;
e = Exp;
Hp Mp Exp都没有初始化,默认是0

public void shuxing(int h, int m, int e) {
h = Hp;
m = Mp;
e = Exp;
System.out.println("HP:"+h+" Mp:"+m+" Exp:"+e);
}

赋值顺序反了

改为
this.Hp= h ;
this.Mp=m;
this.Exp=e ;

...
哥哥了

h=Hp;
是吧HP赋值给h...
你Hp本来就没给值,怎么会有350呢?

没有初始化 确实 this.h=h;
this.m=m;
this.e=e;
这样初始化