• TypeScript----类


    一、类的属性

    public: 公有,

    private: 私有,不能在声明它的类的外部访问,只能在类内部访问

    protect: 保护,不能在声明它的类的外部访问,但继承者除外

    readonly 只读属性,必须在声明时或构造函数里被初始化

    static静态属性,无需实例化就可以访问静态成员

    super()方法,它会执行基类的构造方法

    class Grid {
        static origin = {x: 0, y: 0};
        calculateDistanceFromOrigin(point: {x: number; y: number;}) {
            let xDist = (point.x - Grid.origin.x);
            let yDist = (point.y - Grid.origin.y);
            return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
        }
        constructor (public scale: number) { }
    }
    
    let grid1 = new Grid(1.0);  // 1x scale
    let grid2 = new Grid(5.0);  // 5x scale
    
    console.log(grid1.calculateDistanceFromOrigin({x: 10, y: 10}));
    console.log(grid2.calculateDistanceFromOrigin({x: 10, y: 10}));

    二、类的存取,set() 和 get()

    let passcode = "secret passcode";
    
    class Employee {
        private _fullName: string;
    
        get fullName(): string {
            return this._fullName;
        }
    
        set fullName(newName: string) {
            if (passcode && passcode == "secret passcode") {
                this._fullName = newName;
            }
            else {
                console.log("Error: Unauthorized update of employee!");
            }
        }
    }
    
    let employee = new Employee();
    employee.fullName = "Bob Smith";
    if (employee.fullName) {
        alert(employee.fullName);
    }

    三、类的继承  extends

    class Animal {
        name:string;
        constructor(theName: string) { this.name = theName; }
        move(distanceInMeters: number = 0) {
            console.log(`${this.name} moved ${distanceInMeters}m.`);
        }
    }
    
    class Snake extends Animal {
        constructor(name: string) { super(name); }
        move(distanceInMeters = 5) {
            console.log("Slithering...");
            super.move(distanceInMeters);
        }
    }
    
    class Horse extends Animal {
        constructor(name: string) { super(name); }
        move(distanceInMeters = 45) {
            console.log("Galloping...");
            super.move(distanceInMeters);
        }
    }
    
    let sam = new Snake("Sammy the Python");
    let tom: Animal = new Horse("Tommy the Palomino");
    
    sam.move();
    tom.move(34);

    四、抽象类:abstract,派生类的基类使用。它们一般不会直接被实例化。

    abstract class Department{
        constructor(public name:string){}
        printName():void{
            console.log('Department name:'+this.name);
        }
        abstract printMeeting():void;//抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。
    }
    class AcountingDepartment extends  Department{
        constructor(){
            super('Accounting and Auditing');
        }
        printMeeting():void{
            console.log('The Accounting Department meets each Monday at 10am.');
        }
        generateReports():void{
            console.log('Generating accounting reports...');
        }
    }
    let department:Department;
    department=new Department()  //error
    let department=new AcountingDepartment();
    department.printName();
    department.printMeeting();
    //department.generateReports();
  • 相关阅读:
    javascript高级知识分析——灵活的参数
    javascript高级知识分析——实例化
    javascript高级知识分析——上下文
    javascript高级知识分析——作为对象的函数
    javascript高级知识分析——函数访问
    javascript高级知识分析——定义函数
    new到底做了什么?
    JavaScript中的计时器原理
    解析Function.prototype.bind
    将类数组对象(array-like object)转化为数组对象(Array object)
  • 原文地址:https://www.cnblogs.com/xuepei/p/7520960.html
Copyright © 2020-2023  润新知