• TS学习之类


    定义类

    class Person {
        name: string;   //属性
        constructor(_name: string) {
            this.name = _name;
        }   //构造函数
        sayHello(): string {
            return "Hi,everyone"
        }   //方法
    }
    
    let firstOne = new Person("Fred")   //实例化类

    继承:继承使用关键字extends,调用父类使用super,子类继承父类的属性和方法,并且子类可以改写父类的属性和方法

    class Animal {
        name: string;
        constructor(_name: string) {
            this.name = _name;
        }
        skinColour(color: string = "black"): void {
            console.log(`${this.name} skin colour is ${color}`)
        }
    }
    
    class Horse extends Animal {
        constructor(name: string) { super(name) }
        skinColour(color: string = "brown"): void {
            console.log(`I'am ${this.name}`);
            super.skinColour("brown");
        }
    }
    
    let horse = new Horse("horse");
    horse.skinColour()
    // I'am horse
    // horse skin colour is brown

    public、private、protected、readonly

    • public(不声明默认都为public,也可以显示的设置为public)
    class Person {
        public name: string;   //属性
        public constructor(_name: string) {
            this.name = _name;
        }   //构造函数
        public sayHello(): string {
            return "Hi,everyone"
        }   //方法
    }
    
    let firstOne = new Person("Fred")   //实例化类
    • private(private的成员不能被外部访问;比较带有privateprotected成员的类型时,两个类型兼容的条件是private或protected的成员必须相同切来至同一个声明(同一个类))
    class Person {
        private name: string;   
        public constructor(_name: string) {
            this.name = _name;
        }   
    }
    
    class Employee {
        private name: string;   
        public constructor(_name: string) {
            this.name = _name;
        }   
    }
    
    let firstOne = new Person("Fred")
    console.log(firstOne.name)  //error: Property 'name' is private;
    let lastOne = new Employee("Fred")
    firstOne = lastOne  // error: Type 'Employee' is not assignable to type 'Person'.Types have separate declarations of a private property 'name'.
    • protected(protected和private相似,但protected成员可以在派生类中访问(能被继承,但不能在实例中访问,若构造函数是protected,则不能被实例化,只能被继承))
    class Person {
        protected name: string;   
        protected constructor(_name: string) {
            this.name = _name;
        }   
    }
    
    class Employee extends Person {
        private department: string;   
        public constructor(name: string,department:string) {
            super(name);
            this.department = department;
        }   
    }
    
    let Bob = new Person;   //error: Constructor of class 'Person' is protected
    let fred = new Employee("fred","test");
    console.log(fred.name)  //error: Property 'name' is protected
    • readonly(设置属性为只读,必须在声明时或构造函数里初始化)
    class Person {
        readonly name: string;   
        constructor(_name: string) {
            this.name = _name;
        }   
    }
    
    let fred = new Person("fred");
    fred.name = "Bob"   //error: Cannot assign to 'name' because it is a constant or a read-only property.

    参数属性(参数属性通过给构造函数参数添加一个访问限定符来声明(public,private,protected),把声明和赋值合并至一处)

    class Person {
        constructor(private name: string) { }
        sayHello(): void {
            console.log(`my name is ${this.name}`)
        }
    }
    
    let fred = new Person("fred");
    fred.sayHello() //my name is fred

    存取器(get、set   只带有 get不带有set的存取器自动被推断为readonly

    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) {
        console.log(employee.fullName);
    }

    静态属性(static,不能被实例访问,在类里面访问时,需要加上类名)

    class Person {
        static height:number = 180;
        constructor(private name: string) { }
        sayHello(): void {
            console.log(`my name is ${this.name}, I height is ${Person.height}`)
        }
    }
    
    let fred = new Person("fred");
    fred.sayHello() //my name is fred, I height is 180

    抽象类(abstract,抽象类做为其它派生类的基类使用。 它们一般不会直接被实例化。抽象类中的抽象方法不包含具体实现并且必须在派生类中实现)

    abstract class Person {
        constructor(public name: string) { }
        abstract sayHello():void;
    }
    
    class Empoloy extends Person{
        constructor(){
            super("Fred")
        }
        sayHello(){
            console.log(`my name is ${this.name}`)
        }
    }
    
    let firstOne = new Empoloy();
    firstOne.sayHello();    //my name is Fred
  • 相关阅读:
    WebSocket面试题
    面试题、四
    面试题、三
    面试题、二
    面试题、一
    vue 使用localstorage实现面包屑
    flutter,SliverPersistentHeader实现Tab顶部吸附固定效果
    对flutter中,ExpsionPanel的简单改造
    node.js学习笔记
    PHP和Redis实现在高并发下的抢购及秒杀功能示例详解
  • 原文地址:https://www.cnblogs.com/sghy/p/7742111.html
Copyright © 2020-2023  润新知