zoukankan      html  css  js  c++  java
  • JavaScript面向对象继承

    javascript面向对象继承的三种方法:

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>Untitled Page</title>
        
    <script language="javascript" type="text/javascript">
        
    //基类
        function Person()
        
    {
            
    this.Name="Person";
            
    this.Sex="NONE";
            
    this.Age="?";
            
    this.SayName=function(){alert(this.Name);};
            
    this.SaySex=function(){alert(this.Sex);};
            
    this.SayAge=function(){alert(this.Age);};
        }

        
    //子类
        function ManPerson()
        
    {   
            
    this.Name="ManPerson";
            
    this.Sex="Man";
            
    this.Age="20"
            Person.apply(
    this);//执行该语句时会调用Person中的构造器,先前赋值的ManPerson,Man,20就失去作用了,所以这句话
            
    //要放在this.Name="ManPerson";之前才能即继承Person的方法,又不会覆盖我们的赋值操作。
        }

        
        
    //第一种方法
        function first(){
        var p
    =new Person();
        alert(
    "Name:"+p.Name+"  Sex:"+p.Sex+"  Age:"+p.Age);//执行结果为Name:Person  Sex:NONE  Age:?
        p.SayName();//执行结果Person
        var mp=new ManPerson();
        alert(
    "Name:"+mp.Name+"  Sex:"+mp.Sex+"  Age:"+mp.Age);//apply在赋值后结果为:Name:Person  Sex:NONE  Age:?
        
    //在赋值前结果为:Name:ManPerson  Sex:Man  Age:20
        mp.SaySex();//执行结果Man
        
    //可以看到ManPerson很好的继承了Person
        }

        
        
    //第二种方法
        function second(){
        
    for(pro in Person)
        
    {
            ManPerson[pro]
    =Person[pro];
        }

        var p
    =new Person();
        alert(
    "Name:"+p.Name+"  Sex:"+p.Sex+"  Age:"+p.Age);//执行结果为Name:Person  Sex:NONE  Age:?
        p.SayName();//执行结果Person
        var mp=new ManPerson();
        alert(
    "Name:"+mp.Name+"  Sex:"+mp.Sex+"  Age:"+mp.Age);//执行结果为Name:Person  Sex:NONE  Age:?
        mp.SaySex();//执行结果NONE
        mp.Name="ManPerson";
        mp.SayName();
    //执行结果:ManPerson
        
    //可以看到ManPerson继承了Person的SayName
        }


        function third()
    {
        
    //第三种方法
        ManPerson.prototype=Person.prototype;
        var mmp
    =new ManPerson();
        mmp.SayName();
    //执行结果:Person
        mmp.Name="ManPerson";
        mmp.SayName();
    //执行结果:ManPerson
        
    //ManPerson继承了Person的方法
        }

        
    </script>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
    <button value="FirstMethod" onclick="first()">FirstMethod</button><br />
        
    <button validationgroup="SecondMethod" onclick="second()">SecondMethod
        
    </button><br />
        
    <button value="ThirdMethod" onclick="third()">ThirdMethod</button>
        
    </div>
        
    </form>
    </body>
    </html>

    原创文章,转载请注明出处!
    All CopyRight Reserved !

    主页:http://jingtao.cnblogs.com

    QQ:307073463
    Email:jingtaodeemail@qq.com
    MSN:sunjingtao@live.com

  • 相关阅读:
    第三次博客园作业
    centos7+jdk1.8+tomcat8 配置https
    输入30个数存入数组a,求出数的每个位数的平方和存入数组b,从小到大排列后输出(C语言)
    50个[100,300]的随机数,要求用二分法查找从键盘录入的关键数字。找到回复位置,找不到回复不存在(C语言)
    产生20个随机数,在[200,400]内,其中能被5整除的存入数组array2,要求输出array2中的平均值(C语言)
    最小生成树
    PTA路径判断
    PTA构造哈夫曼树
    图的其中两种表示方式
    中序遍历树并判断是否为二叉搜索树
  • 原文地址:https://www.cnblogs.com/jingtao/p/1175668.html
Copyright © 2011-2022 走看看