定义类
public class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public void introduce() {
System.out.println("我是" + name + ",考了" + score + "分");
}
}
创建对象
Student s = new Student("小明", 92);
s.introduce();
private 隐藏内部细节,通过 public 方法访问,这就是封装。this 指当前对象,用来区分同名变量。