Package Object Oriented

Package: multiple permissions and data confidentiality, with the front end of which js not much since es6 Ye, ES5 good, no such characteristic syntax, can demonstrate ts

 

First talk about three key package
public completely open
protected subclass open
open to their own private
These three keywords can be used to define properties



// parent class, name, age can be made public, can not open weight 
class People {
    protected weight: any // 定义protected 属性
    constructor (public name:any, public age: any) {
        this.name = name
        this.age = age
        this.weight = 120
    }
    eat() {
        alert(`${this.name} eat something`)
    }
    speak() {
        alert(`my name is ${this.name}, age ${this.age}`)
    }
}

// 子类
class Student extends People {
    number
    private girlfriend // 定义private属性
    constructor(name, age, number) {
        super(name, age);
        this.number = number;
        this.girlfriend = 'xiaoli'
    }

    study() {
        alert(`${this.name} study`)
    }

    getWeight() {
        // This is the parent class definition, protected on their own, open subclass 
        Alert ({$ ` the this .Weight}` kg)
    }
}

// 实例
let xaioming = new Student('xiaoming', 10, 'A1');
xaioming.getWeight();
// Girlfriend is private and can not be called 
// xiaoming.girlfriend;

 

Reduce the coupling, should not exposed exposed
Facilitate data, multi-rights management interfaces
es6 does not currently support is generally believed that private property is more than the beginning _

Guess you like

Origin www.cnblogs.com/wzndkj/p/11706668.html