简而言之,这是我一直在尝试做的事情:
class example <T extends Number>
{
private int function(T number)
{
int x = (int) number;
...
}
...
}
基本上,我试图使 T 成为一个数字,这样我就可以在该函数内将它转换为一个 int。问题是我遇到了“无法隐藏的类型”错误,所以我一定是做错了什么。
最佳答案
考虑到抽象类Number
是类的父类(super class)
* BigDecimal
, BigInteger
,
* 字节
, double
, float
,
* Integer
、Long
和 Short
...
如果你想要一个 int 表示,你应该使用 .intValue()
函数。
如果这是你想要做的。
关于java - 在java中将通用参数转换为int,前提是它是一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/393252/