運行結果為什么全是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("無參構造");
}
public Phone(double screen,double cpu,double mem){
screen = this.screen;
cpu = this.cpu;
mem = this.mem;
System.out.println("屏幕"+screen+"cpu"+cpu+"內存"+mem);
}
}
輸出結果:
無參構造
屏幕0.0cpu0.0內存0.0
2017-06-21
screen = this.screen;
cpu = this.cpu;
mem = this.mem
你這一步賦值給反了
只是把成員變量的初始化值進行了輸出(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
大哥,參數賦值的時候方向反了吧。 ?應該是this.XX=XX;形參和實參最好不要用一樣的,。就避免用this。
2017-06-21
screen = this.screen;
cpu = this.cpu;
mem = this.mem;
寫反了。
有參構造方法中傳入的參數screen、cpu、men值,被默認初始化為0.0的screen,cpu,men值替代
this.screen=screen;
this.cpu=cpu;
this.mem=men;
2017-06-21
因為本類中你沒有初始化值,默認值就是0.0,this.屬性名代表就是本類中的屬性值,你把本類中的值賦值給了方法中的屬性,那返回的肯定是默認值