Typescript 中的类

类 Class

类(Class):定义了一切事物的抽象特点

对象(Object):类的实例

面向对象(OOP)三大特性:封装继承多态

JS类的回顾

   class Animal {
     //创建一个基本类
    constructor(name){
      //构造函数:实例化的时候执行的逻辑
      this.name = name
    }
     run(){
       return `${this.name} is running`
      }
     }	
    const snake = new Animal('lily')
    console.log(snake.run())

类的继承

    class Animal {
      //创建一个基本类
      constructor(name){
        //构造函数:实例化的时候执行的逻辑
        this.name = name
      }
      run(){
        return `${this.name} is running`
      }
    }	
    const snake = new Animal('lily')
    console.log(snake.run()) 

    class Dog extends Animal{
      //使用 extends 继承父类的属性和方法
      //除了继承run之外 我们创建了一个bark的方法
      bark(){
        return `${this.name} is barking`
      }
    }
  	 const xiaobao =new Dog('xiaobao')
     console.log(xiaobao.run())
		 console.log(xiaobao.bark())

类的多态

    class Animal {
        //创建一个基本类
        constructor(name) {
            //构造函数:实例化的时候执行的逻辑
            this.name = name
        }
        run() {
            return `${this.name} is running`
        }
    }
    const snake = new Animal('lily')
    console.log(snake.run())

    class Cat extends Animal {
        static categories = ['mammal']
				//static 静态属性创建加static
				//静态属性不需要实例化
        constructor(name) {
     		//重写构造函数需要使用super方法
            super(name)
            console.log(this.name)
        }
        run() {
          //重写run,调用父类方法要使用super
            return 'Meow,' + super.run()
        }
    }
    const maomao = new Cat('maomao')
    console.log(maomao.run())
    console.log(Cat.categories)
     

Typescript中的类

可以使用三种访问修饰符

分别是:

  • Public 修饰的属性或方法是共有的
    • 默认使用的是Public
  • Privatel: 修饰的属性或方法是私有的
    • 不能在申明它的类外部调用
  • Protected: 修饰的属性或方法是受保护的
    • Privatel类似,但是在子类中是可以访问的

有了这三种修饰符,我们可以给类上的方法和属性提供权限管理

Privatel

修饰的属性或方法是私有的

  class Animal {
      //创建一个基本类
    	//name:string
      constructor(name){
          //构造函数:实例化的时候执行的逻辑
          this.name = name
      }
      private  run(){
          return `${this.name} is running`
      }
  }
  const snake = new Animal('lily')
  console.log(snake.run())

在外部访问会直接提示报错:Property 'run is private and only accessible within class 'Animal'

不允许访问或者修改!
在外部访问会直接提示报错

Property 规定了这个变量只能在我的类里面活动,它的子类都不能访问

Protected

修饰的属性或方法是受保护的

让子类访问父类的属性:

    class Animal {
        //创建一个基本类
        constructor(name){
            //构造函数:实例化的时候执行的逻辑
            this.name = name
        }
       protected run(){
            return `${this.name} is running`
        }
    }
    const snake = new Animal('lily')
    console.log(snake.run())

    class Dog extends Animal{
        //使用 extends 继承父类的属性和方法
        //除了继承run之外 我们创建了一个bark的方法
        bark(){
            return `${this.name} is barking`
        }
    }
    const xiaobao =new Dog('xiaobao')
    console.log(xiaobao.run())
    console.log(xiaobao.bark())

在实例上依然会报错 :Property 'run' is protected and only accessible within class 'Animal' and its subclasses

属性“run”受保护,只能在类“Animal”及其子类中访问

但是在子类中依然可以调用run
在这里插入图片描述

总结:有点类似于遗产 只能自己以及自己的子类可以访问,外部人员一概拒绝

readonly

只读属性,控制访问程度

    class Animal {
        //创建一个基本类
        constructor(name){
            //构造函数:实例化的时候执行的逻辑
            this.name = name
        }
       readonly run(){
            return `${this.name} is running`
        }
    }
    const snake = new Animal('lily')
    snake.name = '123' //会直接报错 只读属性不允许更改
    console.log(snake.run())

当改变snake的名称时,会直接报错Cannot assign to 'name' because it is a read-only property

不能修改'name'属性,因为这个属性是一个只读属性
在这里插入图片描述

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值