泛型Java和阴影类型参数 [英] Generics Java and Shadowing of type parameters

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

问题描述

此代码似乎正常工作

  class Rule< T>规则(T t)
{

}
public< T>
{

public< T> void Foo(T t)
{

}
}




  1. 方法类型参数是否影射类类型参数?

  2. 另外,当您创建对象时,它是否使用类的类型参数?

示例

 规则< String> ; r =新规则< String>(); 

这是否通常适用于类的类型参数,在不冲突?我的意思是当只有类有一个类型参数,而不是构造函数,或者这是否在构造函数中查找类型参数?如果他们存在冲突,这种变化如何?

见下文讨论



如果我有一个函数调用

  x =< Type Parameter> method(); //即使在函数或类中,这也是一个语法错误;我必须在它之前放置一个这样的事情,为什么会这样呢,并且一切仍然成立。为什么我不需要为构造函数调用添加任何前缀。 Oracle不应该修复这个问题。 


解决方案

所有 T s是不同的,但如果您使用

pre> new< Float> Rule< Integer>()。< Character> Foo();

为了便于解释,我们假设您的代码是这样的:

  class规则< A> 
{

public< B> Rule()
{

}
public< C> void Foo()
{

}
}



<然后你可以显式声明泛型类型,例如:

  new< B> Rule< A>()。< C> ;美孚(); 

如果类型名称相同,则会选择最内层的(<$ c

使用此代码,请参阅:

  class规则< T>规则(T t)
{

}
public< T>
{

public< T> void Foo(T t)
{

}
}

然后这是有效的:

  new< Float> Rule< Integer>(3.2f); 

请注意,构造函数中的 T code> Float ,而不是整数 另外一个例子: p>

  class示例< T> {

public< T> void foo1(){
// T这里是< T>在foo1上声明
}

public void foo2(){
// T这里是< T>在类中声明示例
}
}

我发现另一个有问题的问题通过调用具有显式泛型类型的方法而没有任何东西。看起来像静态导入和相同类的方法调用是相同的。由于某些原因,Java似乎不会让你开始与< Type> 建立关系。


This code seems to work fine

class Rule<T>
{

    public <T>Rule(T t)
    {

    }
    public <T> void Foo(T t)
    {

    }
 }

  1. Does the method type parameter shadow the class type parameter?
  2. Also when you create an object does it use the type parameter of the class?

example

Rule<String> r = new Rule<String>();

Does this normally apply to the type parameter of the class, in the situation where they do not conflict? I mean when only the class has a type parameter, not the constructor, or does this look for a type parameter in the constructor? If they do conflict how does this change?

SEE DISCUSSION BELOW

if I have a function call

x = <Type Parameter>method(); // this is a syntax error even inside the function or class ; I must place a this before it, why is this, and does everything still hold true. Why don't I need to prefix anything for the constructor call. Shouldn't Oracle fix this.

解决方案

All of your Ts are different, but you can only see it if you call your methods with the complete syntax:

For example, this code is valid:

new <Float>Rule<Integer>().<Character>Foo();

Just to make this easier to explain, let's assume your code is this:

class Rule<A>
{

    public <B>Rule()
    {

    }
    public <C> void Foo()
    {

    }
 }

Then you can explicitly declare generic types like:

new <B>Rule<A>().<C>Foo();

If the types have the same name, the inner-most one will be chosen (the T on the method, not the class):

With this code, taking parameters:

class Rule<T>
{

    public <T>Rule(T t)
    {

    }
    public <T> void Foo(T t)
    {

    }
}

Then this is valid:

new <Float>Rule<Integer>(3.2f); 

Note that T in the constructor is Float, not Integer.

Another example:

class Example<T> {

    public <T> void foo1() {
        // T here is the <T> declared on foo1
    }

    public void foo2() {
        // T here is the <T> declared on the class Example
    }
}

I found another question that deals with calling methods with explicit generic types without something before them. It seems like static imports and same-class method calls are the same. It seems like Java doesn't let you start a line with <Type> for some reason.

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

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