TestStudent.java:
package cn.itcast.demo4;
public class TestStudent {
public static void main(String[] args) {
//需求1: 调用show()方法
Student stu = new Student();
stu.show();
System.out.println("--------------");
//需求2: 按照格式, 优化setAge()中的参数名, 并调用.
//设置年龄
stu.setAge(50);
//获取年龄
System.out.println(stu.getAge());
}
}
Student.java:
package cn.itcast.demo4;
/*
定义一个学生类, 用来演示this关键字
*/
public class Student {
//成员变量
private int age = 20;
//设置年龄
public void setAge(int age) { // age = 50;
this.age = age;
}
//获取年龄
public int getAge() {
return age;
}
//为了让你更好的理解this的作用, 先做一个测试题.
public void show() {
//局部变量
int age = 10;
System.out.println(age); //10
System.out.println(this.age); //20 ?
}
}