java虚拟机管理调用的时候除了知道调用哪个方法,还得知道是调用哪个对象对不对??一个类可以有多个对象,对象ABCDEFG,你调用的时候就要告诉虚拟机,要调用的是A对象的方法,或者属性。如何告诉虚拟机呢??就是传递一个A对象的引用给虚拟机,而 this 关键字就可以访问这个引用。所以我们可以通过this关键字访问到当前对象的任一方法或者属性。(static除外,那是独立于对象的)
public void test(){ this.name="name"; this.thisPrint();//调用对象自身方法 } public static void main(String[] args){ TestThis t=new TestThis(); t.test(); } }其中要补充的就是this("that");//调用构造函数TestThis(String name)当然这种用法不常有,我想this简单的就是这样的用法。要注意的是在static方法中不能使用this调用属性和方法,类还没有实例化,哪来的this指代对象本事,“皮之不存毛将焉附”?
1,当类成员和方法中的成员变量具有相同的名称时需要利用this关键字指定类成员:class AA{ String name; public AA(String name){ this.name=name; //如果不写this那肯定出错编译都通不过. } }2, this用在构造方法中,调用本类中的构造方法. public class AA { String name; public AA(){ System.out.println("1无参构造....."); } public AA(String name){ this(); this.name=name; System.out.println("2有参构造....."+name); } public static void main(String[] args) { // TODO Auto-generated method stub AA a = new AA("author"); } }3, 代表调用对象本身. public class AA { String name; public AA(){ System.out.println("1无参构造....."); System.out.println(this); // 打印的是对象a的内存地址. } public void play(){ System.out.println("游戏人生........"); } public static void main(String[] args) { // TODO Auto-generated method stub AA a = new AA(); } } 再举一个例子:假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数:class Container { Component comp; public void addComponent() { comp = new Component(this); } } class Component { Container myContainer; public Component(Container c) { myContainer = c; } }
public class Test{
int i; public void setI(int i){
this.i = i;
}
}
所有static函数,不会传这个参数。
this用在非static方法中,是自己的引用,主要作用是为了方法提示。this然后打个点。就可以调到自己的方法和从父类继承的方法了。
this("that");//调用构造函数TestThis(String name)
}public TestThis(String name){
this.name=name;//调用对象自身属性
}public static void thisPrint(){
System.out.println(name);
}
public void test(){
this.name="name";
this.thisPrint();//调用对象自身方法
}
public static void main(String[] args){
TestThis t=new TestThis();
t.test();
}
}其中要补充的就是this("that");//调用构造函数TestThis(String name)当然这种用法不常有,我想this简单的就是这样的用法。要注意的是在static方法中不能使用this调用属性和方法,类还没有实例化,哪来的this指代对象本事,“皮之不存毛将焉附”?
String name;
public AA(String name){
this.name=name; //如果不写this那肯定出错编译都通不过.
}
}2, this用在构造方法中,调用本类中的构造方法.
public class AA {
String name;
public AA(){
System.out.println("1无参构造.....");
}
public AA(String name){
this();
this.name=name;
System.out.println("2有参构造....."+name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AA a = new AA("author");
}
}3, 代表调用对象本身.
public class AA {
String name;
public AA(){
System.out.println("1无参构造.....");
System.out.println(this); // 打印的是对象a的内存地址.
}
public void play(){
System.out.println("游戏人生........");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AA a = new AA();
}
}
再举一个例子:假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数:class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this);
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}
所有static函数,不会传这个参数
this表示的是当前