1. 程式人生 > >對象—封裝、繼承

對象—封裝、繼承

一個 對象的訪問 nes sta spa 總結 重用 類的屬性 ron

對象2

面向對象的三大特征:1、封裝 2、繼承 3、多態

封裝

  • 概念:實現隱藏細節的過程。
  • 好處:1、重用
  • 2、隱藏實現的細節(可設置訪問權限/級別)
  • 不限於面向對象,無處不在

純面向對象的訪問級別:

  • public 級別最低,均可訪問
  • protected 僅限父與子之間
  • package 作用域,命名空間
  • private 級別最高

JS都是public,都可訪問;可模擬private;不需要protected、package

let Student=function(name,age){
    this.name=name;
    //  let age =age; 報錯,age已定義; 局部變量
this.show=function(){ console.log(我是+this.name); } } let stu1=new Stu(學生,20); stu1.show(); 或 let Student=function(name,age){ this.name=name; let_age=age; // let age =age; 報錯,age已定義; 局部變量 this.show=function(){ console.log(`我是${this.name},今年¥{_age}歲); } } let stu1
=new Stu(學生,20); console.log(stu1_age); stu1.show();

修改器 set, 訪問器 get

let Student=function(name,age,sex){
    this.name=name;
    this.age=age;
    this.sex=sex;
    this.show=function(){
        console.log(`我是${this.name},今年${this.age}歲,性別${this.sex}`);
    }
}                                                 
name 屬性名,字符串 Object.defineProperty(Student.prototype,name,{ //objec.defineProperty固定寫法,Student.prototype設置set/get的對象 set:function(name){ // 最後單獨設置了名字,進入這一句,輸出改變後的名字和‘我是set方法‘ console.log(我是set方法); this._name=name; // _name 初始化,必須加下劃線 }, // 作為參數,用逗號 get:function(){ // 不管最後有沒有單獨設置其他名字,因為有return,都要進入這一句,輸出‘我是get方法‘ console.log(我是get方法); //可有可無 return this._name; } }); let stu1=new Student(vivi,22,); console.log(stu1.name); stu1.name=json; stu1.age=30; stu1.sex=man; stu1.show();

繼承

  • 概念:一個類沿用了父類的屬性、方法,並且可擁有自己的屬性、方法的行為就是繼承。
  • 好處:1、重用 2、擴展(自己的東西)
  • 缺陷:(零型?)繼承

JS-單根繼承,缺陷得到一定解決;es6以前JS沒有繼承方法

繼承方法

1、對象冒充法

let People =function(name,age){
    this.name=name;
    this.age=age;
}
People.prototype.into=function(){   // 方法;可繼承共有的;into自己取的名字
    console.log(`Hi,我是${this.name}`);
}
let ChinesePeople=function(name,age){
    this.inhert=People;  
    this.inhert=(name,age);  // 不是繼承,只是調用別人的  
    delete this.inhert;      // delete 關鍵字
}
let c1=new ChinesePeople("姓名",22);
console.log(c1.name,c1.age);     // 輸出 姓名,20
c1.intro(); // 檢測,輸出 空; (1)
console.log(c1.instanceof CinesePeople);  // instanceof 檢測;輸出true; (2)
console.log(c1.instanceof People); // 輸出false;不是繼承;(3)

call/apply區別:傳參 call(參數列表) apply(數組)

同上

let ChinesePeople=function(name,age){
    People.call(this,name ,age);
    People.apply(this[name,age]);

}
let c1=new CinesePeople("姓名",22);

2、原型鏈

  • 把原來的原型改為另一對象,作為原型
  • 能變成真的繼承
  • 缺陷:不能傳值

同上
...
let c1=new ChinesePeople(姓名22);
console.log(c1.__proto__);  // 訪問原型;__proto__ 是2條下劃線; ChinesePeople{}...
console.log(c1.__proto__.__proto__.__proto__); //一級一級往上訪問原型 

3、混合方式

let ChinesePeople=function(name,age){
    People.call(this,name,age);   // 加入這句

}
ChinesePeople.prototype = new People();
let c1 = new ChinesePeople("姓名",20);
let c2 = new ChinesePeople("姓名",21);

console.log(c1.name,c1.age);
c1.intro();

console.log(c1 instanceof People);

prototype 和 proto 區別:

  • prototype:1、構造函數的方法屬性 2、可改變原型
  • proto: 1、是每個函數的屬性,通過對象依次往上找原型 2、不可改變原型,只能訪問

總結

  • 面向對象:把復雜問題簡單化,更接近人類思維
  • 對象本身是一種數據類型,包含很多屬性的集合,方法也是屬性
  • es5:要初始化必須加_下劃線封裝
  • 封裝:運用 修改器set、訪問器get , 必須寫 Object.definePropertype;
  • 例:
    
     Object.definePropertype(Teacher.prototype,name{
        set:function(name){
           this.name=name;
        },   // 作為參數,使用逗號
        get:function(){
            return this._name;
        }
    
    });

  • 繼承:1、對象冒充法不是繼承,運用關鍵字delete,輸出可用intro、intanceof檢測

  • 2、原型鏈:能變成真的繼承,運用proto ,可一級一級訪問原型,檢測;但是不能傳值
  • 3、混合方式:加入 call 或 apply

對象—封裝、繼承