使用通用数据类型(Java)进行打字

发布于 2025-02-07 22:27:04 字数 672 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文