• 里氏代换原则(LSP)


    子类必须能替换成它们的父类(龙生龙,凤生凤,老鼠的儿子会打洞)

    可以这么理解,进公司之前人事当然会告诉你公司的制度,如果这个

    公司出得米米比较多,你可能会看都不看就签上你的大名,如果有一天

    人事告诉你因为你一个月因为连续迟到3天而将被开除,你肯定后悔

    没有认真看过制度,这个小例子说明一旦你继承某个类(成为公司员工),

    你就不该该改变它的基本行为(一个月迟到3次),否则你将死的很惨.

    (或者你是超人,可以拯救全人类)

    public interface IWorker {

       void eat();

       void work();

    }

    public class Manager {

    private IWorker iWorker;

    public IWorker getiWorker() {

    return iWorker;

    }

    public void setiWorker(IWorker iWorker) {

    this.iWorker = iWorker;

    }

       

    public void mange(boolean isWorkingHour)

    {

    if(isWorkingHour)

    iWorker.work();

    else

    iWorker.eat();

    }

    }

    public class Robert implements IWorker {

    @Override

    public void eat() {

    DebugLog.log("i am R2, i am eating");

    }

    @Override

    public void work() {

    DebugLog.log("i am R2, i am eating");

    }

    }

     
    public class Worker implements IWorker {
     
    @Override
    public void eat() {
    DebugLog.log("i am Jedi Warrior, i am eating");
     
    }
     
    @Override
    public void work() {
    DebugLog.log("i am Jedi Warrior, i am working");
    }
     
    }
    public class Test {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    IWorker aRobert=new Robert();
    IWorker aWorker= new Worker();
    Manager aManager=new Manager();
    aManager.setiWorker(aWorker);
    aManager.mange(true);
     
    aManager.setiWorker(aRobert);
    aManager.mange(false);
     
    }
     
    }
    工人是人,当然先要吃饭,才能做事,机器人不是人,可以24*7为人类服务
    ,从代码可以看出,机器人继承了iworker,导致机器人也需要吃饭。解决这个
    问题很简单,将吃饭和工作的接口分离出来,机器人只要实现work接口就可以了。
     
     
    public interface IEatable {
    void eat();
    }
     
    public interface IWorkable {
       void work();
    }
     
    public class Manager {
    private IWorkable iWorkable;
    private IEatable iEatable;
     
    public IWorkable getiWorkable() {
    return iWorkable;
    }
     
    public void setiWorkable(IWorkable iWorkable) {
    this.iWorkable = iWorkable;
    }
     
    public IEatable getiEatable() {
    return iEatable;
    }
     
    public void setiEatable(IEatable iEatable) {
    this.iEatable = iEatable;
    }
     
    public IWorkable getiWorker() {
    return iWorkable;
    }
     
    public void mangEatableWork(boolean isWorkingHour) {
    if (isWorkingHour)
    iWorkable.work();
    else
    iEatable.eat();
    }
     
    public void mangNonEatableWork() {
    iWorkable.work();
    }
    }
     
    public class Robert implements IWorkable{
     
    @Override
    public void work() {
    DebugLog.log("i am R2, i am working");
    }
     
    }
     
    public class Worker implements IWorkable,IEatable {
     
    @Override
    public void eat() {
    DebugLog.log("i am Jedi Warrior, i am eating");
     
    }
     
    @Override
    public void work() {
    DebugLog.log("i am Jedi Warrior, i am working");
    }
     
    }
     
    public class Test {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    Robert aRobert=new Robert();
     
    Worker aWorker= new Worker();
    Manager aManager=new Manager();
    aManager.setiWorkable(aWorker);
    aManager.setiEatable(aWorker);
    aManager.mangEatableWork(false);
     
    aManager.setiWorkable(aRobert);
    aManager.mangNonEatableWork();
     
    }
     
    }
     
  • 相关阅读:
    斐波那契数列
    进制转换为伪代码
    python模拟进程
    第十一周学习总结
    第十周学习总结
    第九周学习总结
    俄罗斯方块
    熟悉编程语言
    小学四则运算
    第八周学习总结
  • 原文地址:https://www.cnblogs.com/budoudou/p/2299696.html
Copyright © 2020-2023  润新知