• TS类


    //
    class Person {
        name: string
        constructor(name: string) {
            this.name = name
        }
        run(): void {
            console.log(this.name)
        }
        getName(): string {
            console.log(this.name)
            return this.name
        }
        setName(name: string): void {
            this.name = name
        }
    }
    let p = new Person('张三1')
    p.run()
    p.setName('李四')
    p.getName()
    
    // 继承
    class Person1 {
        public name: string
        protected age: number = 18
        private sex: string = 'boy'
        constructor(name: string) {
            this.name = name
        }
        run(): void {
            console.log(this.name + '父类')
            console.log(this.age + '父类')
            console.log(this.sex + '父类')
        }
    }
    let p1 = new Person1('王五')
    console.log(p1.name)
    // console.log(p1.age) 报错 保护类型
    // console.log(p1.sex) 报错 私有类型
    p1.run()
    class Web extends Person1 {
        constructor(name: string) {
            super(name)
        }
        // 拓展方法
        work() {
            console.log(this.name + 'work....')
            console.log(this.age + 'work....')
            // console.log(this.sex + 'work....') 报错 私有类型
        }
        // 覆盖父类同名方法
        run(): void {
            console.log(this.name + '子类')
        }
    }
    let w = new Web('x6')
    w.run()
    w.work()
    
    // 修饰符
    // public    公有类型,子类,类外都可访问
    // protected 保护类型,子类可以访问,类外无法访问
    // private   私有类型,子类,类外都无法访问
    
    // 静态方法
    class Person2 {
        public name: string
        static age = 18
        constructor(name: string) {
            this.name = name
        }
        // 实例方法
        run(): void {
            console.log(this.name + 'run')
        }
        work(): void {
            console.log(this.name + 'work')
        }
        // 静态方法
        // 无法直接调用类里面的属性
        // 除非是静态属性
        static print() {
            console.log('print' + this.age)
        }
    }
    let p2 = new Person2('张三')
    p2.run()
    Person2.print()
    
    // 多态(属于继承)
    // 父类定义一个方法不去实现,让继承的子类去实现,每个子类有不同的表现
    class Animal {
        name: string
        constructor(name: string) {
            this.name = name
        }
        eat() {
            console.log('eateateat')
        }
    }
    class Dog extends Animal {
        constructor(name: string) {
            super(name)
        }
        eat() {
            console.log(this.name + 'eateateat1')
        }
    }
    class Cat extends Animal {
        constructor(name: string) {
            super(name)
        }
        eat() {
            console.log(this.name + 'eateateat2')
        }
    }
    let dog = new Dog('DogDog')
    dog.eat()
    let cat = new Cat('CatCatCat')
    cat.eat()
    
    // 抽象类,是提供其他类的基类,不能直接实例化
    abstract class Animal1 {
        public name: string
        constructor(name: string) {
            this.name = name
        }
        abstract eat(): any
    }
    // var a =new Animal1() 错误的写法
    class Dog1 extends Animal1 {
        // 抽象类的子类必须实现抽象类里的抽象方法
        constructor(name: string) {
            super(name)
        }
        eat() {
            console.log(this.name + 'eateateat3')
        }
    }
    let dog1 = new Dog1('Dog1Dog1')
    dog1.eat()
    
    let aaa = { a: 1, b: 2, c: 3 }
    let bbb = Object.assign({}, aaa, { a: 2 })
    let ccc = { ...aaa, a: 3 }
    console.log(aaa)
    console.log(bbb)
    console.log(ccc)
  • 相关阅读:
    Qt:绘制时,用线性渐变填充一个圆角矩形范围
    记录一个mysql数据迁移的坑
    idea中添加jar,maven有时候无法引入到jar
    img引入base64格式图片
    URL特殊符号转码_encodeURI/decodeURI
    [js] eval函数
    Modernizr
    判断对象上是否存在指定key
    Linux下格式化大于2T硬盘的方法
    win10 别人无法ping通本机设置
  • 原文地址:https://www.cnblogs.com/ssszjh/p/16063091.html
Copyright © 2020-2023  润新知