由一下的例子,我们可以对
成员变量,类变量,构造函数,方法,对私有变量的取值和赋值,可窥一斑。
- public class Test {
- //成员变量
- private int x; //注意这里的属性
- private int y;
- private String id;
- //类变量
- static int count;
- //构造函数
- public Test() {
- id = "Test" + (++count);
- }
- public Test(int x,int y) {
- this();
- this.x = x;
- this.y = y;
- }
- //方法
- public void showTest() {
- System.out.println(id + "/n" + " x = " + x + "/n" + " y = " + y);
- }
- //增加以后在多个类中的对Private属性变量的操作演示
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public void setX(int x) {
- this.x = x;
- }
- //为了演示,这里就放在同一个类里面
- public static void main(String[] args) {
- Test exam = new Test();
- exam.showTest();
- //exam.x = 5;
- //exam.showTest();
- exam.setId("Example");
- exam.setX(8);
- System.out.println(exam.getId());
- exam.showTest();
- }
- }