类型参数G隐藏类型G [英] The type parameter G is hiding the type G

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

问题描述

public class MyGenericClass< G> {


    public <G> G genericMethod1(G g){
        System.out.println(g.toString());
        return g;

    }

    public <X> G genericMethod2(G g) {
        System.out.println(g.toString());
        return g;
    }

    public <X> X genericMethod3(G g) {
        System.out.println(g.toString());
        Integer i = new Integer(100);
        return (X) i;       
    }


    public static void main(String... cmdarg) {

        MyGenericClass<Employee> gemp = new MyGenericClass<Employee>();
        Employee e1 = new Employee();
        e1.setName("My Name");


        String resultOfMethod3 = gemp.genericMethod3(e1);
        System.out.println(resultOfMethod3);                            

    }


}

ingenericMethod1:我收到编译器警告,因为类型参数G隐藏了类型G".

in genericMethod1: I get the compiler warning as "The type parameter G is hiding the type G".

问题: 该G隐藏了哪个G?这到底是什么意思?

Question: Which G is hidden by this G? What's the actual meaning of this?

genericMethod2没有显示任何错误/警告.

genericMethod2 doesn't show any errors/warnings.

问题: 我认为它不应该编译.为什么在同一方法签名中允许使用两种不同的类型?与G和X进行比较的典型实时示例是什么?

Question: I thought it should not compile. why is two different types allowed in same method signature? What can be typical real time example to compare with G and X?

在genericMethod3中:我有意返回一个Integer对象,并且在main方法中,我试图将结果分配给String.不出所料:我得到了ClassCastException.

In genericMethod3: I'm intentionally returning an Integer object and in the main method i'm trying to assign the result to a String. As expected: I got the ClassCastException.

问题:使用泛型来避免ClassCastExceptions的最初目的不是吗?为什么编译器允许它?这是泛型设计中的错误吗?

Question: Isn't the original purpose of having generics to avoid ClassCastExceptions? Why did the compiler allow it? Is this a bug in the design of Generics?

推荐答案

泛型可以在类级别和方法级别使用:

Generics can be used at class level and at method level:

  • public class MyClass<T>:T可用于类(字段,方法,内部类...)的所有实例成员(即非静态)
  • public (static) <U> ReturnType myMethod(..) {...}:U仅适用于该方法
  • public class MyClass<T>: T is available for all instance members (i.e. non-static) of the class (fields, methods, inner classes...)
  • public (static) <U> ReturnType myMethod(..) {...}: U is available for the method only

您正在混合它们.

对于public <G> G genericMethod1(G g),此<G>泛型将隐藏类的一个,因为它们具有相同的名称,因此后者在该方法中不再可用.如果要使用类的泛型,请不要在方法级别上指定它:public G genericMethod1(G g).

With public <G> G genericMethod1(G g), this <G> generic is hiding the class' one since they have the same name, so the latter isn't available in the method anymore. If you want to use the class' generic, don't specify it at the method level: public G genericMethod1(G g).

使用public <X> G genericMethod2(G g),您将定义不使用的通用<X>:仅使用类的通用G.

With public <X> G genericMethod2(G g), you're defining a generic <X> that isn't used: only the class' generic G is used.

使用public <X> X genericMethod3(G g),您正在定义用于该方法的返回值的通用<X>.在方法主体中,您正在将int强制转换为X,但是不能确定Xint的超类型.从技术上讲,这可以通过<X super Integer>完成,但是我不确定这实际上是否是您想要的.

With public <X> X genericMethod3(G g), you're defining a generic <X> that is used for the return value of the method. In the method body, you're casting an int to X but you can't be sure that X is a super type of int. Technically, this can be done with <X super Integer>, but I'm not sure this is actually what you're looking for.

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

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