java封装
public class teacherDemo { public static void main(String []args) { String name = "张三"; int age = 25; teacher tea = new teacher(); tea.setName(name); tea.setAge(age); System.out.println(tea.getAge()); System.out.println(tea.getName()); } } class teacher{ private String name; private int age; //属性 public String getName() { return name; } public void setName(String sName) { name = sName; } public int getAge() { return age; } public void setAge(int sAge) { if(sAge<22) { System.out.println("错误,教师年龄不能小于22岁"); sAge = 22; }else { age = sAge; } } }