泛型Java和阴影类型参数 [英] Generics Java and Shadowing of type parameters
问题描述
此代码似乎正常工作
class Rule< T>规则(T t)
{
}
public< T>
{
public< T> void Foo(T t)
{
}
}
- 方法类型参数是否影射类类型参数?
- 另外,当您创建对象时,它是否使用类的类型参数?
示例
规则< String> ; r =新规则< String>();
这是否通常适用于类的类型参数,在不冲突?我的意思是当只有类有一个类型参数,而不是构造函数,或者这是否在构造函数中查找类型参数?如果他们存在冲突,这种变化如何?
见下文讨论
如果我有一个函数调用
x =< Type Parameter> method(); //即使在函数或类中,这也是一个语法错误;我必须在它之前放置一个这样的事情,为什么会这样呢,并且一切仍然成立。为什么我不需要为构造函数调用添加任何前缀。 Oracle不应该修复这个问题。
所有 为了便于解释,我们假设您的代码是这样的: 如果类型名称相同,则会选择最内层的(<$ c 使用此代码,请参阅: 然后这是有效的: 请注意,构造函数中的 我发现另一个有问题的问题通过调用具有显式泛型类型的方法而没有任何东西。看起来像静态导入和相同类的方法调用是相同的。由于某些原因,Java似乎不会让你开始与 This code seems to work fine
example 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
All of your For example, this code is valid: Just to make this easier to explain, let's assume your code is this: Then you can explicitly declare generic types like: If the types have the same name, the inner-most one will be chosen (the With this code, taking parameters: Then this is valid: Note that Another 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 这篇关于泛型Java和阴影类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! T
s是不同的,但如果您使用
pre> new< Float> Rule< Integer>()。< Character> Foo();
class规则< A>
{
public< B> Rule()
{
}
public< C> void Foo()
{
}
}
<然后你可以显式声明泛型类型,例如:
new< B> Rule< A>()。< 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>在类中声明示例
}
}
< Type>
建立关系。class Rule<T>
{
public <T>Rule(T t)
{
}
public <T> void Foo(T t)
{
}
}
Rule<String> r = new Rule<String>();
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.
T
s are different, but you can only see it if you call your methods with the complete syntax:new <Float>Rule<Integer>().<Character>Foo();
class Rule<A>
{
public <B>Rule()
{
}
public <C> void Foo()
{
}
}
new <B>Rule<A>().<C>Foo();
T
on the method, not the class):class Rule<T>
{
public <T>Rule(T t)
{
}
public <T> void Foo(T t)
{
}
}
new <Float>Rule<Integer>(3.2f);
T
in the constructor is Float
, not Integer
.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
}
}
<Type>
for some reason.