0

Геттеры и сеттеры Java

Объясните пожалуйста подробней Не понял

12th Feb 2025, 7:10 AM
PyDays🤔
PyDays🤔 - avatar
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() ); } }
12th Feb 2025, 8:35 AM
zemiak