好处:1.隐藏类的实例变量,方便修改和实现
2.只能通过规定的getter/setter来访问数据
public class Car {
String name;
private String color;
float size;
public String getColor(){
return color;
}
public void setColor(String newColor){
color=newColor;
}
}
public class InitialCar {
public static void main(String[] args) {
Car d=newCar();
d.setColor("red");
String a= d.getColor();
System.out.println("color "+a);
}
}