英文:
Java Generics for using <T> methods not defined in parent class
问题
我有一个方法,如下所示:
private <T extends GenericObject> void printStatus(final T result) {
System.out.println("Status", result.getStatus());
System.out.println("GenericStatus", result.getGenericStatus());
System.out.println("GenericMessage", result.getGenericMessage());
}
其中,getGenericStatus
和 getGenericMessage
方法在 GenericObject
父类中定义。然而,getStatus
方法在扩展了 GenericObject
的子类中定义,而 getStatus
的返回类型是一个枚举,每个子类中的枚举定义都不同。例如:
public class A extends GenericObject {
/** 枚举 AStatus. */
private AStatus status;
AStatus getStatus() {
return status;
}
}
public class B extends GenericObject {
/** 枚举 BStatus. */
private BStatus status;
BStatus getStatus() {
return status;
}
}
问题是在 printStatus
中的 result.getstatus()
导致编译错误。在不大幅度重构代码的情况下,如何在 printStatus
方法内部最简单地修复这个问题?
英文:
I have a method which is as follows
private <T extends GenericObject>void printStatus(final T result) {
System.out.println("Status", result.getStatus());
System.out.println("GenericStatus", result.getGenericStatus());
System.out.println("GenericMessage", result.getGenericMessage());
}
The getGenericStatus and getGenericMessage methods are defined in the genericObject parent class. However, the getStatus method is defined in the child classes that extends GenericObject and the return type of getStatus is an enum which is differently defined for each child class. Such as
public class A extends GenericObject {
/** Enum Astatus. */
private AStatus status;
AStatus getStatus() {
return status;
}
}
public class B extends GenericObject {
/** Enum Bstatus. */
private BStatus status;
BStatus getStatus() {
return status;
}
}
Problem is result.getstatus() in the printStatus is giving compile error. What will be the easiest way to fix this within the printStatus method without refactoring a lot of code?
答案1
得分: 6
在T
只出现在一个地方时,将方法设为泛型方法是没有意义的。相反,你可以直接这样写:
private void printStatus(GenericObject result) { ... }
类型变量主要在它们在多个地方出现时才会添加价值,例如:
<T> T choose(boolean condition, T ifTrue, T ifFalse) {
return condition ? ifTrue : ifFalse;
}
或者
<T> List<T> toList(Set<T> set) { ... }
因为它们在多个参数之间或参数与返回值之间表达了实际的约束关系。
英文:
There is no point in making method generic in T
when T
appears in only one place. Instead, you can just write:
private void printStatus(GenericObject result) { ... }
Type variables mainly add value only when they appear in multiple places, such as:
<T> T choose(boolean condition, T ifTrue, T ifFalse) {
return condition ? ifTrue : ifFalse;
}
or
<T> List<T> toList(Set<T> set) { ... }
because they express actual constraints between multiple parameters or between parameter and return.
答案2
得分: 0
一种方法是将getStatus
的声明提升到GenericObject
类中:
public class GenericObject {
public abstract Object getStatus();
// Other methods...
}
其他类无需更改,因为子类允许特化父类方法的返回类型。
英文:
One approach is to push the declaration of getStatus
up to the GenericObject
class:
public class GenericObject {
public abstract Object getStatus();
// Other methods...
}
The other classes won't have to change since subclass are allowed to specialize the return type of a parent's method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论