如何设置一个通用值集?

huangapple go评论109阅读模式
英文:

How do I do a set of a generic value?

问题

I have the following defined:

public class myClass<T> {
	private T value;

	protected <T> void setValue(T value) {
		if (value != null) // optional
			this.value = value;
	}
}

However I get a compilation error with this:

Type mismatch: cannot convert from T to T

(I know, helpful and logical right?).

What's the proper way to setup this method? Also note, the signature is setup in an abstract class like this:

@SuppressWarnings("hiding")
protected abstract <T> void setValue(T value);
英文:

I have the following defined:

public class myClass&lt;T&gt; {
	private T value;

	protected &lt;T&gt; void setValue(T value) {
		if (value != null) // optional
			this.value = value;
	}
}

However I get a compilation error with this:

Type mismatch: cannot convert from T to T

(I know, helpful and logical right?).

What's the proper way to setup this method? Also note, the signature is setup in an abstract class like this:

@SuppressWarnings(&quot;hiding&quot;)
protected abstract &lt;T&gt; void setValue(T value);

答案1

得分: 2

你有两个不相关的 T 类型。在你的方法声明中,&lt;T&gt; 重新声明了一个类型 T,隐藏了类级别的类型变量。

简而言之,你不需要使方法成为泛型,因为你希望 setValue 方法采用在类上声明的类型。由于类型参数在整个类中都可用,你的方法应该尽可能简单:

protected void setValue(T value) {
    if (value != null)
        this.value = value;
}

如果你的抽象类也是泛型的,那么同样的修正也需要在其中进行。否则,你需要重新审视其设计,因为接受随机类型值的方法并不完全正确。

英文:

You have two unrelated T types. In your method declaration, &lt;T&gt; re-declares a type T, hiding the class-level type variable.

In short, you don't need the method to be generic, because you want setValue to take the type declared on the class. Because the type parameter is available throughout the class, your method should be as simple as:

protected void setValue(T value) {
	if (value != null)
		this.value = value;
}

If your abstract class is generic too, then the same correction needs to be made in it too. Otherwise, you need to revisit its design as having a method taking randomly typed values isn't exactly right.

答案2

得分: 1

You already have defined T at class level, if you put it again in the method it takes it as another T. Look at the error message I get when trying to compile your code.

MyClass.java:6: error: incompatible types: T#1 cannot be converted to T#2
        this.value = value;
                     ^
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>setValue(T#1)
    T#2 extends Object declared in class MyClass
1 error

Just remove the second declaration of T and it will compile as intended.

public class MyClass<T> {
    private T value;

    protected void setValue(T value) {
        if (value != null) {
            this.value = value;   
        }
    }
}
英文:

You already have defined T at class level, if you put it again in the method it takes it as another T. Look at the error message I get when trying to compile your code.

MyClass.java:6: error: incompatible types: T#1 cannot be converted to T#2
        this.value = value;
                     ^
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method &lt;T#1&gt;setValue(T#1)
    T#2 extends Object declared in class MyClass
1 error

Just remove the second declaration of T and it will compile as intended.

public class MyClass&lt;T&gt; {
    private T value;

    protected void setValue(T value) {
        if (value != null) {
            this.value = value;   
        }
    }
}

huangapple
  • 本文由 发表于 2020年8月3日 13:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63223988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定