简体   繁体   English

如何创建一个新类来处理Java中的其他两个类?

[英]How do I create a new class to process two other classes in Java?

I have two classes called "J48Tree" and "RandomTree". 我有两个类,分别为“ J48Tree”和“ RandomTree”。 They are both subclasses of "AbstractTree". 它们都是“ AbstractTree”的子类。

"J48Tree" and "RandomTree" have a method called "graph()", other subclasses of "AbstractTree" don't have this method. “ J48Tree”和“ RandomTree”具有称为“ graph()”的方法,“ AbstractTree”的其他子类没有此方法。

These classese above and the graph() method come from exsiting API. 以上这些类和graph()方法来自现有的API。 So I dont need to rewrite them. 所以我不需要重写它们。

Now I want to create a new class called “Visualization”. 现在,我想创建一个名为“ Visualization”的新类。 This class can receive "J48Tree" and "RandomTree" as genetic input data. 此类可以接收“ J48Tree”和“ RandomTree”作为遗传输入数据。 And then run their "graph()" method. 然后运行他们的“ graph()”方法。

I tried 我试过了

public class Visualization  <T super J48Tree>

and

public class Visualization  <T extends AbstractTree>

But it doesn't work. 但这是行不通的。

How do I solve this problem? 我该如何解决这个问题?

Thanks in advance. 提前致谢。

You can have a look at the strategy design pattern: http://www.tutorialspoint.com/design_pattern/strategy_pattern.htm 您可以看一下策略设计模式: http : //www.tutorialspoint.com/design_pattern/strategy_pattern.htm

Look apt for the scenario you mentioned, since the behavior(method) need to be decided on runtime based on input object passed. 寻找适合您提到的场景,因为需要根据传递的输入对象在运行时确定行为(方法)。

As Florian says, you want to create an interface Graphable and make both J48Tree and RandomTree implement it. 正如Florian所说,您想创建一个Graphable接口,并使J48TreeRandomTree实现它。

public interface Graphable {
    void graph();
}

public class J48Tree extends AbstractTree implements Graphable {
    @Override
    public void graph() {
        // implementation
    }
}

public class RandomTree extends AbstractTree implements Graphable {
    @Override
    public void graph() {
        // implementation
    }
}

public class Visualization {
    private Graphable graphable;

    public void setGraphable(Graphable graphable) {
        this.graphable = graphable;
    }

    public void graph() {
        this.graphable.graph();
    }
}

Then, you can set the Graphable implementation on Visualization class by calling setGraphable . 然后,可以通过调用setGraphableVisualization类上设置Graphable实现。

EDIT 编辑

From your comment I understand you are not capable of making neither J48Tree nor RandomTree implement the Graphable interface (that's the only change on the classes you have to make). 根据您的评论,我知道您无法使J48TreeRandomTree实现Graphable接口(这是您必须对类进行的唯一更改)。 In this case, you should 'wrap' an instance of each class into another class, a wrapper, that implements Graphable . 在这种情况下,您应该将每个类的实例“包装”到另一个实现Graphable类(包装器)中。

public interface Graphable {
    void graph();
}

public class J48TreeWrapper implements Graphable {
    private final J48Tree j48Tree;

    public J48TreeWrapper(J48Tree j48Tree) {
        this.j48Tree = j48Tree;
    }

    @Override
    public void graph() {
        this.j48Tree.graph();
    }
}

public class RandomTreeWrapper implements Graphable {
    private final RandomTree randomTree;

    public RandomTreeWrapper(RandomTree andomTree) {
        this.randomTree = randomTree;
    }

    @Override
    public void graph() {
        this.randomTree.graph();
    }
}

public class Visualization {
    private Graphable graphable;

    public void setGraphable(Graphable graphable) {
        this.graphable = graphable;
    }

    public void graph() {
        this.graphable.graph();
    }
}

Now, you will first have to create an instance of J48TreeWrapper that 'wraps' a J48Tree , and then you call setGraphable using it (same with RandomTreeWrapper ). 现在,你必须首先创建一个实例J48TreeWrapper是“包装”一个J48Tree ,然后调用setGraphable使用它(与相同RandomTreeWrapper )。

public static void main(String[] args) {
    J48TreeWrapper wrapper = new J48TreeWrapper(new J48Tree());
    Visualization visualization = new Visualization();
    visualization.setGraphable(wrapper);
    visualization.graph();
}

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

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