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

查看:149
本文介绍了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天全站免登陆