public class Test3
{
public static void main(String [] args)
{
PrintChar printA = new PrintChar('a',100);
PrintChar printB = new PrintChar('b',100);
PrintNum print100 = new PrintNum(100);
print100.start();
printA.start();
printB.start();
}
class PrintChar extends Thread
{
private char charToPrint;
private int times;
public PrintChar(char c,int t)
{
charToPrint=c;
times=t;
}
public void run()
{
for(int i=1;i<times;i++)
System.out.print(charToPrint);
}
}
class PrintNum extends Thread
{
private int lastNum;
public PrintNum(int n)
{
lastNum=n;
}
public void run()
{
for(int i=1;i<lastNum;i++)
System.out.print(" "+i);
}
}
}--------------------Configuration: Test - j2sdk1.4.2 <Default> - <Default>--------------------
D:\JavaProject\Test\Test3.java:5: non-static variable this cannot be referenced from a static context
PrintChar printA = new PrintChar('a',100);
^
D:\JavaProject\Test\Test3.java:6: non-static variable this cannot be referenced from a static context
PrintChar printB = new PrintChar('b',100);
^
D:\JavaProject\Test\Test3.java:7: non-static variable this cannot be referenced from a static context
PrintNum print100 = new PrintNum(100);
^
3 errorsProcess completed.这样的报错如何,我明明创建了一个可运行对象了,也就是printA,printB,print100了,而且也有类PrintChar和PrintNum了,他们都继承了类Thread的,为什么还有这样的报错...20分求解,希望大家帮忙.求学中.......
{
public static void main(String [] args)
{
PrintChar printA = new PrintChar('a',100);
PrintChar printB = new PrintChar('b',100);
PrintNum print100 = new PrintNum(100);
print100.start();
printA.start();
printB.start();
}
class PrintChar extends Thread
{
private char charToPrint;
private int times;
public PrintChar(char c,int t)
{
charToPrint=c;
times=t;
}
public void run()
{
for(int i=1;i<times;i++)
System.out.print(charToPrint);
}
}
class PrintNum extends Thread
{
private int lastNum;
public PrintNum(int n)
{
lastNum=n;
}
public void run()
{
for(int i=1;i<lastNum;i++)
System.out.print(" "+i);
}
}
}--------------------Configuration: Test - j2sdk1.4.2 <Default> - <Default>--------------------
D:\JavaProject\Test\Test3.java:5: non-static variable this cannot be referenced from a static context
PrintChar printA = new PrintChar('a',100);
^
D:\JavaProject\Test\Test3.java:6: non-static variable this cannot be referenced from a static context
PrintChar printB = new PrintChar('b',100);
^
D:\JavaProject\Test\Test3.java:7: non-static variable this cannot be referenced from a static context
PrintNum print100 = new PrintNum(100);
^
3 errorsProcess completed.这样的报错如何,我明明创建了一个可运行对象了,也就是printA,printB,print100了,而且也有类PrintChar和PrintNum了,他们都继承了类Thread的,为什么还有这样的报错...20分求解,希望大家帮忙.求学中.......
在main方法中先new一个Test3的实例。
PrintChar printA = test.new PrintChar('a', 100);
PrintChar printB = test.new PrintChar('b', 100);
PrintNum print100 = test.new PrintNum(100);或者把main中的代码放到构造中去
public Test3(){
PrintChar printA = new PrintChar('a', 100);
PrintChar printB = new PrintChar('b', 100);
PrintNum print100 = new PrintNum(100);
print100.start();
printA.start();
printB.start();
}public static void main(String [] args){
new Test3();
}
public class Test3
{
public static void main(String [] args)
{
PrintChar printA = new PrintChar('a',100);
PrintChar printB = new PrintChar('b',100);
PrintNum print100 = new PrintNum(100);print100.start();
printA.start();
printB.start();
}
}//注意这个括号然后程序就可以通过了...为什么会这样?