package Example01;
/*
- 实现类的封装 */ class Student{ private String name ; //将name私有化 private int age ;
//下面公有方法间接访问类中属性值getxxx方法,setxxx方法
public String getName(){
return name;
}
public void setName(String stuName) {
name = stuName;
}
public int getAge() {
return age;
}
public void setAge(int stuAge) {
if(stuAge < 0){
System.out.println("不合法");
}else{
age = stuAge;
}
}
public void introduce() {
System.out.println("大家好,我叫"+name + ",我今年"+ age +"岁");
}
public class example01 {
public static void main(String[] args) {
Student stu = new Student() ;
stu.setAge(30);
stu.setName("韩强");
stu.introduce();
}
}
}