一、封装的优化1——this关键词
this的含义
this代表所在类的当前对象的引用(地址值),即对象自己的引用。
记住 :方法被哪个对象调用,方法中的this就代表那个对象。即谁在调用,this就代表谁.
this使用格式
this.成员变量名;
使用 this 修饰方法中的变量,解决成员变量被隐藏的问题,代码如下:
小贴士:方法中只有一个变量名时,默认也是使用this 修饰,可以省略不写
地址比较
package KeTang;
public class Student2 {
public static void main(String[] args) {
Student st = new Student();
System.out.println("main:"+st);//main:KeTang.Student@1540e19d
st.setSage(10);//setSage:KeTang.Student@1540e19d
int sage = st.getSage();
System.out.println(sage);
Student xl = new Student();
xl.setSage(-26);