1.将成员变量私有化
2.为每一个成员编写 set 和 get 方法
3.让类含有 “无参构造方法” 和 “有参构造方法”

学生类实例:

public class Student{
    private String name;
    private int age;

    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public Student(){}
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }
}

调用学生类

public class A{
    public static void main(String[]args){
        Student s = new Student("小明",17);
        System.out.println(s.getName() + "今年" + s.getAge() + "了");
    }
}