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

查看:22
本文介绍了通过通用父类访问的子类中的 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.

所以我需要使用基类的公共方法/字段来(最终)获取子类的私有静态字段.显然我不能在基类中声明一个 abstract static 方法,因为那没有意义.如果数据是基于实例的,那么这将是微不足道的,基类中有一个多态的 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 泛型的一个特性,但我不能使用 Thing.whatever() 除非 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.

选项:

  • 使用反射...但请记住,类型擦除意味着您无法获得 ThingClass 除非您显式传入它
  • 如果你有 一个 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天全站免登陆