问答详情
源自:7-1 什么是 Java 中的封装

为什么封装出现问题,求各位大神指点

class Telphone

{

        private float screen;

        private float cpu;

        private float mem;


public float getScreen()

{

return screen;

}


public void setScreen(float newScreen)

{

screen=newScreen;

}

public Telphone()

{

System.out.println("无参");

}

public Telphone(float newScreen,float newCpu,float newMem)

{

screen=newScreen;

cpu=newCpu;

    mem=newMem;

System.out.println("有参");

   }

}


class InitailTelphone

{

public static void main(String[] args)

{

Telphone phone=new Telphone();


Telphone phone2=new Telphone(5.0f,1.4f,2.0f);

 phone2.setScreen(6.0f);


System.out.println("screen:"+phone2.getScreen());

}

}


提问者:慕婉清0372823 2018-07-19 17:00

个回答

  • ByteMan
    2019-01-09 21:20:51

    这些代码一点问题没有!如果你报错那就是你把他们都写道一个类中了。

  • wujingfei
    2018-07-19 18:50:04

    public Telphone()没有指定类型 public void Telphone()