简体   繁体   English

Java - 如何将方法的返回类型作为参数传递?

[英]Java - How do I pass the return type of a method as parameter?

I'm trying to make a small game with a TUI. 我正试图用TUI做一个小游戏。 The TUI should have a get() method that should take a String question and a return type as parameter, so you can use it to request an object. TUI应该有一个get()方法,该方法应该将String question和返回类型作为参数,因此您可以使用它来请求对象。

For example, get("What is your name?", String) should return a String and get("What color do you want?", Color) should return a Color . 例如, get("What is your name?", String)应该返回一个Stringget("What color do you want?", Color)应该返回一个Color

This is what I've come up with: 这就是我想出来的:

public interface View {

    public <T> T get(String question, Class<T> type) throws InputException;

}

public class TUI implements View {

    public Color get(String question, Class<Color> type) {
        // code
    }

    public String get(String question, Class<String> type) {
        // code
    }

}

Color color = Game.getView().get("What piece do you want to place?", Color.class);

Unfortunately, the methods in TUI are not accepted as implementations of public <T> T get(String question, Class<T> type) throws InputException; 不幸的是, TUI中的方法不被接受为public <T> T get(String question, Class<T> type) throws InputException; . Is there any way I can do this? 有什么方法可以做到这一点吗?

Keeping it type safe by splitting the functions would seem more appropriate: 通过拆分函数保持类型安全似乎更合适:

Color color = Game.getView().getColor("What piece do you want to place?");
String string = Game.getView().getString("What piece do you want to place?");

Try to make two classes AskColor and AskString that implements View, with method respectively 尝试分别使用方法创建两个实现View的AskColor和AskString类

  public Color get(String question, Class<Color> type) {
    // code
}

public String get(String question, Class<String> type) {
    // code
}

and the interface View with method 以及使用方法查看的界面

public T get(String question, Class<T> type) throws InputException;

Your TUI class is not valid because under type erasure both get methods have the same arguments. 您的TUI类无效,因为在类型擦除下,两个get方法都具有相同的参数。

You might want to do this: 你可能想这样做:

public interface View<T> {
    T get(String question, Class<T> type) throws InputException;
}

public class ColorTUI implements View<Color> {
    public Color get(String question, Class<Color> type) {
        // code
    }
}

public class StringTUI implements View<String> {
    public String get(String question, Class<String> type) {
        // code
    }
}

You can use something similar to Template Method Design Pattern. 您可以使用类似于模板方法设计模式的内容。

public interface TypeTemplate {
    public Object get(String question);
}
public class ColorTemplate implements TypeTemplate {
    @Override public Color get(String question) { /*code for color*/ }
}
public class StringTemplate implements TypeTemplate {
    @Override public String get() { /*code for string*/ }
}

public class TUI {
    public Color get(String question, ColorTemplate templ) {
        return templ.get(String question)
    }
    public String get(String question, StringTemplate templ) {
        return templ.get(String question);
    }
}



public static void main(String args[]) {
    TUI tui = new TUI();
    String string = tui.get("What is your name?", new StringTemplate());
    Color color = tui.get("What color do you want?", new ColorTemplate());
}

You can of course replace Object type in TypeTemplate with something less generic. 当然,你可以用不太通用的东西替换TypeTemplate Object类型。 You can also make TypeTemplate one abstract class; 您还可以将TypeTemplate一个抽象类; this way if all (or some) of your get() methods will have to share some code you can add methods in this class and call from the subclasses. 这样,如果你的所有(或某些) get()方法必须共享一些代码,你可以在这个类中添加方法并从子类调用。 eg 例如

public abstract class TypeTemplate {
    public abstract Object get(String question);
    protected void init() {/*some initialization code*/} 
    protected void cleanup(){/*some more code*/}
}
public class ColorTemplate implements TypeTemplate {
    @Override
    public Color get(String question) {
        init();
        /*code for color*/
        cleanup(); 
    }
}
public class StringTemplate implements TypeTemplate {
    @Override
    public String get() {
        init();
        /*code for string*/
        cleanup(); 
    }
}

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

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