原创的,转载可注明地址,转载自菜鸟乙。。。谢谢!

 

Test类:

package com.geely.design.pattern.behavioral.interpreter;


/**
 * Created by ddwei
 *
 */
public class TestExtends {

    public static void main(String [] args){
        Son son = new Son();
        son.hand();
    }
}

 

子类:

package com.geely.design.pattern.behavioral.interpreter;

public class Son extends Father {
    @Override
    public void say() {
        System.out.println("我是你儿子,你说啥就是啥!");
    }
}

 

父类:

package com.geely.design.pattern.behavioral.interpreter;

public class Father {
    public void say(){
        System.out.println("我是你爸爸!");
    }

    public void hand(){
        System.out.println("开始打儿子!");
        this.think();
    }

    public void think(){
        System.out.println("打在儿身,疼在我心!");
        this.say();
    }
}

 

打印日志:

开始打儿子!
打在儿身,疼在我心!
我是你儿子,你说啥就是啥!

Process finished with exit code 0

 

posted on 2020-02-16 12:16  菜鸟乙  阅读(77)  评论(0编辑  收藏  举报