Java泛型,用于使用父类中未定义的<T>方法。

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

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());
}

其中,getGenericStatusgetGenericMessage 方法在 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 &lt;T extends GenericObject&gt;void printStatus(final T result) {
    System.out.println(&quot;Status&quot;, result.getStatus());
    System.out.println(&quot;GenericStatus&quot;, result.getGenericStatus());
    System.out.println(&quot;GenericMessage&quot;, 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:

&lt;T&gt; T choose(boolean condition, T ifTrue, T ifFalse) { 
    return condition ? ifTrue : ifFalse;
}

or

&lt;T&gt; List&lt;T&gt; toList(Set&lt;T&gt; 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.

huangapple
  • 本文由 发表于 2020年10月10日 05:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64287657.html
匿名

发表评论

匿名网友

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

确定