视频中的 screen,cpu 和mem 都是private,为何只有screen需要用set 和get 方法获取,其他两个为何可以直接使用?(代码如下)
Public class Telephone(){
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;
}
public static void main(String[]args){
Telphone phone = new Telphone();
Telphone phone2 = new Telphone(5.0f,1.4f,2.0f);
//phone2.cpu = 6.0f;
phone2.setScreen(6.0f);
System.out.println("screen:"+phone2.getScreen());
就拿screen举个例子,只是用到screen了,就getter,setter了screen
那请问是不是只有单独访问private的变量时才需要get和set,带参构造函数不管变量是不是private的 都可以访问?烦请大神再多解释一下呗