简体   繁体   English

您可以将原始类型用于类或方法中的泛型参数吗?

[英]Can you use primitive types for generic parameters in a class or method?

I am writing a short paper on generics and I have not really used them all that much in Java. 我正在写一篇关于泛型的简短文章,而我在Java中并没有真正使用它们。 I was wondering if a primitive type can be used as a parameter type in a generic variable. 我想知道原始类型是否可以用作通用变量中的参数类型。 I have not been able to get it to compile with a generic parameter that is of a generic type. 我无法使其与具有通用类型的通用参数一起编译。

Question: I am somewhat not certain as to how generics can handle primitive datatypes. 问题:对于泛型如何处理原始数据类型,我有些不确定。 Can someone example how I would have to declare the method different to not have to add a cast to the int type with Integer and if it is possible to use a primitive Java datatype in the class declaration 有人可以举例说明我如何必须声明不同的方法,而不必在Integer中将强制类型转换添加到int类型,以及是否可以在类声明中使用原始Java数据类型。

   DDHGeneric<int,int,int,int> t = new DDHGeneric<int,int,int,int>();

Is there a way to do this? 有没有办法做到这一点?

The following declaration does not compile either. 以下声明也不编译。 I do know that from what I know about generics that there is a different between using the primitive data types and a user-defined class. 我确实知道,从对泛型的了解来看,使用原始数据类型和用户定义的类之间存在差异。

   DDHGeneric<int, int, int, int> temp2 = new  DDHGeneric<int, int, int, int>();

Class: 类:

public class DDHGeneric<T,K,Bigfoot,Pizza> {
    T a;
    K n;
    Bigfoot u;

    public DDHGeneric() {
    }

    private <rapper> void myMethod(rapper y) {
    int temp = y; <--Errors out on this line as will.
}

    public <Pizza> void myAddition(Pizza a, Pizza b) {
    }

    private <K> void me(K a, K b, K c) {
    }

    public static void main(String[] args) {
         DDHGeneric<int, Integer, Integer, Integer> temp = new  DDHGeneric<int, Integer, Integer, Integer>();
         temp.myAddition(5, 4);
         temp.a = "Test this string!" ;
    }
}

You cannot. 你不能。

Oracle gives an article on this topic . Oracle 对此主题发表了一篇文章

to quote: 报价:

When creating a Pair object, you cannot substitute a primitive type for the type parameter K or V: 创建对对象时,不能用基本类型替换类型参数K或V:

Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error

You can substitute only non-primitive types for the type parameters K and V: 您只能将非基本类型替换为类型参数K和V:

Pair<Integer, Character> p = new Pair<>(8, 'a');

No. 没有。

Generics aren't meant to represent primitives. 泛型不代表原始。 They're meant to represent objects (as the generic types all extend Object implicitly ). 它们旨在表示对象(因为泛型类型都隐式扩展了Object )。

You can use the wrapper variants of the primitive class you want instead to achieve the same effect. 您可以使用所需的原始类的包装器变体来实现相同的效果。

 DDHGeneric<Integer, Integer, Integer, Integer> t = new DDHGeneric<>();

Here is a brief excerpt from Why Use Generics? 这是“ 为什么使用泛型?”的简短摘录 , found in the Java Trails: ,可在Java Trail中找到:

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. 简而言之,泛型在定义类,接口和方法时使类型 (类和接口)成为参数。

Generics only take primitive class but not the primitive data type. 泛型仅采用原始类,而不采用原始数据类型。

For each data type used its corrasponding class for it 对于每种数据类型,使用其对应的类

DDHGeneric<int,int,int,int> t = new DDHGeneric<int,int,int,int>();

can be used as 可以用作

DDHGeneric<Integer, Integer, Integer, Integer> t = new DDHGeneric<Integer, Integer, Integer, Integer>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM