Js Code
<Html>
<Head>
<Title> New Document </title>
<Script src = "js/prototype. js" type = "text/javascript"> </script>
</Head>
<Script tupe = "text/javascript">
// The method used to create classes in javascript cannot reference prototype. js. Otherwise, conflicts may occur.
Var People = function (){
This. name = ""; // attribute
This. getName = function (){
Return "the host named me. My name is" + this. name + ". Master, do you want me to be a pretty girl or handsome guy? "; // Method
}
This. say = function (){
Return "Hello, Master! I am your robot and the number is: "+ Person. getNum () +". Give me a name"
}
This. sex = "man"
This. getSex = function (){
Return "I am a" + this. sex;
}
}
People. num = 0; // static attribute
People. getNum = function () {// static method
People. num = People. num + 1; // use People or this
Return People. num;
}
// Use prototype. js to create a class
Var Person = Class. create (); // use the Class. create method to create an empty Class
// Add non-static attributes and Methods
/** Method 1 :*/
Person. prototype = {// define the method to prototype. Note that the class attributes are initialized through the initalize method.
Name: "name ",
Initialize: function (name) {// This is a constructor and must be implemented and non-static
Alert ("an initial parameter object ...... ")
This. name = name;
},
Say: function (message ){
Return this. name +: + message;
}
};
/** Method 2:
Object. extend (Person. prototype ,{
Initialize: function (){
Alert ("No parameter object ...... ")
},
Email: lkujhn@qq.com ",
GetEmail: function (){
Return this. email
}
})
*/
// Add static attributes and Methods
// Method 1:
Object. extend (Person, {// extends some static attributes and Methods
Qq: "1, 103430585 ",
GetQq: function () {return this. qq ;}
});
/** Method 2:
Person. num = 0;
Person. getNum = function (){
Person. num = Person. num + 1; // use this. num.
Return this. name + ":" + Person. num;
}
*/
// Inherit
Var PersonSon = Class. create ();
// Initialize the subclass. If it is not initialized, a new object cannot be created. (when the non-static method of the subclass inherits the non-static method of the parent class, the constructor also inherits, so do not initialize)
/**
Object. extend (PersonSon. prototype ,{
Name: "personson ",
Initialize: function (name ){
This. name = name;
Alert (this. name + "Initial subclass PersonSon .................. ")
}
})
**/
// Inheritance form 1: (static) inherit the static method of the parent class. After inheritance, the method and attribute of the subclass are still static.
Object. extend (PersonSon, Person );
// Inheritance form 2: (non-static) inherit the static method of the parent class. After inheritance, the method and attribute of the subclass are non-static.
Object. extend (PersonSon. prototype, Person );
// Inheritance form 3: (static) inherit the non-static method of the parent class. After inheritance, the method and attribute of the subclass are static.
Object. extend (PersonSon, Person. prototype)
// Inheritance form 4: (non-static) inherit the non-static method of the parent class. After inheritance, the method and attribute of the subclass are non-static.
Object. extend (PersonSon. prototype, Person. prototype) // After inheritance, the constructor of the Child class will be overwritten by the parent class.
Function ceshi (){
Var pp = new People (); // create an object
// P. getNum () // call error
Alert (People. getNum () // call the static method
T (p. say (); // call a common method
Pp. name = "test" // change the attributes of an object
Alert (pp. getName ());
Alert (pp. getSex ());
/**
Var p = new Person ("lj"); // create an object
// Call a non-static method
Alert (p. say ("hello "));
Alert (p. getEmail ());
// Call the static method
Alert (Person. getQq ());
Alert (Person. getNum (); // non-static attributes cannot be accessed in static methods. In this method, this. name is undefined.
*/
Var ps = new PersonSon ("ljson"); // create a subclass
Alert ("the static method of the called subclass inherits the static method of the parent class:" + PersonSon. getQq ());
Alert ("The called subclass non-static method inherits the static method of the parent class:" + ps. getQq ());
Alert ("the static method of the called subclass inherits the non-static method of the parent class:" + PersonSon. say ("static method of the subclass "));
Alert ("The called subclass non-static method inherits the non-static method of the parent class:" + ps. say ("subclass non-static method "));
Alert ("non-static property (the property in the parent class is static):" + ps. qq );
Alert ("static property (the property in the parent class is static):" + PersonSon. qq)
Alert ("non-static property (the property in the parent class is not static):" + ps. name)
Alert ("static property (the property in the parent class is not static):" + PersonSon. name)
}
</Script>
<Body>
<Input type = "button" value = "robot" onclick = "ceshi ()"/>
</Body>
</Html>