在 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.
Class.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();
}
}
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
相关用法
- Java ClassLoader.clearAssertionStatus()用法及代码示例
- Java ClassLoader.getSystemResource()用法及代码示例
- Java Class asSubclass()用法及代码示例
- Java Class getSuperClass()用法及代码示例
- Java Class isAnonymousClass()用法及代码示例
- Java Class isPrimitive()用法及代码示例
- Java Class newInstance()用法及代码示例
- Java ClassLoader clearAssertionStatus()用法及代码示例
- Java ClassLoader definePackage()用法及代码示例
- Java ClassLoader findClass()用法及代码示例
- Java ClassLoader findLibrary()用法及代码示例
- Java ClassLoader findLoadedClass()用法及代码示例
- Java ClassLoader findResource()用法及代码示例
- Java ClassLoader findResources()用法及代码示例
- Java ClassLoader findSystemClass()用法及代码示例
- Java ClassLoader getPackage()用法及代码示例
- Java ClassLoader getPackages()用法及代码示例
- Java ClassLoader getParent()用法及代码示例
- Java ClassLoader getResource()用法及代码示例
- Java ClassLoader getResourceAsStream()用法及代码示例
- Java ClassLoader getResources()用法及代码示例
- Java ClassLoader getSystemClassLoader()用法及代码示例
- Java ClassLoader getSystemResource()用法及代码示例
- Java ClassLoader getSystemResourceAsStream()用法及代码示例
- Java ClassLoader getSystemResources()用法及代码示例
注:本文由纯净天空筛选整理自anujanonymous大神的英文原创作品 Difference Between Class.this and this in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。