Java abstract static Workaround

前端 未结 15 1927
猫巷女王i
猫巷女王i 2020-12-29 04:43

I understand that neither a abstract class nor an interface can contain a method that is both abstract and static because of ambiguity problems, but is there a workaround?

相关标签:
15条回答
  • 2020-12-29 05:10

    No. You can't do that. If you're willing to compromise and make the method non-static or provide an implementation of the static method in your abstract class, you'll be able to code this in Java.

    0 讨论(0)
  • 2020-12-29 05:17

    Static methods are relevant to an entire class of object, not the individual instances. Allowing a static method to be overridden breaks this dictum.

    The first thing I would consider is to access your database from a non-static context. This is actually the norm for Java apps.

    If you absolutely must use a static method, then have it parameterised with instance specific arguments (of a generic type) to allow the different subclasses to interact with it. Then call that single static method from you polymorphic methods.

    0 讨论(0)
  • 2020-12-29 05:17

    A type system allows you to express some constraints among types, but it's limited. That's why javadocs are littered with constraints in human language, asking people to follow rules that the compiler cannot check.

    if you want to extend it beyond what language provides natively, you can write your own static analysis tool. that is not uncommon. for example: findbug. also IDEs do that too, they checking thing beyond what language dictates. you can write a plug in to enforce that a subclass must have a static method of such signature.

    in your case, it's not worth it. have javadoc in the superclass urge implementors to include a static method, that's good enough.

    I'll provide a convoluted way of expressing your constraint anyway, but DO NO DO IT. people get really carried away of make everything checkable at compile time, at the price of making code unreadable.

    interface WidgetEnumerator
    {
        List getAllWidgets();
    }
    
    public class Abs<T extends WidgetEnumerator>
    {
        static List getAllWidgets(Class<? extends Abs> clazz){ ... }
    }
    
    public class Sub extends Abs<SubWidgetEnumerator>
    {
    }
    
    public class SubWidgetEnumerator implements WidgetEnumerator
    {
        public List getAllWidgets() { ... }
    }
    

    How it works: for any subclass of Abs, it is forced to provide an implementation of WidgetEnumerator. subclass author cannot forget that. Now invocation Abs.getAllWidgets(Sub.class) contains sufficient information to resolve that implementation, i.e. SubWidgetEnumerator. It is done through reflection, but it is type safe, there are no string literals involved.

    0 讨论(0)
  • 2020-12-29 05:17

    static methods can't be abstract because they aren't virtual. Therefore anywhere that calls them has to have the concrete type with the implementation. If you want to enforce that all implementations of an interface have a certain static method, then that suggests a unit test is required.

    abstract class A
    {
        public static void foo()
        {
            java.lang.System.out.println("A::foo");
        }
    
        public void bar()
        {
    
            java.lang.System.out.println("A::bar");
        }
    }
    
    class B extends A
    {
        public static void foo()
        {
            java.lang.System.out.println("B::foo");
        }
    
        public void bar()
        {
    
            java.lang.System.out.println("B::bar");
        }
    }
    
    public class Main
    {
        public static void main(String[] args)
        {
            B b = new B();
            b.foo();
            b.bar();
    
            A a = b;
            a.foo();
            a.bar();
        }
    }
    
    0 讨论(0)
  • 2020-12-29 05:17

    It doesn't make sense to do what you're asking:

    Why can't static methods be abstract in Java

    0 讨论(0)
  • 2020-12-29 05:20

    I too am dealing with this problem. For those that insist that it "doesn't make sense", I would invite you to think outside of that semantic box for a moment. The program I am working with is inherently about reflection.

    Reflection, as you know, can take three orders of magnitude longer than straight-up binary function calling. That is an inevitable problem, and the software needs to port to as many machines as possible, some of which will be 32 bit and slower than my development machine to begin with. Thus, the applicability of a class to the requested operation needs to be checked via a static method, and all of the reflective methods are run at once during module booting.

    Everything works, first and foremost. I've built the entire thing. The only catch is that a module can be compiled in a .class without compile time checking to see if the identifying static function exists at all, resulting in an innately useless class. Without the identifier, and its included information, for security's sake the module is not loaded.

    I clearly understand the issue with the complete definition of "abstract" and "static", and understand that they don't make sense together. However, the ability to have a class method that is compiler-enforced for inclusion is lacking in Java, and as much as I like the language, I miss it. Thus, this is a human constraint on every programmer that ever works on the software, which I'm sure we can all agree is a pain.

    0 讨论(0)
提交回复
热议问题