漫漫技术人生路

C#

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

function Person(name,age){   //定义一个类,人类
    this.name=name     //名字
    this.age=age       //年龄
    this.sayhello=function(){alert("hello")}
}

function Print(){            //显示类的属性
    this.funcName="Print"
    this.show=function(){    
        var msg=[]
        for(var key in this){//遍历参数
            if (typeof(this[key])!="function") msg.push([key,":",this[key]].join(""))
        }
        alert(msg.join("\n"))
    }
}

function Student(name,age,grade,school){    //学生类
    Person.apply(this,arguments)
    Print.apply(this,arguments)
    this.grade=grade                  //年级
    this.school=school                    //学校
}

var p1=new Person("jake",10)
p1.sayhello()

var s1=new Student("tom",13,6,"清华小学")
s1.show()//inherit from Print, after that,because of using instance of Student ,this will refrence to Student
s1.sayhello()
alert(s1.funcName)
/*
学生类本来不具备任何方法,但是在 Person.apply(this,arguments)  后,他就具备了 Person类的sayhello方法和
所有属性。  在 Print.apply(this,arguments) 后就自动得到了  show() 方法。

*/

posted on 2006-12-15 09:47  javaca88  阅读(218)  评论(0)    收藏  举报