类中私有方法,私有变量
public class girl {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 0 || age > 100) {
} else {
this.age = age;
}
}
private void wash() {
System.out.println("我要洗澡了,要不要来看");
}
public void show() {
if (age < 18) {
System.out.println("少儿不宜");
} else {
wash();
}
}
}
测试类
public class test {
public static void main(String[] args) {
girl g=new girl();
g.setName("老王");
String name = g.getName();
System.out.println(name);
g.setAge(19);
g.show();
}
}