gpt4 book ai didi

java - 如何使用泛型调用带有类参数的方法?

转载 作者:行者123 更新时间:2023-11-30 03:27:41 25 4
gpt4 key购买 nike

我有一个类,它使用反射来操作其他类:

package com.cw.cmt;

public class Container<T extends Class<?>> {
private final Class<T> clazz;

public Container(final Class<T> clazz) {
this.clazz = clazz;
System.out.println("Expensive constructor for " + this);
}

@Override
public String toString() {
return String.format("Container [clazz=%s]", clazz);
}
}

因为构建这些可能会很昂贵,所以我想将它们缓存在 Map 中,以便仅根据需要构建它们:

package com.cw.cmt;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ContainerCache {
private static final ConcurrentMap<Class<?>, Container<? extends Class<?>>> cache = new ConcurrentHashMap<>();

public static <C extends Class<?>> Container<C> getContainer(final Class<C> clazz) {
final Container<? extends Class<?>> result = cache.computeIfAbsent(clazz, k -> new Container<C>(clazz));
// it would be nice to eliminate this cast!
return (Container<C>) result;
}
}

我无法确定调用此 getContainer 方法的正确通用语法。

// The parameterized method <Class<String>>getContainer(Class<Class<String>>) of type ContainerCache 
// is not applicable for the arguments (Class<String>)
ContainerCache.getContainer(String.class);

// Bound mismatch: The generic method getContainer(Class<C>) of type ContainerCache is not applicable
// for the arguments (Class<String>). The inferred type String is not a valid substitute for the bounded
// parameter <C extends Class<?>>
ContainerCache.<String>getContainer(String.class);

// The parameterized method <Class<String>>getContainer(Class<Class<String>>) of type ContainerCache
// is not applicable for the arguments (Class<String>)
ContainerCache.<Class<String>>getContainer(String.class);

最佳答案

我认为您无法消除转换,因为您的 map 包含多种“类型”的容器,因此您需要转换为正确的容器。正如所指出的,您的容器应该是 Container<T>以及您的 getContainer 中的更多类型转换应该做你想做的事:

public static class ContainerCache {
private static final Map<Class<?>, Container<?>> cache = new ConcurrentHashMap<>();

public static <T> Container<T> getContainer(final Class<T> clazz) {
return (Container<T>) cache.computeIfAbsent(clazz, c -> new Container<> ((Class<T>) c));
}
}

public static class Container<T> {
private final Class<T> clazz;

public Container(final Class<T> clazz) {
this.clazz = clazz;
System.out.println("Expensive constructor for " + this);
}

@Override
public String toString() {
return String.format("Container [clazz=%s]", clazz);
}
}

这有点难看,但我认为你不能做得更好......

关于java - 如何使用泛型调用带有类参数的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com