Java 通用类型在这种情况下不起作用
Java generic type not working in this case
此class显示信息:
// display line numbers from a file
display(getLineNumber(myFile));
// display users from DB
display(getUsersName(myDBRepository));
等...
我想做一个通用界面,这样我就可以外部化显示信息的代码。
然后我可以这样做:
myInformationElements.stream().forEach(e -> display(e.getValue());
这是我目前所拥有的(不工作):
public interface InformationElement {
public <T> String getValue (T param);
}
public class NbFileLineInformationElement implements InformationElement{
@Override
public <File> String getValue(File param) {
return *same code as in getLineNumber(myFile)*;
}
}
public class UserInformationElement implements InformationElement{
@Override
public <UserRepository> String getValue(UserRepository param) {
return *same code as in getUsersName(myDBRepository)*;
}
}
- 这里我的泛型类型不起作用:文件未重新识别为 java.io.File(我的 jpa 存储库也是如此)我在这里做错了什么?
- 这是满足我需求的最佳做法吗?
您定义了类型参数 File
和 UserRepository
,它们隐藏了 class 名称 File
和 UserRepository
。这是命名类型参数与现有 classes 相同的惊喜之一。类型参数不代表 classes,它们没有边界,因此编译器只能假设它们有 Object
方法。
这不是最佳做法。在实现泛型方法时,这些方法必须保持泛型,并且至少在边界方面是完全开放的。为了以后能够限制类型参数的含义,在 class/interface 上定义它,并让 subclasses 提供它对带有类型参数的特定实现的含义。
此处最好的解决方案是将 InformationElement
的类型参数移至 class,并在您的子 class 中提供类型参数。这些方法不再是通用的,但它们确实使用 interface/classes.
上定义的类型参数
interface InformationElement<T> {
public String getValue (T param);
}
class NbFileLineInformationElement implements InformationElement<File>{
@Override
public String getValue(File param) {
return /*same code as in getLineNumber(myFile)*/;
}
}
class UserInformationElement implements InformationElement<UserRepository>{
@Override
public String getValue(UserRepository param) {
return /*same code as in getUsersName(myDBRepository)*/;
}
}
此class显示信息:
// display line numbers from a file
display(getLineNumber(myFile));
// display users from DB
display(getUsersName(myDBRepository));
等...
我想做一个通用界面,这样我就可以外部化显示信息的代码。
然后我可以这样做:
myInformationElements.stream().forEach(e -> display(e.getValue());
这是我目前所拥有的(不工作):
public interface InformationElement {
public <T> String getValue (T param);
}
public class NbFileLineInformationElement implements InformationElement{
@Override
public <File> String getValue(File param) {
return *same code as in getLineNumber(myFile)*;
}
}
public class UserInformationElement implements InformationElement{
@Override
public <UserRepository> String getValue(UserRepository param) {
return *same code as in getUsersName(myDBRepository)*;
}
}
- 这里我的泛型类型不起作用:文件未重新识别为 java.io.File(我的 jpa 存储库也是如此)我在这里做错了什么?
- 这是满足我需求的最佳做法吗?
您定义了类型参数
File
和UserRepository
,它们隐藏了 class 名称File
和UserRepository
。这是命名类型参数与现有 classes 相同的惊喜之一。类型参数不代表 classes,它们没有边界,因此编译器只能假设它们有Object
方法。这不是最佳做法。在实现泛型方法时,这些方法必须保持泛型,并且至少在边界方面是完全开放的。为了以后能够限制类型参数的含义,在 class/interface 上定义它,并让 subclasses 提供它对带有类型参数的特定实现的含义。
此处最好的解决方案是将 InformationElement
的类型参数移至 class,并在您的子 class 中提供类型参数。这些方法不再是通用的,但它们确实使用 interface/classes.
interface InformationElement<T> {
public String getValue (T param);
}
class NbFileLineInformationElement implements InformationElement<File>{
@Override
public String getValue(File param) {
return /*same code as in getLineNumber(myFile)*/;
}
}
class UserInformationElement implements InformationElement<UserRepository>{
@Override
public String getValue(UserRepository param) {
return /*same code as in getUsersName(myDBRepository)*/;
}
}