js面向對象的三大特性


0x00:使用OOP技術,常常要使用許多的代碼模塊,每個模塊都提供特定的功能,每個模塊老師孤立的,甚至與其它的模塊完全獨立,這種模塊化的編程方法大大的提供了代碼實現的多樣性,大大增加了代碼的重用性。js並不是直接的oop語言,但是可以通過模擬的方式做到其它很多oop語言才能做到的事情,如繼承、多態、封裝(沒有做不到,只有想不到)

0x01:對象的屬性

對象的屬性分為對象屬性、私有屬性 和類屬性

對象屬性:要創建對象后才能使用

 私有屬性:在內部就可以使用,在外部需要通過閉包才能使用

類屬性可以通過對象名稱直接使用

     function func(){
         this.objPro1 = "對象屬性";
        func.prototype.objPro2 = "對象屬性";
         var privatePro = "私有屬性";
    }

    func.classPor = "類屬性";
    console.log(func.classPor);    
     var f =  new func();
    console.log(f.objPro1);

    console.log(f.objPro2);         

 結果如圖 :

 

 我們再訪問一下私有屬性試試看

console.log(f.privatePro); 

 結果如圖:

 

 在內部訪問私用屬性:

     function func(){
         this.objPro1 = "對象屬性";
        func.prototype.objPro2 = "對象屬性";
         var privatePro = "私有屬性";
         this.getter =  function(){
             return privatePro;
        }
    }

    func.classPor = "類屬性";
     var f =  new func();

    console.log(f.getter()); 

 結果如圖:

 

還有就是通過閉包的方式來取得私有變量

     function func(){
         this.objPro1 = "對象屬性";
        func.prototype.objPro2 = "對象屬性";
         var privatePro = "私有屬性";
         this.getter =  function(){
             return privatePro;
        }
         return  function(){
             return privatePro;
        }
    }
     var fun =  new func();

    console.log(fun()); 

看結果: 

 

 

0x02:對象方法

對象方法包括:對象方法、私有方法和類方法,使用類似 前面的屬性

 

     function demoClass(){

         // 私有方法
         var privateMethod =  function(){
            console.log("this is a private method");
        }

         // 屬性方法
         this.objMethod =  function(){
            console.log("this is a object method");
        }

        demoClass.prototype.proMethod =  function(){
            console.log("this is a object method too");
        }
    }

    demoClass.classMethod =  function(){
        console.log("this is class method");
    }

     var f =  new demoClass();
    f.objMethod(); // 調用屬性方法
    f.proMethod(); // 調用屬性方法

 結果如圖:

 

 那我們再調用一下私有方法試一下:

f.privateMethod(); //調用私有方法 

  

 調用一下類方法:

 f.classMethod();//調用類方法 

 

demoClass.classMethod(); //調用類方法

 

總結:

1,對象方法是可以通過實例被調用的,無論是類內的屬性方法或者是原型方法構造的屬性方法。

2,類方法不能通過實例被調用,可以通過類名"."類方法()的模式調用。 類方法也叫靜態方法

 

0x03:繼承、封裝、多態

 js實現繼承是通過 apply方法或者是通過prototype實現的,如果使用apply方法,子類的原型是子類,如果使用prototype,那么子類的原型也將繼承父類。

     function fatherClass(name,age){
         this.name = name;
         this.age = age;
         this.say =  function(){
            console.log(" the say method of father class");
        }
    }

     function oneSon(name,age){
        fatherClass.apply( this,[name,age]);
    }

     var objOneSon =  new oneSon("oneSon",20);
    console.log(objOneSon.name);
    console.log(objOneSon.age);
    objOneSon.say();
    console.log(objOneSon  instanceof fatherClass);

    console.log(objOneSon instanceof oneSon); 

 結果如圖:

 如果使用prototype方法

     function fatherClass(name,age){
         this.name = name;
         this.age = age;
         this.say =  function(){
            console.log(" the say method of father class");
        }
    }

     function oneSon(name,age){
        fatherClass.apply( this,[name,age]);
    }
    oneSon.prototype =  new fatherClass();
     var objOneSon =  new oneSon("oneSon",20);
    console.log(objOneSon.name);
    console.log(objOneSon.age);
    objOneSon.say();
    console.log(objOneSon  instanceof fatherClass);

    console.log(objOneSon instanceof oneSon); 

結果如圖:

 

子類的方法會覆蓋父類的方法,即表現的是多態性

     function fatherClass(name,age){
         this.name = name;
         this.age = age;
         this.say =  function(){
            console.log(" the say method of father class");
        }
    }

     function anotherSon(name,age){
         this.say =  function(){
            console.log("i am anotherSon");
        }
    }

    anotherSon.prototype =  new fatherClass();

     function threeSon(name,age){
         this.say =  function(){
            console.log("i am threeSon");
        }
    }

    threeSon.prototype =  new fatherClass();

     function yes_or_no(cls){
         if(cls  instanceof fatherClass){
            cls.say();
        }
    }

     var objanotherSon =  new anotherSon();
     var objthreeSon =  new threeSon();
    yes_or_no(objanotherSon);

    yes_or_no(objthreeSon);

 結果如圖所示:

 

 即實現了類的多態。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM