抽象类
abstract class A {
protected static Logger log = AppContainer.getInstance().getLogger();
}子类
class B extends A {
private Timer t;
public B() {
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
try {
....
} catch(Exception e) {
B.log.error(...);
}
}
}
}
}
这里的子类中的内部类应该如何使用父类的静态变量?
abstract class A {
protected static Logger log = AppContainer.getInstance().getLogger();
}子类
class B extends A {
private Timer t;
public B() {
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
try {
....
} catch(Exception e) {
B.log.error(...);
}
}
}
}
}
这里的子类中的内部类应该如何使用父类的静态变量?
后来我在子类也加了log变量才解决了,但是觉得那样程序不漂亮,东西继承不下来
class B extends A {
private Timer t;
public B() {
t=new Timer();
t.schedule(new TT(this));
}
class TT extends TimerTask {
B b;
public TT(B b) {
this.b=b;
}
public void run() {
try {
...
} catch(Exception e) {
b.log.error(...);
}
}
}
}
private Timer t;
public B() {
t=new Timer();
t.schedule(new TT(this));
}
class TT extends TimerTask {
B b;
public TT(B b) {
this.b=b;
}
public void run() {
try {
...
} catch(Exception e) {
b.log.error(...);
}
}
}
}
protected Logger log;
public A(){
log = AppContainer.getInstance().getLogger();
}}class B extends A {
private Timer t;
public B() {
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
try {
....
} catch(Exception e) {
B.this.log.error(...);
}
}
}
}
}
你的例子应该A.log 就可以了