gpt4 book ai didi

java - 如果我的构造函数接受原始类型,如何使用 getDeclaredConstructor?

转载 作者:行者123 更新时间:2023-11-29 10:14:36 27 4
gpt4 key购买 nike

我正在寻找这样的构造函数:

type.getDeclaredConstructor(Integer.class);

当类型为

MyType {
public MyType(Integer a);
}

但是,当类型为

MyType {
public MyType(int a);
}

而且我不想将 int 替换为 Integer,因为有人告诉我这可能会产生不必要的开销。

我该怎么办?

最佳答案

你可以使用

type.getDeclaredConstructor(int.class);

来自 section 15.8.2 of the JLS :

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.

关于java - 如果我的构造函数接受原始类型,如何使用 getDeclaredConstructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21134932/

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