English 中文(简体)
以相同方法从超级扩展类运行( Java)
原标题:Extended class running from super via same method (Java)

我知道这个标题很混乱,但我无法想出另一种措辞方式。

So, what I am trying to achieve: I have seen some java programs have a load of subclasses that have a method in their super class, and they all run when the supers method is called. What I am trying to do is make ever single child class of Synchroniser run their own RunAll method when I call the Synchroniser.RunAll method.

我尝试过很多事情,我经常去谷歌,但我发现的东西不适合我。

这就是我有的: 超优等班

public class Synchroniser 
{
    public Synchroniser()
    {
        RunAll();
    }

    protected void RunAll()
    {
        System.out.println("RunAll");
    }
}

小组委员会:

import org.lwjgl.input.Keyboard;

public class ArrayList extends Synchroniser
{
    public ArrayList()
    {

    }

    public static void Keybind(Info info)
    {
        info.Toggle();
    }

    @Override
    protected void RunAll()
    {
        System.out.println("Ran ArrayList");
    }
}
问题回答

似乎您正在寻找“ 坚固的” 观察器模式 < / 坚固 ” 。 您是否对此进行了谷歌? 或者“ 坚固的” 板板方法 < / 坚固 ”?

您的代码不匹配( 这可能是为什么它不起作用), 但您对问题的描述却符合 。

UPDATE
Bases on your latest comment, @Luchian was the right one. Here is a simple example applied to your use case - ps: not a good idea to use existing JDK classes as names for your own classes (ArrayList):

public class Test {

    public static void main(String[] args) {
        ArrayList child1 = new ArrayList(1);
        ArrayList child2 = new ArrayList(2);
        Synchronizer sync = new Synchronizer(); //prints RunAll in Parent
        sync.addListener(child1);
        sync.addListener(child2);
        sync.runAll(); //prints RunAll in Parent, in Child 1 and in Child 2
    }

    public static interface RunAll {

        void runAll();
    }

    public static class Synchronizer implements RunAll {

        List<RunAll> listeners = new CopyOnWriteArrayList<RunAll>();

        public Synchronizer() {
            runAll();
        }

        public void addListener(RunAll l) {
            listeners.add(l);
        }

        @Override
        public void runAll() {
            System.out.println("RunAll in Parent");
            for (RunAll l : listeners) {
                l.runAll();
            }
        }
    }

    public static class ArrayList implements RunAll {
        private final int i;

        private ArrayList(int i) {
            this.i = i;
        }

        @Override
        public void runAll() {
            System.out.println("RunAll in Child " + i);
        }
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签