Java static members in subclasses accessed through generic parent classes

This seems like a new problem, but the last time I use Java, the language has no generics I have a class hierarchy (change the name to as broad 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. There's a place to break a ton of duplicate data into a single routine through generics I'm thinking about generics because when this routine is called, it only needs to operate on a specific class

The routine looks like

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]
}

I'm throwing a billion lines, but this is an important part: somefunc () is a parameter type (actually a reference parameter, but none is a thing, so there is no inference) Finally, I need to get a special token, which is where I become blurred

A token is a unique string for each concrete class They are class - based, not instance - based The actual token value is declared as a private static final field in each subclass

So I need to use the public method / field of the base class (eventually) to the private static field of a subclass Obviously, I can't declare an abstract static method in the foundation because it doesn't make sense If the data is instance based, it will be insignificant. There is a polymorphic getter in the base class, but the subclass is static

I think I lack a feature of Java generics here, but I can't use thing except what is declared in the abstract base class whatever(). I am working with Java limitations or I lack expertise to fill the gap A promising attempt I have made has been a lot of code repetition in the class hierarchy, defining abstract methods over and over again with exactly the same code. This is a generic type, which should help prevent!

Solution

This is a limitation of Java, although it is a quite reasonable IMO. basically, you still try to use static members as if they were polymorphic, which won't work - generics won't help you

Options:

>Use reflection... But remember, type erasure means you can't get the class unless you explicitly pass it > if you have an instance of something, just make it an abstract instance member, and the value of static field will be returned in each implementation > create a unique type hierarchy, which will use instance members

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>