java泛型如何在泛型中创建对象 [英] java generics how objects are creating in generics

查看:3208
本文介绍了java泛型如何在泛型中创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public<< requestType>>详细信息createRequest()

{

<< requestType>>详情请求= new<< requestType> ;> Impl();

//填充请求属性

返回请求;

}



java中的含义是什么

public <<requestType>>Details createRequest()
{
<<requestType>>Details request = new <<requestType>>Impl();
// populate request attributes
return request;
}

what does it mean in java

推荐答案

你好,



代码你提出没有任何意义。要了解有关泛型的更多信息,请查看 [ ^ ]文档。



你可以写可以使用不同类型的参数调用的单个泛型方法声明。根据传递给泛型方法的参数类型,编译器会适当地处理每个方法调用。以下是定义通用方法的规则:



  • 所有泛型方法声明都有一个由尖括号分隔的类型参数部分(<和> )在方法的返回类型之前(在下一个示例中为< E>)。
  • 每个类型参数部分包含一个或多个以逗号分隔的类型参数。类型参数(也称为类型变量)是指定泛型类型名称的标识符。
  • 类型参数可用于声明返回类型并充当传递的参数类型的占位符通用方法,称为实际类型参数。
  • 泛型方法的主体声明为任何其他方法的主体。请注意,类型参数只能表示引用类型,而不是基本类型(如int,double和char)。
Hello,

The code you have presented has no meaning. To learn more about Generics please have a look at this[^] documentation.

You can write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods:

  • All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example).
  • Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.
  • The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.
  • A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types (like int, double and char).
 // generic method printArray                         
public static < E > void printArray( E[] inputArray )
{
    // Display array elements              
    for ( E element : inputArray ){        
        System.out.printf( "%s ", element );
    }
    System.out.println();
}



问候,


Regards,


这篇关于java泛型如何在泛型中创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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