Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在方法内部重写Runnable?_Java_Overriding_Actionlistener_Extend - Fatal编程技术网

Java 如何在方法内部重写Runnable?

Java 如何在方法内部重写Runnable?,java,overriding,actionlistener,extend,Java,Overriding,Actionlistener,Extend,请就以下问题给我一个建议 我有A班和B班 如何在类B中的方法foo中重写Runnable class A { //some code ....... protected void foo() { //some code ....... //adding click listener to instance of MyButton myButton.show(new Runna

请就以下问题给我一个建议

我有A班和B班 如何在类B中的方法foo中重写Runnable

class A {    
    //some code
    .......    
    protected void foo() {
        //some code
        .......         
        //adding click listener to instance of MyButton
        myButton.show(new Runnable(){
            @Override
            public void run() {
                .......
            }
        });         
        //some code
        .......
    }    
    //some code
    .......    
}

class B extends A {    
    @Override
    protected void foo() {
        super.foo();            
        //some NEW code
        .......         
        //adding click listener to instance of MyButton
        myButton.show(new Runnable(){
            @Override
            public void run() {
                //Copied&Pasted old code
                .......                 
                //NEW code
                .......
            }
        });
    }

}

我是否可以向按钮的处理程序添加新代码(在myButton中可运行),而无需从super复制和粘贴现有代码?如何使用?

如果要重新使用逻辑,必须使用命名类实例而不是匿名类实例

例如:

class A {
    ...
    static class ButtonLogic implements Runnable
    {
        public void run() {...}
    }

    protected void foo() {
        //adding click listener to instance of MyButton
        myButton.show(new A.ButtonLogic());
            .......
    }
}
然后B可以覆盖该逻辑:

class B extends A {

    @Override
    protected void foo() {
        super.foo();

        //some NEW code
        .......

        //adding click listener to instance of MyButton
        myButton.show(new A.ButtonLogic(){
            @Override
            public void run() {
                super.run();
                .......

                //NEW code
                .......
            }
        });


    }

}

mybutton的访问说明符是什么?@Anton在这种情况下,您不能覆盖该Runnable。