gpt4 book ai didi

java - 如何转换 : cut(Orange. 类)-> 剪切(橙色)

转载 作者:行者123 更新时间:2023-11-29 04:21:58 29 4
gpt4 key购买 nike

如何变换:cut(Orange.class) -> cut(Orange)

剪切函数如下所示:

protected <T extends Fruit> T cut(Class<T> fruitClass){
try {
return fruitClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {

}
}

最佳答案

您可以从传递的实例中获取类型:

protected <T extends Fruit> T cut(T fruit) {
try {
return (T)fruit.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
// throw some exception or return an alternative instance or null
}
}

关于java - 如何转换 : cut(Orange. 类)-> 剪切(橙色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48562259/

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