java自定义before和after

package com.ada.wuliu.worker.web.cooperation.worker;

public class TestOne {
    abstract class Father{
        private int id;
        void before(){
            id=1;
            System.out.println("before ---"+id);
        }
        void doSmThing(){
            before();body(id);after();
        }
        abstract void body(int id);
            void after(){
            id=3;
            System.out.println("after ---"+id);
        }
    }
    class ChildOne extends Father{

        @Override
        public void body(int id) {
            // TODO Auto-generated method stub
            System.out.println("ChildOne  "+id);
        }
    
    }
    class ChildTwo extends Father{

        @Override
        public void body(int id) {
            // TODO Auto-generated method stub
            id=2;
            System.out.println("ChildTwo ---"+id);
        }
    
    }
    void hehe(Father father){
        father.doSmThing();
    }
    public static void main(String[] args) {
        TestOne testOne=new TestOne();
        TestOne.ChildOne childOne= testOne.new ChildOne();
        testOne.hehe(childOne);
    }
    }

 

posted @ 2017-09-27 12:33  流氓剑客  阅读(4244)  评论(0编辑  收藏  举报