Java中Class.this和this的区别
在Java中, Class.this 和 this 可能指代相同或不同的对象,具体取决于用法。
这
this is a reference variable that refers to the current object. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.
类.this
Class.this is a reference variable that refers to the outer class object inside a nested class or anonymous methods. If there is ambiguity between the inner (or anonymous) class and outer class, Class.this resolves the problem of ambiguity.
如果没有嵌套(或匿名)类,则 this 和Class.this引用同一个对象。
示例代码
考虑下面给出的代码,其中 this 和 Class.this 都指向同一个对象。
Java
class Outer
{
public void showOuter()
{
System.out.println ("this = " + this);
System.out.println ("Outer.this = " + Outer.this);
}
public static void main (String[] args)
{
Outer outer = new Outer();
outer.showOuter();
}
}
Java
class Outer
{
class Inner
{
public void showOuter()
{
System.out.println ("Outer.this = " + Outer.this);
}
public void showInner()
{
System.out.println ("this = " + this);
}
}
public static void main (String[] args)
{
Outer outer = new Outer();
Inner inner = outer.new Inner();
inner.showOuter();
inner.showInner();
}
}
Java
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class GFG
{
public void createAndFireButton()
{
JButton button = new JButton();
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println ("GFG.this = " + GFG.this);
System.out.println ("this = " + this);
}
});
button.doClick();
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.createAndFireButton();
}
}
Java
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class GFG
{
public void createAndFireButton()
{
JButton button = new JButton();
button.addActionListener(e -> {
System.out.println ("GFG.this = " + GFG.this);
System.out.println ("this = " + this);
});
button.doClick();
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.createAndFireButton();
}
}
this = Outer@b4c966a
Outer.this = Outer@b4c966a
在下面给出的代码中,添加了一个内部嵌套类。在这个例子中, this 和 Outer.this 指向不同的对象。
Java
class Outer
{
class Inner
{
public void showOuter()
{
System.out.println ("Outer.this = " + Outer.this);
}
public void showInner()
{
System.out.println ("this = " + this);
}
}
public static void main (String[] args)
{
Outer outer = new Outer();
Inner inner = outer.new Inner();
inner.showOuter();
inner.showInner();
}
}
Outer.this = Outer@2f4d3709
this = Outer$Inner@4e50df2e
在下面给出的代码中,已经演示了 class.this 在匿名方法中的用法。在这个例子中,class.this 和 this 也指向不同的对象。出于演示的目的,使用了 JButton 并调用了它的 doClick() 方法。
Java
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class GFG
{
public void createAndFireButton()
{
JButton button = new JButton();
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println ("GFG.this = " + GFG.this);
System.out.println ("this = " + this);
}
});
button.doClick();
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.createAndFireButton();
}
}
GFG.this = GFG@1fbc7afb
this = GFG$1@45c8e616
Note: Please note that when using lambda expressions, class.this and this both refer to the same outer class reference.
在下面给出的示例中,我们没有使用匿名类,而是使用 lambda 向 JButton 添加动作侦听器。
Java
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class GFG
{
public void createAndFireButton()
{
JButton button = new JButton();
button.addActionListener(e -> {
System.out.println ("GFG.this = " + GFG.this);
System.out.println ("this = " + this);
});
button.doClick();
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.createAndFireButton();
}
}
GFG.this = GFG@1fbc7afb
this = GFG@1fbc7afb