関数 ArrayList()
{
var m_elements = []; //プライベートメンバーは継承できません
m_elements = Array.apply(m_elements, argument);
//ArrayList 型はコレクションを継承します
this.base = Collection ;
this.base.call(this, m_elements.length );
this.add = function()
{
return m_elements.push.apply(m_elements, argument); 🎜>}
this.toArray = function()
{
return m_elements;
}
}
ArrayList.prototype.toString = function()
{
return this.toArray().toString();
}
//ArrayList 型を継承する SortedList 型を定義します。
関数 SortedList()
{
// SortedList 型は ArrayList を継承します。
this.base = ArrayList;
this.sort = function()
{
var arr = this.toArray();
arr.sort.apply(arr, argument);
}
}
//ArrayList を構築します
var a = new ArrayList(1, 2,3);
dwn(a);
dwn (a.size()); //a は Collection
dwn(a.isEmpty) から継承します。 isEmpty() メソッドを継承しません
//SortedList を 1 つ作成します
var b = new SortedList(3,1,2); //b ArrayList から add() メソッドを継承
dwn(b.toArray() ); //b ArrayList から toArray() メソッドを継承
b.sort() メソッド
dwn(b.toArray());
dwn(b );
dwn(b.size()); //b はコレクション
から継承します🎜>2、プロトタイプ継承の例:
コードをコピー
コードは次のとおりです:
//Define a Point type
function Point(dimension)
{
this.dimension = dimension;
}
//Define a Point2D type, "inherits" Point type
function Point2D(x, y)
{
this.x = x;
this.y = y;
}
Point2D.prototype .distance = function()
{
return Math.sqrt(this.x * this.x this.y * this.y);
}
Point2D.prototype = new Point(2) ; //Point2D inherits Point
//Define a Point3D type and also inherits Point type
function Point3D(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}
Point3D.prototype = new Point(3); //Point3D also inherits the Point
//construction A Point2D object
var p1 = new Point2D(0,0);
//Construct a Point3D object
var p2 = new Point3D(0,1,2);
dwn( p1.dimension);
dwn(p2.dimension);
dwn(p1 instanceof Point2D); //p1 is a Point2D
dwn(p1 instanceof Point); //p1 is also a Point
dwn(p2 instanceof Point); //p2 is a Point
The above two methods are the most commonly used
3, instance inheritance method example:
Before talking about this method example, Let’s talk about the limitations of the construction inheritance method, as follows:
function MyDate ()
{
this.base = Date;
this.base.apply(this, arguments);
}
var date = new MyDate();
alert(date .toGMTString); //undefined, date does not inherit from the Date type, so there is no toGMTString method
Some methods of the core object cannot be inherited by the constructor because the core object is not as customized as ours How about
performing assignment or initialization operations
in the constructor like a normal object and replacing it with prototypal inheritance? , as follows:
function MyDate(){}
MyDate.prototype=new Date();
var date=new MyDate();
alert(date.toGMTString); //'[object]' is not a date object, and it still does not inherit the Date type!
Now, switch to instance inheritance:
function MyDate()
{
var instance = new Date(); //instance is a newly created date object
instance.printDate = function(){
document. write("
" instance.toLocaleString() "
");
} //Extend the printDate() method to instance
return instance; //Return instance as the constructor Value return
}
var myDate = new MyDate();
dwn(myDate.toGMTString()); //The correct time string was successfully output this time. It seems that myDate is already a Date. Instance
, inheritance successful
myDate.printDate(); //If there is no return instance, it cannot be accessed with subscripts, because it is a private object method
4, copy inheritance method Example:
Function.prototype.extends = function(obj)
{
for(var each in obj)
{
this.prototype[each] = obj[each];
//Copy the properties of the object one-to-one, but It is slow and prone to problems
//So this "inheritance" method is generally not recommended
}
}
var Point2D = function(){
//...
}
Point2D.extends(new Point())
{
//...
}
This inheritance method seems to be rarely used.
5, mixed inheritance example:
function Point2D( x, y)
{
this.x = x;
this.y = y;
}
function ColorPoint2D(x, y, c)
{
Point2D .call(this, x, y); //This is construction inheritance, calling the constructor of the parent class
//From the previous example, this is equivalent to
//this.base=Point2D ;
//this.base.call(this,x,y);
this.color = c;
}
ColorPoint2D.prototype = new Point2D(); //Prototype is used here Inherit and let ColorPoint2D take the Point2D object as the prototype