通过通用父级访问的子类中的Java静态成员 [英] Java static members in a subclass accessed via a generic parent

查看:43
本文介绍了通过通用父级访问的子类中的Java静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是个新问题,但是上一次我使用Java时,该语言没有泛型.我有一个类层次结构(将名称更改为尽可能通用的名称):

This seems like a newbish question, but the last time I worked with Java, the language didn't have generics. I have a class hierarchy (names changed to be as generalized as possible):

public abstract class AbstractBase { .... }
public class ConcreateSubA extends AbstractBase { .... }
public class ConcreateSubB extends AbstractBase { .... }
...
public class ConcreateSubZZ9PluralZAlpha extends AbstractBase { .... }
...

我正在尝试清理一些遗留代码,并且在一个地方可以通过泛型将大量重复复制全部分解为一个例程.(我在考虑泛型,因为调用此例程时,它只需要对一个具体的类进行操作即可.)

I'm trying to clean up some legacy code, and there's one place where a ton of repetitive duplication can all be factored out into a single routine via generics. (I'm thinking generics because when this routine is called, it needs to operate on only one of the concrete classes.)

例程看起来像

public <Thing extends AbstractBase> void someFunc()
{
    another_function_call (Thing.concreteSpecialToken);
    // could also be
    //   another_function_call (Thing.concreteSpecialToken());
    // if methods are more feasible than fields

    // Cannot use
    //   another_function_call (Thing().concreteSpecialToken());
    // because creating instances of these types is a Major Operation[tm]
}

我将要删除的行数不计其数,但这是重要的部分: someFunc()是参数类型的(实际上它接受参数,但是它们都不是事物,因此没有推论).最终,我需要获取一个特殊的令牌,这就是我变得模糊的地方.

I'm leaving out about a zillion lines, but that's the important part: someFunc() is type parametric (it actually takes arguments but none of them are Things so no inference). Eventually I need to fetch a special token and this is where I'm getting fuzzy.

对于每个具体类,标记都是巨大的唯一字符串.它们是基于类的,而不是基于实例的.实际的令牌值在每个子类中都声明为 private static final 字段.

The tokens are huge-ass unique strings for each concrete class. They're class-based, not instance-based. The actual token value is declared as a private static final field in each subclass.

因此,我需要使用基类的公共方法/字段来(最终)获取子类的私有静态字段.显然,我无法在基础中声明抽象静态方法,因为这没有任何意义.如果数据是基于实例的,那么这将是微不足道的,在基类中具有多态getter,但是子类的内容是静态的.

So I need to use the public methods/fields of a base class to (eventually) get to the private static field of a subclass. Obviously I can't declare an abstract static method in the base, because that makes no sense. If the data were instance-based, then this would be trivial, with a polymorphic getter in the base class, but the subclass stuff is static.

我觉得我在这里缺少Java泛型的功能,但是除非 whatever 是可能的东西,否则我不能使用 Thing.whatever()在抽象基类中声明.我遇到了Java的局限性,或者由于缺乏专业知识而试图缩小差距.我进行的一次尝试似乎很有希望,在类层次结构中一直有大量的代码重复,用一遍又一遍地用完全相同的代码定义抽象方法...这是泛型应该帮助防止的!

I feel like I'm missing a feature of Java generics here, but I can't use Thing.whatever() unless the whatever is something that's possible to declare in the abstract base class. I'm running up against either limitations of Java or my lack of expertise trying to bridge the gap. The one attempt I made that seemed promising also had a ton of code duplication all the way down the class hierarchy, defining abstract methods with the exact same code over and over... which is what generics are supposed to help prevent!

推荐答案

我遇到了Java的局限性,或者我缺乏弥补桥梁的专业知识.

I'm running up against either limitations of Java or my lack of expertise trying to bridge the gap.

这是Java的局限性,尽管这是一个相当合理的IMO.基本上,您仍在尝试使用静态成员,就好像它们是多态的一样,这是行不通的-泛型对您无济于事.

It's a limitation of Java, although a fairly reasonable one IMO. Basically you're still trying to use static members as if they were polymorphic, and that's not going to work - generics don't help you there.

选项:

  • 使用反射...但是请记住,类型擦除意味着除非明确地将其传递,否则您无法获得 Thing Class
  • 如果您还是 Thing 实例,只需将其设为抽象的 instance 成员,该成员在每个实现中都会碰巧返回静态字段的值
  • 创建一个将使用实例成员的单独的类型层次结构
  • Use reflection... but bear in mind that type erasure means you can't get at the Class for Thing unless you pass it in explicitly
  • If you've got an instance of Thing anyway, just make it an abstract instance member, which in each implementation happens to return the value of a static field
  • Create a separate type hierarchy which will use instance members

这篇关于通过通用父级访问的子类中的Java静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆