typescript2-类的类型

类的类型

类实现接口

  1. 与Java 里接口的基本作用一样,TypeScript 也能够用它来明确的强制一个类去符合某种契约
  2. 类的类型通过接口来实现
  3. 一个类可以被多个接口约束
interface IEat{
    
    
    eat()
}
interface ISwim{
    
    
    swim()
}
class Person implements IEat,ISwim{
    
    
    eat(){
    
    
        console.log("吃饭")
    }
    swim(){
    
    
        console.log("游泳")
    }
}
const p1=new Person()
p1.eat()
p1.swim()
/*
* 输出
* 吃饭
* 游泳
*/

继承与多态

  1. 子类继承父类特征子类 extends 父类
  2. 当需要父类参数传递时,用子类也可以,这就是多态
/*
* 继承:子类继承父类
* 多态:当需要父类参数时,传入子类的也可以
* */
class Animal{
    
    
    name: string
    constructor(name:string) {
    
    
        this.name=name
    }
    run(distance=0){
    
    
        console.log(this.name+"跑了" + distance + "米")
    }
}
class Dog extends Animal{
    
    
    constructor(name:string="dog") {
    
    
        super(name);
    }
    run(distance=5){
    
    
        console.log(this.name+"跑了" + distance + "米")
    }
}
class Cat extends Animal{
    
    
    constructor(name:string="cat") {
    
    
        super(name);
    }
    run(distance=10){
    
    
        console.log(this.name+"跑了" + distance + "米")
    }

}
const animal1=new Animal("aniaml")
animal1.run()
const dog1=new Dog()
dog1.run()
const cat1=new Cat()
cat1.run()
const dog2:Animal=new Dog() // 当使用了父元素类型Animal也没报错,这就是因为多态
dog2.run()
/*
*  输出
* aniaml跑了0米
* dog跑了5米
* cat跑了10米
* dog跑了5米
*/



类的修饰符

  1. 修饰构造函数,属性,方法是否可以被外部访问
修饰符 作用范围
public 公共的,默认属性,都可以访问
protected 外部无法访问,子类可以访问
private 内部使用,外部无法访问,子类也无法访问
readonly 只读属性,添加后外部就不能修改了,只能在构造函数内修改
class Person{
    
    
    readonly name: string
    constructor(name: string) {
    
    
        this.name=name
    }
    showName(){
    
    
    	//在类中的普通方法中也不能对只读属性的name重新赋值
        // this.name="rose"
        console.log(this.name)
    }
}
//实例化对象
const p1: Person=new Person("jack")
p1.showName()
//输出jack
// p1.name="rose"
/*
* 这里应为实在类的外部,name为只读的,所以不能再对name赋值
*/
  1. 参数属性
  • 构造函数中的参数如果使用readonly修饰后,那么参数就叫参数属性
  • 构造函数中的参数如果使用readonly修饰后,那么类中就有了一个参数成员
  • 构造函数中的参数如果使用readonly修饰后,外部也是无法访问
  • 构造函数中的参数如果使用其他修饰后,那么类中就有了一个相应作用范围的参数成员
class Person{
    
    
	//虽然类中并没name属性,但这里使用了readonly属性修饰,在类中隐式添加了一个name属性
    constructor(readonly name: string='jack') {
    
    
        this.name=name
    }
    showName(){
    
    
        console.log(this.name)
    }
}
//实例化对象
const p1: Person=new Person()
p1.showName()

存取器

TypeScript 支持通过 getters/setters 来截取对对象成员的访问,它能帮助你有效的控制对对象成员的访问

 class Person{
    
    
    private firstName: string
    private lastName: string
    constructor(firstName,lastName) {
    
    
        this.firstName = firstName
        this.lastName = lastName
    }
    get fullName(){
    
    
        return this.firstName+ "-" + this.lastName
    }
    set fullName(value){
    
    
        let val = value.split("-")
        this.firstName = val[0]
        this.lastName = val[1]
    }
}
const p1: Person=new Person("东方","不败")
console.log(p1.fullName)
p1.fullName = "西门-吹雪"
console.log(p1.fullName)
/*
* 输出
* 东方-不败
* 西门- 吹雪
* 注意这里不能直接赋值给名字,因为名字属性被private修饰了,只能在类的内部访问
*/

static

  1. 静态成员:在类中通过static修饰的属性或方法,那么就是静态的属性或方法
  2. 静态成员都是通过类.方法来调用
  3. 也就是static修饰后的方法或属性不能通过实例化方式调用
class Person{
    
    
	// 注意这里不能static name因为类中默认有个name属性
    static name1: string="jack"
    static eat(){
    
    
        console.log("吃饭")
    }
}

console.log(Person.name1)
Person.name1="rose"
console.log(Person.name1)
Person.eat()
/*
* 输出
*jack
*rose
*吃饭
*也就是既可以通过类.属性调用,也能赋值
*/

抽象类(abstract)

  1. 抽象类:包含抽象方法(抽象方法没有任何具体内容的实现),也可以包含实例方法,但抽象类不能被实例化
  2. 抽象类主要是为了让子类实例化并实现内部的抽象方法
  3. 理解为抽象类占个位置,子类有办法重写父类的方法
abstract class Animal {
    
    
    abstract name: string
    abstract eat() // 抽象方法时里面不能有代码
    say(){
    
    
        console.log("say")
    }
}
class Dog extends Animal{
    
    
    name = "大黄"
    eat(){
    
    
        console.log("吃骨头")
    }
}
// const animal: Animal = new Animal() 这里不能实例化Animal类
const dog: Dog=new Dog()
console.log(dog.name)
dog.say()
dog.eat()
/*
* 输出
* 大黄
* say
* 吃骨头
*/

猜你喜欢

转载自blog.csdn.net/weixin_64925940/article/details/124775213