公用类? 我把代码贴上来吧。 import java.util.*;abstract class Instrument4{ public static int i; public abstract void play(); }public class m{ public static int i; public static void main(String[] args){ Instrument4[] g = new Instrument4[2]; i = 6; int j = 0; g[j++] = new w(); g[j++] = new q(); tuneAll(g); } static void tune(Instrument4 e){ e.play(); } static void tuneAll(Instrument4[] f){ for (int j = 0; j < f.length; j++) tune(f[j]); } }class q extends Instrument4{ public void play(){ i = 4; System.out.println("i" + i); } }class w extends Instrument4{ public void play(){ System.out.println(i); } }我要达到的效果是在类m中向i赋值
g[j++] = new w();g[j].i = this.i;g[j++] = new q(); tuneAll(g);你并没有赋值
抽象类?
子类?
另一个类?
在其它类中引入公用类,即可使用公用的变量
我把代码贴上来吧。
import java.util.*;abstract class Instrument4{
public static int i;
public abstract void play();
}public class m{
public static int i;
public static void main(String[] args){
Instrument4[] g = new Instrument4[2];
i = 6;
int j = 0;
g[j++] = new w();
g[j++] = new q();
tuneAll(g);
}
static void tune(Instrument4 e){
e.play();
}
static void tuneAll(Instrument4[] f){
for (int j = 0; j < f.length; j++)
tune(f[j]);
}
}class q extends Instrument4{
public void play(){
i = 4;
System.out.println("i" + i);
}
}class w extends Instrument4{
public void play(){
System.out.println(i);
}
}我要达到的效果是在类m中向i赋值
tuneAll(g);你并没有赋值
破thinking in java。
那书里的原话:“下面是抽象方法声明时采用的语法:
abstract void x();”
我还以为抽象类中的抽象方法不能带参数,也不能返回值呢。
看了别人的例子才知道,既可以带参数,也可以返回值。
7456!
这一上午浪费的,没意义。