package com.itheima.encapsulation;
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setAge(-23);
System.out.println(s.getAge());
}
}
package com.itheima.encapsulation;
public class Student {
private int age;
/**
* 取值的方法:getAge
*/
public int getAge(){
return age;
}
/**
* 赋值的方法:setAge
*/
public void setAge(int age){
if(age >= 0 && age <= 200){
this.age = age;
}else{
System.out.println("年龄非法,应该不是人的年龄!");
}
}
}