当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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.

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


相关用法


注:本文由纯净天空筛选整理自anujanonymous大神的英文原创作品 Difference Between Class.this and this in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。