JS_JavaScript类_超类与子类_2011-08-25;

/**
 * 奇谈怪论:JavaScript类_超类与子类_2011-08-25;
 * 超类:
 * Here is a simple Rectangle(矩形) class.It has a width and height and can compute its own area.    
 * @param {} w
 * @param {} h
 */
function Rectangle(w, h) {
    // 私有属性:
    this.width = w;
    this.height = h;
}
Rectangle.prototype.area = function() { // JavaScript采用原型继承机制;此为实例方法,子类会通过继承也拥有此方法;
    return this.width * this.height;    //矩形面积;
}
Rectangle.prototype.toString = function() {
    return "[" + this.width + "," + this.height + "]";
}

/**
 * 子类:
 * @param {} x
 * @param {} y
 * @param {} w
 * @param {} h
 */
function PositionedRectangle(x, y, w, h) {
    // First, invoke the superclass constructor on the new object so that it can initialize the width and height.
    // We use the call method so that we invoke the constructor as a method of the object to be initialize.
    // This is called constructor chaining(链).
    Rectangle.call(this, w, h);    //Rectangle需要传递宽度和高度两参数;
    
    // Now store the position of the upper-left corner(左上角) of the rectangle.
    this.x = x;
    this.y = y;
}

// If we use the default prototype object that is created when we define the PositionedRectangle() constructor,
// we get a subclass of object. To subclass Rectangle, we must explicitly(显示地) create our prototype object.
PositionedRectangle.prototype = new Rectangle();

// we create this prototype object for inheritance purposes(目的), but we don't actually(实际上) want to inherit the
// width and height properties that each Rectangle object has, so delete them from the prototype.
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;

// Since the prototype object was created with Rectangle() constructor, it has a constructor property that
// refers to that constructor. But we want PositionedRectangle objects to have a different constructor property,
// so we've got to reassign(再分配) this default constructor property.
PositionedRectangle.prototype.constructor = PositionedRectangle;

// Now that we've configured the prototype object for out subclass, we can add instance methods to it.
PositionedRectangle.prototype.contains = function(x, y){    //判断传入的点是否在矩形框里边;
    return (x > this.x && this.x < this.x + this.width && y > this.y && y < this.y + this.height);
}
PositionedRectangle.prototype.toString = function() {// 覆盖Rectangle超类的toString()方法
    return "(" + this.x + "," + this.y + ")" + //子类的属性,而width属性和height属性委托给其超类。
            Rectangle.prototype.toString.apply(this);     // chain to superclass
}
// 特别说明:如果PositionedRectangle下边没有子类,可以使用如下方式:
//PositionedRectangle.prototype.toString = function() {
//    return "(" + this.x + ", " + this.y + ")" +
//            this.superclass.prototype.toString.apply(this);
//}


/**
 * 运用子类:
 * 特别说明:如果在控制台运行,请使用console.log()方式打印。
 */
var r = new PositionedRectangle(2, 2, 2, 2);
print(r.contains(3, 3));    // invoke an instance method
print(r.area());            // invoke an inherited instance method(实例方法)

// Use the instance fields(字段,或者说是属性) of the class:
print(r.x + "," + r.y + "," + r.width + "," + r.height);

// Our object is an instance of all 3 of these classes
print(r instanceof PositionedRectangle &&
      r instanceof Rectangle &&
      r instanceof Object);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值