1.类的属性设置为 private 2.类的方法设置成 public 3.setter&getter class Student { private int math, english, chinese; public Student() { } public Student(int math, int english, int chinese) { this.chinese = chinese; this.math = math; this.english = english; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } public int getChinese() { return chinese; } public void setChinese(int chinese) { this.chinese = chinese; } public double avg() { return (this.chinese + this.english + this.math) / 3; } } public class Test { public static void main(String[] args) { Student s = new Student(60, 78, 90); System.out.println(s.avg()); } }