package test2;
/*
* 封装和private 的应用:
* A:把成员变量用private修饰
* B:提高对应的getXxx()个SetXxx的方法
*
*
* this : 当前类的对象引用
代表当前类的一个对象
*
* 记住:
* 方法被哪个对象调用,this就代表那个对象
* 什么时候使用this呢?
* 局部变量隐藏成员变量
* 其他用法后面和super一起讲解
*
*/
//定义学生类
class Student1
{
//姓名
private String name;
private int age;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age=age;
}
public void setName(String name)
{
//age=a;
this.name=name;
}
public String getname()
{
return name;
}
}
public class PrivateDemo {
public static void main(String[] args)
{
//创建学生对象
Student1 s =new Student1();
s.setAge(27);
s.setName("林青霞");
System.out.println(s.getname()+"----"+s.getAge());
}
}
this 关键字
最新推荐文章于 2019-01-16 00:08:04 发布