0
Геттеры и сеттеры Java
Объясните пожалуйста подробней Не понял
1 Respuesta
+ 2
If a class has a private field, such as a,
you can write methods that allow users of the class to
read that field with getA() or
change its value with setA().
The advantage is that you have better control over reading and writing to the a.
public class StoreA {
private int a;
public int getA() {
return a;
}
public void setA(int a) {
//you can check the inputed value here
this.a = a;
}
public static void main(String[] args) {
StoreA obj = new StoreA();
obj.setA(100);
System.out.println( obj.getA() );
}
}