..................................... 新学Java的,不会因为这而气死。虫子有创意,but i'm very sorry: non-static variable this cannot be referenced from a static context this.f2();//如果不注释掉这句,编译通不过: ^
public static class ThisClass9 { public static void f1() {//如果没有关键字 static ,编译出错: //non-static method f1() cannot be //referenced from a static context
//f2()一样。 f2(); this.f2();//如果不注释掉这句,编译通不过: //non-static variable this cannot //be referenced from a static context } public static void f2() { System.out.println("2nd functiong"); } public static void main( String args[] ){ System.out.println("End!"); } }
这个方法在编译的时候就会执行
而你这样this.f2();是一种动态的调用,在静态的方法里面是不可以有动态的调用的
同样,f1()是static,而this涉及到具体实例,所以不能引用this.
public void f1() {//如果没有关键字 static ,编译出错:
//non-static method f1() cannot be
//referenced from a static context
//f2()一样。
f2();
this.f2();//如果不注释掉这句,编译通不过:
//non-static variable this cannot
//be referenced from a static context
}
public void f2() {
System.out.println("2nd functiong");
}
public static void main( String args[] ){
f1();
f2();
System.out.println("End!");
}
}
这样?编译通不过啊!~你按题目写一个给我看看吧!谢谢了~
public static void f1() {
f2();
f2();
}
public static void f2() {
System.out.println("2nd functiong");
}
public static void main( String args[] ){
f1();
f2();
System.out.println("End!");
}
}
public void f1() {//如果没有关键字 static ,编译出错:
//non-static method f1() cannot be
//referenced from a static context
//f2()一样。
f2();
this.f2();//如果不注释掉这句,编译通不过:
//non-static variable this cannot
//be referenced from a static context
}
public void f2() {
System.out.println("2nd functiong");
}
public static void main( String args[] ){
ThisClass9 t = new ThisClass9(); //我加的
t.f1();
t.f2();
System.out.println("End!");
}
}ok?呵呵
'''
(0 0)
+-----oOO----(_)------------+
| |
| 有感而发! |
| |
| |
+------------------oOO------+
|__|__|
|| ||
ooO Ooo
新学Java的,不会因为这而气死。虫子有创意,but i'm very sorry:
non-static variable this cannot be referenced from a static context
this.f2();//如果不注释掉这句,编译通不过:
^
public static void f1() {//如果没有关键字 static ,编译出错:
//non-static method f1() cannot be
//referenced from a static context
//f2()一样。
f2();
this.f2();//如果不注释掉这句,编译通不过:
//non-static variable this cannot
//be referenced from a static context
}
public static void f2() {
System.out.println("2nd functiong");
}
public static void main( String args[] ){
System.out.println("End!");
}
}
不是你写的函数有问题,是你调用的问题。请赐教 '''
(0 0)
+-----oOO----(_)------------+
| |
| 有感而发! |
| |
| |
+------------------oOO------+
|__|__|
|| ||
ooO Ooo
//*使用this,第二次调用时使用this。你们别看我写的那个了,根据上面的条件给我个class. OK?
public static int i = 1;
public void f1() {
System.out.println("used the 1st function!");
f2();
this.f2();
}
public void f2() {
System.out.println("used the 2nd function! " + i + "time.");
i++;
}
public static void main( String args[] ){
TestClass t = new TestClass();
t.f1();
t.f2();
System.out.println("ok? my friend?! he he...");
}
}
'''
(0 0)
+-----oOO----(_)------------+
| |
| 有感而发! |
| |
| |
+------------------oOO------+
|__|__|
|| ||
ooO Ooo