回调机制:父亲与儿子

问题描述:父亲让儿子写作业,然后父亲去工作,当儿子写完作业后父亲需要检查儿子写的作业。

①:请用Java回调机制模拟这个场景;

②:当父亲和儿子都是一个线程时,模拟这个场景。

问题①分析:

分析结束,那么可以给出代码了:

Father类:

public class Father {

    private Son son = new Son(this);;

    public void call(){
        System.out.println("父亲让儿子写作业");
        son.doTask();
    }

    public void check(){
        System.out.println("父亲检查儿子的作业");
    }
}

Son类:

public class Son {

    private Father father;

    public Son(Father f) {
        father = f;
    }

    public void doTask(){
        System.out.println("儿子做作业");
        father.check();
    }
}

Client类:

public class Client {

    public static void main(String[] args) {
        Father father = new Father();
        father.call();
    }
}

实现效果:

此时,让Father实现一个回调接口ICallBack,那么ICallBack接口声明的对象就可以回调ICallBack接口的实现类中的方法,实现如下:

ICallBack接口:

public interface ICallBack {
    void check();
}

Father类:

public class Father implements ICallBack {

    private Son son;

    public void call(){
        System.out.println("父亲让儿子做作业");
        son = new Son(this);
        son.doTask();
    }

    public void check(){
        System.out.println("父亲检查儿子的作业");
    }
}

Son类:

public class Son {

    private ICallBack father;

    public Son(Father f) {
        father = f;
    }

    public void doTask(){
        System.out.println("儿子做作业");
        father.check();
    }
}

Client类:

public class Client {
    public static void main(String[] args) {
        Father father = new Father();
        father.call();
    }
}

实现效果:

问题②分析:

问题其实很简单:本质:父亲和儿子各自的工作内容不变,我画图说明:

Father的run方法先执行son.start()方法,这时Son开始写作业,写完后打断爸爸工作让他check()就好了。

代码如下:

Father类:

public class Father extends Thread implements ICallBack {

    private Son son = new Son(this);

    public void run(){
        System.out.println("父亲让儿子做作业");
        son.start();
        int k = 1;
        for (int i=0; i<100000; i++){
            if (this.isInterrupted()){
                if (k == 1){
                    check();
                    k--;
                }
            }
            if (k != 1 && i%100 == 1){
                System.out.println("父亲继续工作");
            }
            else if (k == 1 && i%100 == 1)
                System.out.println("父亲工作中");
        }
    }

    public void check(){
        System.out.println("父亲检查儿子的作业");
    }
}

Son类:

public class Son extends Thread {

    private Father father;

    public Son(Father f) {
        father = f;
    }

    public void run(){

        for (int i=0; i<10000; i++){
            if (i%100 == 0)
                System.out.println("儿子做作业");
        }
        System.out.println("儿子做完了作业");
        father.interrupt();
    }
}

ICallBack接口:由于多态的限制(ICallBack接口声明的对象不能使用Father类中独有的方法:例如father.join()),我就用Father声明对象了,所以说,这个接口在这没用0.0,不过写上也没错。

public interface ICallBack {
    void check();
}

Client类:

public class Client {
    public static void main(String[] args) {
        Father father = new Father();
        father.start();
    }
}

实现效果:

谢谢观看!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值