Student.this和new Student()区别

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/30 01:15:59

Student.this和new Student()区别

new Student()是一个Student类的新的实例,并进行了初始化操作.
Student.this是一个Student实例的引用,并没有进行初始化,正确的说,他早已进行了初始化.
Student.this的应用与this不太一样.
举个.this的应用例子:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyApplet extends Applet{
public void init(){
Button b=new Button("print");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
MyApplet.this.print(e);
/*!看到吧,想要调用print(),就可以这样.
我的例子还不太到位,因为直接print(e);也可以.
但是假如ActionEvent也有print方法,而我想掉用MyApplet的print就可以这样用;还有假如我需要调用一个方法为print(MyApplet m);,他需要将MyApplet实例作为参数传进去,也可以这样用print(MyApplet.this);.
*/
}
});
setLayout(new BorderLayout());
add(b,"Center");
}
public void print(ActionEvent e){
this.showStatus(String.valueOf(e.getWhen()));
}
}
也就是说.this应用于同一文件内的类或内部类或匿名内部类,一般是不需要这样用的.
说了这么多不知道你明白了吗,这个用法不常用,不明白也不是很碍事,到了必须要用他的时候自然会了解.