简体   繁体   English

扩展抽象类的匿名类

[英]anonymous class extending an abstract class

I'm looking to schedule a task at a specified time startTime , and when that time comes, i want it to run a method rrun(boolean param1, Object someObj) . 我希望将任务安排在指定的时间startTime ,当该时间到来时,我希望它运行方法rrun(boolean param1, Object someObj)

rrun() is a method in class, say SomeClass . rrun()是在类中的方法,说SomeClass

And, within SomeClass again, i have a method m1() that does this as part of its code: 而且,再次在SomeClass ,我有一个方法m1()作为其代码的一部分来执行此操作:

    Timer startTimer = new Timer(); 


    startTimer.schedule(new TimerTask() {
                    public void run() {rrun(false, this);}
                }, 
            startTime);

To this, i'm getting the error that 为此,我得到的错误

incompatible types: <anonymous TimerTask> cannot be converted to SomeClass

What's wrong with what i'm doing? 我在做什么错了?

TIA. TIA。

//=============================== // ==============================

EDIT: 编辑:

I'm using JDK 8 -- the latest one 我正在使用JDK 8-最新的

Inside the anonymous inner class, this is a reference to the current TimerTask anonymous subclass, not the enclosing class. 在匿名内部类内部, this是对当前TimerTask匿名子类的引用,而不是对封闭类的引用。

To refer to the enclosing class, qualify this : 要引用封闭类,请this限定:

public void run() {rrun(false, SomeClass.this);}

We need more code than this. 我们需要更多的代码。 What does rrun take as arguments? rrun将哪些作为参数?

My guess is you want this though: 我的猜测是您想要这个:

public void run() {rrun(false, SomeClass.this);}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM