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

     

转载于:https://www.cnblogs.com/xiaoyaoxingchen/p/10695701.html

你可能感兴趣的:(ts中类的继承)