gpt4 book ai didi

java - 我可以在不使类泛型的情况下使构造函数泛型吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:31 25 4
gpt4 key购买 nike

我在 Java 中看到可以使类泛型和方法泛型。我还看到了使构造函数与类一起通用的代码。我可以只使构造函数通用吗?如果是,如何调用构造函数?

最佳答案

是的,你可以。

class Example {

public <T> Example(T t) {}

public static void main(String[] args){

// In this example the type can be inferred, so new Example("foo")
// works, but here is the syntax just to show you the general case.
Example example = new<String>Example("foo");
}
}

关于java - 我可以在不使类泛型的情况下使构造函数泛型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32531769/

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