example 1<Student inherit Person>
//example 1
//define super class Person
function Person(personId, name){
this.personId = personId || ""; //constructor
this.name = name || "";
/**
* meaning constructor
* if (name is undefined) this.name = "";
* else this.name = name;
*/
//getter and setter method
this.setPersonId = function(personId){
this.personId = personId;
return this;
}
this.getPersonId = function(){
return this.personId;
}
this.setName = function(name){
this.name = name;
return this;
}
this.getName = function(){
return this.name;
}
}
//define sub class Student
function Student(studentId){
this.studentId = studentId || "";
//getter and setter method
this.setStudentId = function(studentId){
this.studentId = studentId;
return this;
}
this.getStudentId = function(){
return this.studentId;
}
}
//Student inherit Person
Student.prototype = new Person; //***********
var student = new Student("0000000000")
.setPersonId("1111111111111")
.setName("Redcrow");
console.log(student);
//
example 2 <NumberFormatException inherit Exception>
//example 2
//define super class Exception
function Exception(name, message){
this.name = name || "Exception";
this.message = message || "";
//getter and setter method
this.printException = function(){
console.error(this.name + " : " + this.message);
}
this.getMessage = function(){
return this.message;
}
this.getName = function (){
return this.name;
}
this.toString = function(){
return this.name + " : [" + this.message + "]";
}
}
//define sub class NumberFormatException
function NumberFormatException(message){
this.message = message || "";
}
//NumberFormatException inherit Exception
NumberFormatException.prototype = new Exception("NumberFormatException");
try{
throw new NumberFormatException("hello NumberFormatException!");
}catch(ex){
console.log(ex);
console.log(ex.toString());
console.log("excetion name : " + ex.getName());
console.log("exception message : " + ex.getMessage());
if(ex instanceof NumberFormatException){
ex.printException();
console.log("ex instanceof NumberFormatException");
}
if(ex instanceof Exception){
console.log("ex instanceof Exception");
}
}finally{
console.log("finally");
}
//
/**
* public static class Class
*/
var Class = {
extends : function(subClass, superClass) {
var Define = function() {};
Define.prototype = superClass.prototype;
subClass.prototype = new Define();
subClass.prototype.constructor = subClass;
subClass.superClass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
};
/**
* Exception class
*/
var Exception = function(name, message){
this.name = name || "Exception";
this.message = message || "";
};
Exception.prototype.printException = function(){
console.error(this.name + " : " + this.message);
};
Exception.prototype.getMessage = function(){
return this.message;
};
Exception.prototype.getName = function (){
return this.name;
};
Exception.prototype.toString = function(){
return this.name + " : [" + this.message + "]";
};
//end Exception class
/**
* NumberFormatException class
*/
var NumberFormatException = function(message){
NumberFormatException.superClass.constructor.call(this, "NumberFormatException", message);
};
//NumberFormatException inherit Exception
Class.extends(NumberFormatException, Exception);
try{
throw new NumberFormatException("hello NumberFormatException!");
}catch(ex){
console.log(ex);
console.log(ex.toString());
console.log("excetion name : " + ex.getName());
console.log("exception message : " + ex.getMessage());
if(ex instanceof NumberFormatException){
ex.printException();
console.log("ex instanceof NumberFormatException");
}
if(ex instanceof Exception){
console.log("ex instanceof Exception");
}
}finally{
console.log("finally");
}
//


ไม่มีความคิดเห็น:
แสดงความคิดเห็น