Telphone.java:
public class Telphone {
//用private关键字进行封装,保证属性安全性
private float screen;
//通过setScreen()方法设置属性值
public void setScreen(float newScreen){ //newScreen是参数
screen = newScreen; //将参数值传给属性
}
//通过getScreen()方法得到属性值
public float getScreen(){ //返回值类型必须与属性相同
return screen; //将screen的值返回
}
public Telphone(float newScreen){
screen = newScreen;
}
}
InitailTelphone.java:
public class InitailTelphone {
public static void main(String[] args) {
Telphone phone2 = new Telphone(5.0f);
//5.0为初始值,不影响setScreen()方法
//通过setScreen()方法给属性赋值
phone2.setScreen(6.0f); //screen=newScreen=6.0
//通过getScreen()方法得到属性值
System.out.println("screen:"+phone2.getScreen());
}
}
(理解)值传递:6.0 → setScreen() → screen → getScreen
大家看看我的代码,说说我最后一句理解对吗?如果不对,请给一个正确答案好吗?谢谢,不胜感激。
qq_匡璐_0