成员变量私有的标准代码
Student.java
/* * 学生类 */ public class Student { private String name; private int age; public void setName(String a) { name=a; } public String getName() { return name; } public void setAge(int b) { age=b; } public int getAge() { return age; } }
StudentDemo.java
public class StudentDemo { public static void main(String[] args) { //使用Student.java //创建对象 Student s=new Student(); System.out.println(s.getAge()+s.getName()); s.setAge(30); s.setName("林依晨"); System.out.println(s.getAge()+s.getName()); } }