多继承

by shitao1988

JavaScript

dojo.declare(
    "Shape", // 类名
null, // 无父类,使用null
{
    color: 0,
    setColor: function (color) {
        this.color = color;
    }
});

dojo.declare(
    "Circle",
Shape, {
    radius: 0,
    constructor: function (radius) {
        this.radius = radius || this.radius;
    },

    setRadius: function (radius) {
        this.radius = radius;
    },

    area: function () {
        return Math.PI * this.radius * this.radius;
    }
});


dojo.declare(
    "Rectangle",
Shape, {
    length: 0,
    width: 0,

    constructor: function (l, w) {
        this.length = l || this.length;
        this.width = w || this.width;
    },

    setLength: function (l) {
        this.length = l;
    },

    setWidth: function (w) {
        this.width = w;
    },

    area: function () {
        return this.length * this.width;
    }
});


dojo.declare(
    "Position",
    null,
    {
        x: 0,
        y: 0,

        constructor: function(x, y) {
            this.x = x || this.x;
            this.y = y || this.y;
        },

        setPosition: function(x, y) {
            this.x = x;
            this.y = y;
        },

        move: function(deltaX, deltaY) {
            this.x += deltaX;
            this.y += deltaY;
        }
    }
);

dojo.declare(
    "PositionedCircle",
    [Circle, Position],
    {
        constructor: function(radius, x, y) {
            this.setPosition(x, y);
        }
    }
);

var pc=newPositionedCircle(5,1,2);
//测试shape功能
var colorl=pc.color;//colorl为黑色
pc.setColor(0x0000FF);
var color2=pc.color;//color2为红色
//测试circle功能
varradiusl=pc.radius;//radiusl为5
var areal=pc.area();//areal为78.54

pc.setRadius(10);
var radius2=pc.radius;
var area2=pc.area();
//测试Position功能
var positionl=[pc.x,pc.y];
pc.move(3,5);
var position2=[pc.x,pc.y];