为了账号安全,请及时绑定邮箱和手机立即绑定

第一个对象pd对象提示要参数 然后为什么set方法需要创建字段?初学者,求大神

public class Hi{

private float screen;

private float cpu;

private float mem;

public float getScreen(){

return screen;

}

public void setScreen(float newScreen){

screen=newScreen;

}


public Hi(float screen,float cpu,float mem){

if(screen<3.5f){

System.out.println("您输入有误,自动赋值");

screen=3.5f;

}

screen=screen;

cpu=cpu;

mem=mem;

System.out.println("有参数的方法实现了"+cpu+""

+ " "+screen);

}

}



public class Hello {


public static void main(String[] args) {

Hi pd = new Hi();

Hi pd2= new Hi(2.1f,2.2f,3.0f);

// TODO 自动生成的方法存根

pd2.setScreen=(5.5f);

System.out.println(pd2.getScreen());

}

}


正在回答

4 回答

哈哈哈,才发现是你自己给自己回答了,懂了就好了昂~

0 回复 有任何疑惑可以回复我~

改了主类里的一些内容,

public class Hello {

public static void main(String[] args) {

//去掉了这边这个无参数的。

Hi pd2= new Hi(2.1f,2.2f,3.0f);

pd2.setScreen(5.5f);  //这个是这样子的。而不是用等号去赋值。

System.out.println(pd2.getScreen());

}

}

0 回复 有任何疑惑可以回复我~

懂了   多打了等于号。少了Hi方法

0 回复 有任何疑惑可以回复我~

懂了   多打了等于号。少了Hi方法

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

第一个对象pd对象提示要参数 然后为什么set方法需要创建字段?初学者,求大神

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信