java - 泛型方法]在JAVA中扩展多种类型参数

标签 java generics type-parameter

我正在创建一个获取对象和字符串的通用方法。

一开始它是这样工作的..

public static <K, V> V getValue(Pair<K,V> _pair, String k){ ... }

我想也许它在主类中工作

GrandChildPair<String, Integer> tst = new GrandChildPair<>("dd", 1);
Integer result = Util.getValue(tst,"dd");

但我想将绑定(bind)类型扩展到 childPair 而不是 Grand one.. 要限制这个..访问?直到 ChildPair 行,并且在 GrandChildPair 中没有唤醒它。 所以我首先尝试了方法定义,

public static <K extends ChildPair<K,V>, V extends ChildPair<K,V> V getValue (

但是是的,这很愚蠢,所以我已经搜索了大约 3 个小时关于 java 中的多类型参数扩展,但我还没有找到。 我找到了其他单一类型婴儿车扩展示例,但我找不到扩展整个 泛型(可能不擅长搜索..)

有什么我可以做的吗?

public class Util { 

    ///define method
    ////          //Type parameters //return type// method name ( prams...) {
    public static (Pair extends ChildPair) V getValue (Pair<K,V> _pair, String k) {

        if (k != _pair.getK())  return null; 
        else return _pair.getV(); 
    }
}

public class Pair<K, V> {
    private K k;
    private V v;

    Pair(K k, V v) {
        this.k = k;
        this.v = v;
    }

    public K getK() {
        return k;
    }
    public V getV() {
        return v;
    }
}

public class ChildPair<K, V> extends Pair<K, V> {
    public ChildPair(K k, V v) {
    super(k, v);
    }
}

public class GrandChildPair<K, V> extends ChildPair<K, V> {

    public GrandChildPair(K k, V v) {
        super(k, v);
    }
}

public class Main {

    public static void main(String[] args) {
        Pair<String, Integer> pair = new Pair<>("someone", 35);
        Integer age = Util.getValue(pair, "someone");
        System.out.println(age);

        ChildPair<String, Integer> childPair = new ChildPair<>("gh", 20);
        Integer childAge = Util.getValue(childPair, "sss");
        System.out.println(childAge);

    }
}

最佳答案

编辑根据@OleV.V.的评论更新了对不允许哪些类(class)的检查

我认为你不能干净强制该方法不接受GrandChildPair作为参数,因为java将其视为ChildPair和Pair的类型。我还认为,如果你必须这样做,那么也许这些关系必须重新设计,因为这不是正确的方法。

还有一条不太好的路要走:

主要

public class Main {

    public static void main(String[] args) {
        try {
            // works
            Pair<String, Integer> pair = new Pair<>("someone", 35);
            Integer age = (Integer) Util.getValue(pair, "someone");
            System.out.println(age);
        } catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }

        try {
            // mismatch in K so returns null
            ChildPair<String, Integer> childPair = new ChildPair<>("ch", 20);
            Integer childAge = (Integer) Util.getValue(childPair, "sss");
            System.out.println(childAge);
        } catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }

        try {
            // error
            GrandChildPair<String, Integer> grandChildPair = new GrandChildPair<>("gh", 40);
            Integer grandChildAge = (Integer) Util.getValue(grandChildPair, "gh");
            System.out.println(grandChildAge);
        } catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }

        try {
            // error
            OtherChildPair<String, Integer> otherChildPair = new OtherChildPair<>("oh", 60);
            Integer otherChildAge = (Integer) Util.getValue(otherChildPair, "oh");
            System.out.println(otherChildAge);
        } catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }
}

实用程序

public class Util {

    public static Object getValue(Pair<?, ?> _pair, String k) throws Exception {
        // check specific class and return / throw error
        // if (_pair instanceof GrandChildPair) {

        // OR check if it extends ChildPair and return / throw error
        if (_pair.getClass().isAssignableFrom(ChildPair.class) == false) {
            throw new Exception("call not allowed");
        }

        if (_pair.getK().equals(k) == false) {
            return null;
        }

        return _pair.getV(); 
    }
}

其他子对

public class OtherChildPair<K, V> extends ChildPair<K, V> {

    public OtherChildPair(K k, V v) {
        super(k, v);
    }
}

输出

35
null
error: call not allowed
error: call not allowed

关于java - 泛型方法]在JAVA中扩展多种类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775815/

相关文章:

java - 关于 Java 泛型下限用法 : ? super T

string - Rust 中是否有 AsRef<T> 的简写形式?

java - 为什么java不允许父类的实例方法在子类中受到更多限制

java - pg_advisory_lock 有效,但 pg_try_advisory_lock 无效

java - Scala:具有具有通用返回类型的函数

c# - 同一列表中的多个泛型类型并调用它们的方法

generics - F# 类型参数缺少约束

java - 泛型类型参数构造函数

java - 正则表达式模式与? character 和 characterSequense 包含 unicode 字符

java - 在 Java 中读取 .txt 文件并将值存储到 double 组中