英文:
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<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);
答案1
得分: 2
你有两个不相关的 T
类型。在你的方法声明中,<T>
重新声明了一个类型 T
,隐藏了类级别的类型变量。
简而言之,你不需要使方法成为泛型,因为你希望 setValue
方法采用在类上声明的类型。由于类型参数在整个类中都可用,你的方法应该尽可能简单:
protected void setValue(T value) {
if (value != null)
this.value = value;
}
如果你的抽象类也是泛型的,那么同样的修正也需要在其中进行。否则,你需要重新审视其设计,因为接受随机类型值的方法并不完全正确。
英文:
You have two unrelated T
types. In your method declaration, <T>
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 <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;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论