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
screen = this.screen;
cpu = this.cpu;
mem = this.mem
你这一步赋值给反了
只是把成员变量的初始化值进行了输出(double类型初始化值是0.0)
还有以后记得加注释
把screen = this.screen;
cpu = this.cpu;
mem = this.mem;改成
this.screen = screen;
this.cpu = cpu;
this.mem = mem;
大哥,参数赋值的时候方向反了吧。 应该是this.XX=XX;形参和实参最好不要用一样的,。就避免用this。
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;
因为本类中你没有初始化值,默认值就是0.0,this.属性名代表就是本类中的属性值,你把本类中的值赋值给了方法中的属性,那返回的肯定是默认值