以具体类型作为参数的Java类 [英] Java class with concrete type as parameter

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

问题描述

有没有必要声明一个具有泛型的具体类型的类?



如果是的话,它有什么用?

如果不是,编译器允许它的具体原因是什么?



代码:

  public class SomeClass< Integer> {

// ...

public static void main(String a []){
// SomeClass<> iSome = new SomeClass<>();
// SomeClass<整数> jSome = new SomeClass<>();

SomeClass< Double> kSome = new SomeClass<>();

$ ...


code


运行正常,并且当我取消注释声明 iSome jSome 的行时发生编译器错误。

我试图把这些东西放在一起来解密泛型。

预先感谢。

解决方案

这不是你的想法。您正在创建一个名为整数的泛型类型参数,该参数会影响 java.lang.Integer


Is there any point in declaring a class with "concrete" types as generics?

If yes, what's the use for it?

If no, any specific reason why the compiler is allowing it?

The code:

public class SomeClass<Integer> {  

    //...

    public static void main (String a[]) {
        // SomeClass <> iSome = new SomeClass<>();
        // SomeClass <Integer> jSome = new SomeClass<>();

        SomeClass <Double> kSome = new SomeClass<>();

        // ...
    }
}

is running fine, and is giving compiler errors when I uncomment the lines declaring iSome and jSome.

I'm trying to put things together in "deciphering" the generics.

Thanks in advance.

解决方案

It's not what you think. You're creating a generic type parameter called Integer which shadows java.lang.Integer.

这篇关于以具体类型作为参数的Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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