Java泛型方法的返回类型 [英] Return Type of Java Generic Methods

查看:47
本文介绍了Java泛型方法的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么不返回任何 void 的泛型方法是(或可以)这样声明的:

I wonder why generic methods which return nothing void are (or can be) declared this way:

   public static <E> void printArray( E[] inputArray ) {
     // Display array elements              
     for ( E element : inputArray ){        
        System.out.printf( "%s ", element );
     }
     System.out.println();
   }

看起来<E>是返回对象的类型,但实际上方法什么都不返回.那么<E>在这种情况下的真正含义是什么,一般在泛型方法中是什么?

It seems like <E> is the type of the returned object, but the method returns nothing in fact. So what is the real meaning of <E> in this case specifically and in generic methods generally?

推荐答案

这个问题适合我的一个旧笔记.我希望这个插图有帮助:

This question suits one of my old notes. I hope this illustration helps:

这篇关于Java泛型方法的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆