UML类图关系在代码中的体现

UML类图中有继承、实现、依赖、关联、聚合、组合等几种关系,身边的小伙伴问我怎么区分,为此写了个简单的类,在注释里说明了这几种关系在代码中的体现。

public class Uml {

    /**
     * 实现关系的代码体现形式,通过implements关键字体现
     */
    static class People implements Serializable {

    }

    /**
     * 继承关系的代码体现形式,通过extends关键字体现
     */
    static class Child extends People {
        /**
         * 依赖关系的代码体现形式,体现在方法参数中。
         */
        void play(Computer computer) {

        }

    }

    /**
     * 聚合和组合本身都是一种关联关系。
     */
    static class Family {
        /**
         * 聚合关系的代码体现形式,体现在全局变量上,表示Family有多个孩子。
         */
        List children;
    }

    /////////////////////////////////////

    /**
     * 聚合和组合本身都是一种关联关系,关联关系的例子不再单列。
     */
    static class Computer {
        /**
         * 组合关系,computer由多个零件组成,关系比聚合更强
         */
        Cpu cpu;
        Memory memory;

    }

    static class Cpu {

    }

    static class Memory {

    }

}

你可能感兴趣的:(UML类图关系在代码中的体现)