日期:2014-05-20  浏览次数:20587 次

this的用法和含义```
只知道this在方法中是指隐式参数,还可以调用类中的其他构造函数,但是如果在一个类名后,应该怎么理解?
如:Manager.this

------解决方案--------------------
this引用
(1)指代对象本身。
(2)访问本类的成员变量或成员方法。
如:this. <变量名>
this. <方法名>
(3)调用本类的构造方法

------解决方案--------------------
不能省略的地方主要有三个:
1、在自身的构造方法内部调用自身的其他构造方法
public Test(){
this(10);
}

public Test(int n){

}
2、代表自身类的对象
3、使用this.属性名引用被覆盖掉的属性

------解决方案--------------------
对于 Manager.this 这样的语句,它肯定在Manager这个类的内部,也许直接在Manager下,也许在Manager的内部类InManager中,也许在它内部类的内部类InInManager中.....对于这最后一种情况,使用Manager.this、InManager.this、InInManager.this都是可以的,当然InInManager.this==this

引用this的语句不能处于static上下文中
------解决方案--------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame
{
public test()
{
super( "Test " );

JButton b = new JButton( "Click ");
b.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
JOptionPane.showMessageDialog( test.this, "Just a test ", "test ", JOptionPane.PLAIN_MESSAGE );
}
});

getContentPane().add( b );
setSize( 300, 400 );
setVisible( true );
}
public static void main( String [] args )
{
test f = new test();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
---------------------------------
直接用this将传递一个匿名内置类(匿名内部类)ActionListener,用test.this则传递test的实例对象。
参考!!!!