class Demo extends Thread{
private String name;
Demo(String name) {
this.name = name;
}
public void run() {
for(int i = 0; i <= 20; i++) {
for(int j = -99999; j <= 99999; j ++){}
System.out.println(name+Thread.currentThread().getName());
}
}
}
class test5 {
public static void main(String[] args) {
//Thread t = new Thread();
Demo d1 = new Demo("CSDN222222");
Demo d2 = new Demo("CSDN111111");
d1.start();
System.out.println("哈哈"+Thread.currentThread().getName());
d2.start();
}
}
1,为什么可以在main()函数中,直接调用“Thread.currentThread().getName()”这个方法呢。我知道currentThread()这个方法是静态的,但是getName()不是静态的啊。难道直接(类名.静态方法.非静态方法)可以实现?
2,为什么Thread类可以直接用呢?
谢谢各位大大。新手问题没水准,多包涵。
currentThread()是静态方法没错,但是它返回了一个对象实例,这个对象实例可以调用成员方法,好比:
class A
{
static public A GetInstanceOfA() { return new A(); }
public void foo();
}
你就可以写
A.GetInstabceOfA().foo();