運(yùn)行結(jié)果為什么全是0.0?
package com.fgs;
public class InitailTelephone {
public static void main(String[] args) {
Phone p = new Phone();
Phone p1 = new Phone(5.0,2.5,4);
}
}
**************************************************
public class Phone {
private double screen;
private double cpu;
private double mem;
public Phone() {
System.out.println("無參構(gòu)造");
}
public Phone(double screen,double cpu,double mem){
screen = this.screen;
cpu = this.cpu;
mem = this.mem;
System.out.println("屏幕"+screen+"cpu"+cpu+"內(nèi)存"+mem);
}
}
輸出結(jié)果:
無參構(gòu)造
屏幕0.0cpu0.0內(nèi)存0.0
2017-06-21
screen = this.screen;
cpu = this.cpu;
mem = this.mem
你這一步賦值給反了
只是把成員變量的初始化值進(jìn)行了輸出(double類型初始化值是0.0)
還有以后記得加注釋
2017-06-21
把screen = this.screen;
cpu = this.cpu;
mem = this.mem;改成
this.screen = screen;
this.cpu = cpu;
this.mem = mem;
2017-06-21
大哥,參數(shù)賦值的時(shí)候方向反了吧。 ?應(yīng)該是this.XX=XX;形參和實(shí)參最好不要用一樣的,。就避免用this。
2017-06-21
screen = this.screen;
cpu = this.cpu;
mem = this.mem;
寫反了。
有參構(gòu)造方法中傳入的參數(shù)screen、cpu、men值,被默認(rèn)初始化為0.0的screen,cpu,men值替代
this.screen=screen;
this.cpu=cpu;
this.mem=men;
2017-06-21
因?yàn)楸绢愔心銢]有初始化值,默認(rèn)值就是0.0,this.屬性名代表就是本類中的屬性值,你把本類中的值賦值給了方法中的屬性,那返回的肯定是默認(rèn)值