zoukankan      html  css  js  c++  java
  • groovy动态类型--能力式设计

    动态类型

      动态类型中的类型是在运行时推断的,方法及其参数也是在运行时检查的。

    能力式设计

      被称作鸭子模式:他有这么一个观点:如果它走路像鸭子,叫起来也像鸭子,那么他就是一只鸭子。

    契约式设计

      相当于Java中定义的接口,他与能力式设计相对。

    使用动态类型语言要自律

      单元测试等等手段

    **
     * Created by Jxy on 2019/1/4 10:09
     * groovy动态类型
     * 没有实现任何接口,也没有扩展任何公共类
     * 这里是能力式设计
     * 能力式设计与契约式设计相对应
     *
     * 使用动态类型语言编程,而却没有使用单元测试的自律,感觉就像是在玩火。
     */
    def takeHelp(helper){
        helper.helpMoveThings()
        if(helper.metaClass.respondsTo(helper,"eatFood")){
            //helper.eatFood()
        }
    }
    
    class Man{
        void helpMoveThings(){
            println "man is helper"
        }
    }
    
    class Woman{
        void helpMoveThings(){
            println "woman is helper"
        }
    }
    
    class Elephant{
        void helpMoveThings(){
            println "elephant is helper"
        }
    }
    
    takeHelp(new Man())
    takeHelp(new Woman())
    takeHelp(new Elephant())

    运行结果:

    man is helper
    woman is helper
    elephant is helper
    
    Process finished with exit code 0
  • 相关阅读:
    返回一个一维整数数组中最大子数组的和02
    软工作业04四则运算网页版
    所有子数组的和的最大值
    学习进度表(第五,六周)
    学习进度表(第四周)
    四则运算2的单元测试
    《构建之法》阅读笔记02
    软工作业03
    单元测试练习
    学习进度表(第三周)
  • 原文地址:https://www.cnblogs.com/jsersudo/p/10218786.html
Copyright © 2011-2022 走看看