1:类的属性一般设置为private
getter 添加一个方法,用来读取属性的值
setter 添加一个方法,用来设置属性的值
例子
hello的主函数:
package my;
public class Hello {
public static void main(String[] args) {
Example ex=new Example();
ex.setNumber(123);
System.out.println("the result:"+ex.getNumber());
}
}
/*the result:123
Process finished with exit code 0
*/
Example 类
package my;
public class Example {
public int number;
public String name;
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
}