JS實現oo編程 【call()函數-並且支持多重繼承】 @ R~福氣拉! :: 隨意窩 Xuite日誌
  • BloggerAds
  • 關鍵字
  • Search Google
  • Google
    1. 沒有新回應!
  • 看起來滿像樣
  • 工商時間
  • 2008-04-18 17:12 JS實現oo編程 【call()函數-並且支持多重繼承】
    平均分數:0 顆星    投票人數:0
    我要評分:

    Javascript實現oo編程 (JavaScript call()函數-並且支持多重繼承)
    <script language="javascript">
    <!--
    function Parent()//父類
    {   
     var self=this;     //私有屬性,子類裡不會繼承!
     var personalId="A1234567890"; //私有屬性   
     var personalTel="0911123456";
     var showParentPrivate=function()//私有方法,子類不能調用
     {
      alert("父類-私有方法");
      alert('父類名姓:'+ self.name +'\n\n父類ID:'+ personalId +'\n\n父類手機:'+ personalTel);
     }
        
     this.name="大頭";    //公有屬性
     this.age="45";
     this.officeTel="02-34958133"
     
     this.showParentPublic=function ()//公有方法
     {
      alert("父類-公有方法");
      alert('父類名姓:'+ self.name +'\n\n父類年齡:'+ self.age +'\n\n父類辦公室電話:'+ self.officeTel);
      //alert('父類名姓:'+ this.name +'\n\n父類年齡:'+ this.age +'\n\n父類辦公室電話:'+ this.officeTel); //2個是一樣的  
     } 
     
     this.PrivateToPublic=function(){ //在公有方法中使用私有方法
      alert("公有方法中使用私有方法");
      showParentPrivate();
     }
    }
    function Parent_Other()//父類-其它

     this.email="
    Parent@yahoo.com.tw";
    }
    function suber(){//子類 
     Parent.call(this);//通過這一句來繼承父類(Parent)類的可見屬性及方法(this)
    }
    function all(){//實現多重繼承
     
     suber.call(this);
     Parent_Other.call(this);
    }

    alert('Nxt.1111111111111111111111111111111111')
    var m=new Parent();
     m.showParentPublic();
     //m.showParentPrivate();//無法使用私有方法
     m.PrivateToPublic();

    alert('Nxt.2222222222222222222222222222222222')
    var p=new suber();
     p.showParentPublic();
     //p.showParentPrivate();//這一句會發生錯誤碼,因為showParentPrivate是父類的私有類 
     alert('父類的公有屬性,子類可讀取:'+p.name);
     alert('父類的私有屬性,子類不能讀取:'+p.personalId);

    alert('Nxt.3333333333333333333333333333333333') 
    var s=new all(); 
     
     alert('父類的公有屬性,子類可讀取:'+p.name);
     alert('父類的私有屬性,子類不能讀取:'+p.personalId);
     alert('email:'+s.email);

      
    //屬性&方法可以重覆a~~ (=.=)!!  所以很難debug~~
    alert('Nxt.4444444444444444444444444444444444')  
    Parent.prototype.LikeColoe = "red";
    Parent.prototype.TellLikeColor=function (){
     return "color: "+ this.LikeColoe
    }

    var cc1=new Parent();
    alert(cc1.TellLikeColor())


    Parent.prototype.LikeColoe = "green";
    Parent.prototype.TellLikeColor=function (){
     return "color: "+ this.LikeColoe
    }

    var cc1=new Parent();
    alert(cc1.TellLikeColor())

    //-->
    </script>

    小米 / Xuite日誌 / 回應(1) / 引用(0) / 好文轉寄
    回應