Why can't static methods be abstract in Java?

后端 未结 25 2300
不思量自难忘°
不思量自难忘° 2020-11-22 08:34

The question is in Java why can\'t I define an abstract static method? for example

abstract class foo {
    abstract void bar( ); // <-- this is ok
    ab         


        
25条回答
  •  -上瘾入骨i
    2020-11-22 09:00

    This is a terrible language design and really no reason as to why it can't be possible.

    In fact, here is an implementation on how it CAN be done in JAVA:

    public class Main {
    
            public static void main(String[] args) {
                    // This is done once in your application, usually at startup
                    Request.setRequest(new RequestImplementationOther());
    
                    Request.doSomething();
            }
    
            public static final class RequestImplementationDefault extends Request {
                    @Override
                    void doSomethingImpl() {
                            System.out.println("I am doing something AAAAAA");
                    }
            }
    
            public static final class RequestImplementaionOther extends Request {
                    @Override
                    void doSomethingImpl() {
                            System.out.println("I am doing something BBBBBB");
                    }
            }
    
            // Static methods in here can be overriden
            public static abstract class Request {
    
                    abstract void doSomethingImpl();
    
                    // Static method
                    public static void doSomething() {
                            getRequest().doSomethingImpl();
                    }
    
                    private static Request request;
                    private static Request getRequest() {
                            // If setRequest is never called prior, it will default to a default implementation. Of course you could ignore that too. 
                            if ( request == null ) {
                                    return request = new RequestImplementationDefault();
                            }
                            return request;
                    }
                    public static Request setRequest(Request r){
                            return request = r;
                    }
    
            }
    }
    

    ================= Old example below =================

    Look for getRequest, and getRequestImpl ... setInstance can be called to alter the implementation before the call is made.

    import java.io.IOException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    /**
     * @author Mo. Joseph
     * @date 16 mar 2012
     **/
    
    public abstract class Core {
    
    
        // ---------------------------------------------------------------        
        private static Core singleton; 
        private static Core getInstance() {
            if ( singleton == null )
                setInstance( new Core.CoreDefaultImpl() );  // See bottom for CoreDefaultImpl
    
            return singleton;
        }    
    
        public static void setInstance(Core core) {
            Core.singleton = core;
        }
        // ---------------------------------------------------------------        
    
    
    
        // Static public method
        public static HttpServletRequest getRequest() {      
            return getInstance().getRequestImpl();
        }
    
    
        // A new implementation would override this one and call setInstance above with that implementation instance
        protected abstract HttpServletRequest getRequestImpl();
    
    
    
    
        // ============================ CLASSES =================================
    
        // ======================================================================
        // == Two example implementations, to alter getRequest() call behaviour 
        // == getInstance() have to be called in all static methods for this to work
        // == static method getRequest is altered through implementation of getRequestImpl
        // ======================================================================
    
        /** Static inner class CoreDefaultImpl */
        public static class CoreDefaultImpl extends Core { 
            protected HttpServletRequest getRequestImpl() {
                return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            }
        }
    
         /** Static inner class CoreTestImpl : Alternative implementation */
        public static class CoreTestImpl extends Core { 
            protected HttpServletRequest getRequestImpl() {
                return new MockedRequest();
            }
        }       
    
    }
    

    Used as follow:

    static {
         Core.setSingleton(new Core.CoreDefaultImpl());
    
         // Or
    
         Core.setSingleton(new Core.CoreTestImpl());
    
         // Later in the application you might use
    
         Core.getRequest(); 
    
    }
    

提交回复
热议问题