使用通用数据类型(Java)进行打字
public class Main
{
static <T extends Number , E > void add ( T o2 )
{
@SuppressWarnings("unchecked")
E e2 = (E) o2; // it must be casted to integer
System.out.println( e2.getClass().getName() ); // java.lang.Float ( it is not
casted )
}
public static void main(String[] args)
{
Main g1= new Main();
g1.<Float , Integer>add( 6.2f );
}
}
我要实现的内容类似于以下代码评估的内容 -
float y = 6.2f;
int x = (int) y;
System.out.println(x);
我需要将float转换为函数添加的整数。 System.Out语句应打印Java.lang.integer作为输出。 我该如何解决?是否可以实现此问题,或者我需要解决此代码。请帮忙。
public class Main
{
static <T extends Number , E > void add ( T o2 )
{
@SuppressWarnings("unchecked")
E e2 = (E) o2; // it must be casted to integer
System.out.println( e2.getClass().getName() ); // java.lang.Float ( it is not
casted )
}
public static void main(String[] args)
{
Main g1= new Main();
g1.<Float , Integer>add( 6.2f );
}
}
What I'm trying to implement is similar to what the below code evaluates to -
float y = 6.2f;
int x = (int) y;
System.out.println(x);
I need to convert Float to Integer in the function add. The System.out statement should print java.lang.Integer as output.
How can i solve this? Is it possible to implement this or i need to workaround this code. Please Help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论