zoukankan      html  css  js  c++  java
  • 006--TypeScript之类

    先看ES6中一个简单的类

    class Greeter {
      greeting: string
      constructor(msg: string){
        this.greeting = msg
      }
      greet(){
        return 'hello ' + this.greeting
      }
    }
    let greeter = new Greeter('world!')
    console.log(greeter.greet())//hello world!

    简单的继承

    //简单的继承
    class Animal {
      move(distince: number=0){
        console.log(`The animal move ${distince} m`)
      }
    }
    class Dog extends Animal {
      bark() {
        console.log('woof! woof!')
      }
    }
    
    const dog = new Dog()
    dog.bark()//woof! woof!
    dog.move(12)//The animal move 12 m

    稍微复杂一点的继承

    class Animal {
      name: string 
    
      constructor(name) {
        this.name = name 
      }
    
      move(distance: number){
        console.log(`${this.name} is move ${distance} m`)
      }
    }
    
    class Snake extends Animal {
      constructor(name: string){
        super(name)
      }
      move(distance: number=10){
        console.log('Slithering...')
        super.move(distance)
      }
    }
    
    class Horse extends Animal {
      constructor(name: string){
        super(name)
      }
      move(distance: number = 35){
        console.log('Gallophing...')
        super.move(distance)
      }
    }
    
    let sam = new Snake('sam')
    let tommy: Animal = new Horse('tommy')
    sam.move()
    tommy.move(38)
    // Slithering...
    // sam is move 10 m
    // Gallophing...
    // tommy is move 38 m

    2019-05-24  15:57:48

    工欲善其事,必先利其器
  • 相关阅读:
    FIS3常用配置
    PC端模拟移动端访问 字体大小限制
    table布局 防止table变形 td固定宽度
    fis3 scss 版本报错
    移动端布局方案 网易
    提示浏览器版本低
    JS Math.round()方法原理
    margin 负边距应用
    box-shadow IE8兼容处理
    border-radius IE8兼容处理
  • 原文地址:https://www.cnblogs.com/ccbest/p/10918629.html
Copyright © 2011-2022 走看看