class BounceThread extends Thread{ UpDown applet;//} 那个this就表明将自身传到bounceThread的对象中,然后在BounceThread的方法操纵的UpDown对象就是自己.
还是不大明白,用Runnable接口来实现的还可以理解: 但为什么sleep要用对象来调用呢?不加t.sleep的话就编译不过。 import java.awt.*; import java.applet.Applet;public class UpDown2 extends Applet implements Runnable{ static int RADIUS=20; static int x=30; public int y=30; Thread t; public void init(){ t=new Thread(this); t.start(); } public void paint(Graphics g){ g.setColor(Color.blue); g.drawOval(x-RADIUS,y-RADIUS,2*RADIUS,2*RADIUS); } public void run(){ int yDir=1;//the direction of the ball moves int incr=1;//the distance of the ball moves every time int sleepFor=10; while(true){ y+=(incr*yDir); repaint(); if(y-UpDown2.RADIUS<incr || y+UpDown2.RADIUS+incr>getSize().height yDir*=-1; try{ t.sleep(sleepFor);//为什么要加t?? } catch(InterruptedException e){} } } }
可是翁凯的视频中说的是: sleep是属于Thread类的静态函数,它不属于哪个对象。 就算有t1.sleep(1000)语句,线程t1也不睡觉,而是 由哪个线程执行了sleep函数,就由那个线程睡觉: 比如 public static void main(String [] args){ ..... t1.sleep(1000); ..... } 在上面的程序段中,线程t1并不睡觉,而是调用它的main线程去睡觉,和在sleep前面写了任何东西都没关系。最好就直接写成: Thread.sleep(1000); 我又看了遍翁凯的视频,有点明白了。差点被书上的例程搞糊涂了。关于第一个问题:thinking in java 中是这么说的: "The this keyword---which can be used only inside a method---produces the reference to the object the method has been called for.You can treat this reference just like any other object reference." this 的用途我是知道的。只是两个类中反复的调用让我迷糊了: 在UpDown类中: BounceThread b=new BounceThread(this);在Bounce Thread类中: UpDown applet;BounceThread(UpDown a){ this.applet=a; } 哪位能详细地解释一下?谢谢
UpDown applet;//}
那个this就表明将自身传到bounceThread的对象中,然后在BounceThread的方法操纵的UpDown对象就是自己.
但为什么sleep要用对象来调用呢?不加t.sleep的话就编译不过。
import java.awt.*;
import java.applet.Applet;public class UpDown2 extends Applet implements Runnable{
static int RADIUS=20;
static int x=30;
public int y=30;
Thread t;
public void init(){
t=new Thread(this);
t.start();
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.drawOval(x-RADIUS,y-RADIUS,2*RADIUS,2*RADIUS);
}
public void run(){
int yDir=1;//the direction of the ball moves
int incr=1;//the distance of the ball moves every time
int sleepFor=10;
while(true){
y+=(incr*yDir);
repaint();
if(y-UpDown2.RADIUS<incr ||
y+UpDown2.RADIUS+incr>getSize().height yDir*=-1;
try{
t.sleep(sleepFor);//为什么要加t??
}
catch(InterruptedException e){}
}
}
}
我自己的看法而已...
如果你只是写了sleep那么系统不知道你要调用
哪一个sleep,所以会出现错误。你可以查看一下几乎每个
类中都有sleep方法。
sleep是属于Thread类的静态函数,它不属于哪个对象。
就算有t1.sleep(1000)语句,线程t1也不睡觉,而是
由哪个线程执行了sleep函数,就由那个线程睡觉:
比如
public static void main(String [] args){
.....
t1.sleep(1000);
.....
}
在上面的程序段中,线程t1并不睡觉,而是调用它的main线程去睡觉,和在sleep前面写了任何东西都没关系。最好就直接写成:
Thread.sleep(1000);
我又看了遍翁凯的视频,有点明白了。差点被书上的例程搞糊涂了。关于第一个问题:thinking in java 中是这么说的:
"The this keyword---which can be used only inside a method---produces the reference to the object the method has been called for.You can treat this reference just like any other object reference."
this 的用途我是知道的。只是两个类中反复的调用让我迷糊了:
在UpDown类中:
BounceThread b=new BounceThread(this);在Bounce Thread类中:
UpDown applet;BounceThread(UpDown a){
this.applet=a;
}
哪位能详细地解释一下?谢谢
UpDown的一个对象作参数.
现在对面向对象的思想还有点欠缺,所以搞迷糊了。看来要多加练习啊