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

哪位大侠了解JAVA的this用法?帮我看看这个程序
import java.applet.*;
import java.awt.*;

public class testThread4 extends java.applet.Applet implements Runnable {
int x = 0, y = 5;
Thread Scrollwords = new Thread(this);
//Scrollwords.start();
//Scrollwords = new Thread(this);
//testThread4 a=new testThread4();
public void init() {
setFont(new Font("TimesRoman", Font.BOLD, 16));
}

public void start() {
//if (Scrollwords == null) {
//Scrollwords = new Thread();
Scrollwords.start();
//}
}

public void run() {
while (Scrollwords != null) {
x = x + 1;
y = y + 1;
if (x > 200)
x = 0;
if (y > 100)
y = 10;
repaint();
try {
Scrollwords.sleep(20);
} catch (InterruptedException e) {
}
}
}

public void paint(Graphics g) {
g.drawString("here is big club", x, y);
}

/*public void stop() {
Scrollwords.yield();
Scrollwords = null;
}*/

}
上面的这段代码this不是表示当前对象吗?为什么把第五行的this换成new teseThread()就不行呢?哪位知道帮下忙了,脑袋都想爆了。先谢谢了

------解决方案--------------------
this 是当前对象,而new teseThread()新生成了另一个对象,完全不是一个东西。

你不信这样:
testThread4 a=new testThread4();
System.out.println(this == a);