简体   繁体   English

从抽象类返回Java上的通用对象

[英]Returning a generic object on Java from abstract class

I have a class that should accept different datatypes as the second constructor parameter: 我有一个应该接受不同数据类型作为第二个构造函数参数的类:

public abstract class QueryMatch {
String key;
Object input;

public <T> QueryMatch(String key, T o) {
    this.key = key;
    input = o;
}

public String getKey() {
    return key;
}

public Object getValue() {
    return input;
}
}

I don't want to use type parameters, like 我不想使用类型参数,例如

public abstract class QueryMatch<T>{
String key;
T input;
...

As this way I'm getting raw types warnings when declaring retrieving QueryMatch as a generic (as I don't know the datatype it contains). 这样,当我声明检索QueryMatch为泛型时会收到原始类型警告(因为我不知道它包含的数据类型)。 But the problem is that I need to return the value and I'm not totally comfortable by returning an Object (is that just me, but it doesn't seem like a good practice?). 但是问题是我需要返回值,而返回一个对象并不令我完全满意(仅仅是我一个人,但这似乎不是一个好习惯吗?)。

Additionally, another class inherits from it: 此外,另一个类继承自它:

public class QueryMatchOr extends QueryMatch {
public QueryMatchOr() {
    super("title", new ArrayList<String>());
}

public void addMatch(String match) {
    ((ArrayList<String>) input).add(match);
}

}

And of course I'm getting a Unchecked cast warning (which I can avoid with @SuppressWarnings(“unchecked”)). 当然,我得到了一个未选中的强制警告(可以使用@SuppressWarnings(“ unchecked”)避免它)。

So, my question is... is there a better way to achieve what I'm trying to do? 所以,我的问题是...是否有更好的方法来实现我的目标? An abstract class that contains an object (which could be bounded), and returning the datatype it contains (instead of an Object) without using a type parameter in the class declaration? 一个抽象类,它包含一个对象(可以绑定),并返回其包含的数据类型(而不是Object),而无需在类声明中使用类型参数?

So first, I think the best answer is to make your class generic. 所以首先,我认为最好的答案是使您的类通用。 But if you really don't want to do this you could do something like this: 但是,如果您真的不想这样做,则可以执行以下操作:

public <T> T getValue(Class<T> type) {
    return (T)input;
}

In some way you need to provide the expected type for the return value to the class. 您需要以某种方式为类提供返回值的预期类型。 This can either be done my making that class generic or the method generic. 这可以通过使该类通用或使该方法通用来完成。

What you are doing is not a good design. 您正在做的不是一个好的设计。 You are using an Object type field from the superclass while you only can know it's actual (needed) type in the subclass. 您正在使用超类中的“ Object类型”字段,而您只能在子类中知道它的实际(所需)类型。 If you only know that in the subclass, declare that variable in the subclass. 如果仅在子类中知道该变量,则在子类中声明该变量。 Not even to mention that your fields are not private. 更不用说您的字段不是私有的。

How about: 怎么样:

public abstract class QueryMatch {

    private String key;

    public QueryMatch(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public abstract void addMatch(String match);
}


public class QueryMatchOr extends QueryMatch {

    private ArrayList<String> input;

    public QueryMatchOr() {
        super("title");
        input = new ArrayList<String>();
    }

    public void addMatch(String match) {
        input.add(match);
    }
}

If you need the getValue() method in the superclass, you really should make it generic: 如果您需要超类中的getValue()方法,则应该使它通用:

public abstract class QueryMatch<T> {

    private String key;

    public QueryMatch(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public abstract void addMatch(String match);

    public abstract T getValue();
}


public class QueryMatchOr extends QueryMatch<ArrayList<String>> {

    private ArrayList<String> input;

    public QueryMatchOr() {
        super("title");
        input = new ArrayList<String>();
    }

    public void addMatch(String match) {
        input.add(match);
    }

    public ArrayList<String> getValue(String match) {
        input;
    }
}

So, my question is... is there a better way to achieve what I'm trying to do? 所以,我的问题是...是否有更好的方法来实现我的目标?

No, there isn't. 不,没有。

I think you should use generics instead of @SuppressWarnings(“unchecked”)) 我认为您应该使用泛型而不是@SuppressWarnings(“ unchecked”))

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

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