Call super class method automatically

前端 未结 4 1126
北海茫月
北海茫月 2021-01-17 17:27

Consider the following class

class A{
    public void init(){
        //do this first;
    }
    public void atEnd(){
        //do this after init of base cl         


        
4条回答
  •  醉话见心
    2021-01-17 18:04

    Make init() final, and provide a separate method for people to override that init() calls in the middle:

    class A{
        public final void init(){
            //do this first;
        }
    
        protected void initCore() { }
    
        public void atEnd(){
            //do this after init of base class ends
        }
    }
    
    class B1 extends A{
    
        @Override
        protected void initCore()
        {
            //do new stuff.
        }
    }
    

提交回复
热议问题