Typescript

原文:TypeScript基本知识点整理

一、访问修饰符

  • public - 公共修饰符,表示属性或者方法是公有的,在 类内部、子类内部、类的实例 都能被访问。
    class People {
        public name : string
        constructor(name:string) { //构造函数必须写
            this.name = name
        }
        public say() :void {
            console.log('你好')
        }
    }
    
  • private - 私有修饰符,表示在当前类中可以访问,子类,外部类都不可以访问。
    class People {
        private name : string
        constructor(name:string) { //构造函数必须写
            this.name = name
        }
        private say() :void {
            console.log('你好')
        }
    }
    
  • protected - 保护类型,表示在当前类和子类中可以访问,外部类不可以访问。
    class People {
        protected name : string
        constructor(name:string) { //构造函数必须写
            this.name = name
        }
        protected say() :void {
            console.log('你好')
        }
    }
    
  • readonly - 只读修饰符,表示某个属性是制度的,不能被修改。

注意:TypeScript 只做编译时检查,当你试图在类外部访问被 private 或者 protected 修饰的属性或方法时,TS 会报错,但是它并不能阻止你访问这些属性或方法。

二、声明类

class People {
    name : string //默认为public
    age : number
    constructor(name:string, age:number) { //构造函数必须写
        this.name = name
        this.age = age
    }
    say() :void {
        console.log('你好')
    }
}

const HH : People = new People('含含', 21)
console.log(HH.name)
console.log(HH.age)
HH.say()

三、类的继承

class People {
    name : string //默认为public
    age : number
    constructor(name:string, age:number) { //构造函数必须写
        this.name = name
        this.age = age
    }
    say() :void {
        console.log('你好')
    }
}

class Student extends People {
    cm : number
    constructor (name:string, age:number, cm:number) {
        super(name, age) //super 继承父类的构造函数,并向构造函数传参,super必须写在第一行
        this.cm = cm
    }
    work() : void {
        console.log('学习')
    }
}

const  stu1 : Student = new Student('liu', 22, 175)
console.log(stu1.name)
console.log(stu1.age)
console.log(stu1.cm)
stu1.say()
stu1.work()

四、静态方法和静态属性

  1. 静态方法和静态属性必须使用类名调用;
  2. 静态属性和静态方法在实例化之前就已经存在;
class People {
    static name1 : string = '静态属性';
    static say () :void {
        console.log('静态方法')
    }
}
console.log(People.name1)
People.say()

注意:静态方法调用不了实例化方法和实例化属性,因为静态域加载是在解析阶段,而实例化是在初始化阶段,(java原理),所以静态方法里面不能调用本类的方法和属性,可以调用静态属性和静态方法。

五、多态

  父类定义一个方法可以不去实现,让继承它的子类去实现,每个子类的该方法有不同的表现;

class Animal {
    name : string
    constructor (name:string) {
        this.name = name
    }
    eat () : void {
        //让它的子类去实现不同的eat方法
        console.log('animal eat')
    }
}

class Laohu extends Animal {
    constructor (name:string) {
        super(name)
    }
    eat () : void {
        console.log(`${this.name}吃肉!`)
    }
}

class Laoshu extends Animal {
    constructor (name:string) {
        super(name)
    }
    eat () : void {
        console.log(`${this.name}吃粮食!`)
    }
}
const laohu : Laohu = new Laohu('老虎')
laohu.eat()
const  laoshu : Laoshu = new Laoshu('老鼠')
laoshu.eat()

六、接口和类

  类可以实现(implement)接口。通过接口,你可以强制地指明类遵守某个契约。你可以在接口中声明一个方法,然后要求类去具体实现它。

  接口不可以被实例化,实现接口必须重写接口中的抽象方法。

interface Play {
    plays (difang:string) : void;
}

class Playy implements Play {
    plays(difang: string): void {
        console.log(`我们要去${difang}玩!!!`)
    }
}

const pl : Playy = new Playy();
pl.plays('北京')

七、抽象类:

抽象类是提供其他类继承的基类,不能直接被实例化,子类继承可以被实例化;

1.用abstract关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类(抽象类的子类)中实现;

2.abstract修饰的方法(抽象方法)只能放在抽象类里面;

3.抽象类和抽象方法用来定义标准(比如定义标准为:抽象类Animal有抽象方法eat,要求它的子类必须包含eat方法)

abstract class People {
    name : string
    constructor (name:string) {
        this.name = name
    }
    abstract eat (food:string) :void;//抽象方法不包括具体实现,并且必须再派生类中实现
}

class Stud1 extends People {
    //抽象类的子类必须实现抽象类中的抽象方法
    constructor (name:string) {
        super(name)
    }
    eat(food: string): void {
        console.log(`我爱吃${food}`)
    }

}

const stu11 : Stud1 = new Stud1('liu')
stu11.eat('面条')

抽象类与接口的区别:

1.类可以实现(implement)多个接口,但只能扩展(extends)自一个抽象类。

2.抽象类中可以包含具体实现,接口不能。

3.抽象类在运行时是可见的,可以通过 instanceof判断。接口则只在编译时起作用。

4.接口只能描述类的公共(public)部分,不会检查私有成员,而抽象类没有这样的限制。



原文地址:https://www.cnblogs.com/cc-freiheit/p/10985662.html