/**
* 奇谈怪论: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);
* 奇谈怪论: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);