美文网首页
04. typescript类

04. typescript类

作者: DDLH | 来源:发表于2019-11-15 16:40 被阅读0次

    学习typescript前,大家可以先回忆一下es5中的相关知识点

    例如:

    • 对象冒充继承

    • 原型链继承

    • 原型链+对象冒充继承

    ok, 接下来我们一起了解下 typescript类,学完之后希望能思考一下其中的差别与共性

    typescript类

    // 1. 类的定义

    // function Person(name,age){
        
    //     this.name = name;
    
    //     this.age = age;
    
    //     this.run = function(){
    
    //         console.log(this.name + '在运动');
        
    //     }
    // }
    
    // var p = new Person('张三', 30);
    
    // p.run();
    
    
    // ts中定义类
    class Person{
        name:string; // 属性
    
        constructor(n:string){  // 构造函数 实例化类的时候触发的方法
            this.name = n;
        }
    
        run():void{
            
            console.log(this.name + '在运动');
        }
    
        getName():string{
    
            return this.name;
        }
    
        setName(name:string):void{
    
            this.name = name;
        }
    }
    
    console.warn('ts中定义类');
    
    var p = new Person('张三');
    
    p.run();
    
    console.log(p.getName());
    
    p.setName('李四');
    
    console.log(p.getName());
    

    // 2. ts中实现继承 extends super

    class Person1{
    
        name:string;
    
        constructor(name: string){
            this.name = name;
        }
    
        run():string{
    
            return `${this.name}在运动`;
        }
    }
    
    // var p = new Person1('旺旺');
    
    // console.log(p.run());
    
    class Web extends Person1{
    
        constructor(name:string){
    
            super(name);    //初始化父类的构造函数
        }
    
        work(){     // 子类增加方法
            console.log(`${this.name}在工作`);
        }
    
        // run():string{  // 父类的方法和子类一致
    
        //     return `${this.name}在运动-子类`;
        // }
    }
    
    
    console.warn('ts中实现继承');
    
    var w = new Web('王五');
    
    console.log(w.run());
    
    //  ts中继承探讨
    
    w.work();
    
    // console.log(w.run());
    

    // 3. ts中类的修饰符 三种

    // public  公有: 在类里面、子类、类外面都可以访问
    
    // protected    保护类型: 在类里面、子类可以访问,在类外面没法访问
    
    // pravite      私有:在类里面可以访问, 在子类、类外面没法访问
    
    
    class Person2{
    
        name:string;
    
        // public name:string;
    
        // protected name:string;
    
        // private name:string;
    
        constructor(name: string){
            this.name = name;
        }
    
        run():string{
    
            return `${this.name}在运动`;
        }
    }
    
    class Web2 extends Person2{
    
        constructor(name:string){
    
            super(name);    //初始化父类的构造函数
        }
    
        work(){     // 子类增加方法
            console.log(`${this.name}在工作`);
        }
    }
    
    console.warn('ts中类的修饰符');
    
    var p2 = new Person2('王五');
    
    console.log(p2.name);
    
    console.log(p2.run());
    
    var w2 = new Web2('web2');
    
    w2.work();
    

    // 4. 静态属性 静态方法

    class Person4{
        
        public name:string;
        public age:number = 20;
    
        static sex:string = '男';   // 静态属性 不能被继承
    
        constructor(name:string){
            
            this.name = name;
        }
    
        run() {     // 实例方法
    
            console.log(`${this.name}在运动`);
        }
    
        work(){
    
            console.log(`${this.name}在工作`);
        }
    
        static print(){     // 静态方法
    
            // console.log('print方法' + this.age);    // 错误写法  没法直接调用类里面的属性
    
            console.log('print方法' + this.sex);    
        }
    }
    
    console.warn('静态属性 静态方法');
    
    var p4 = new Person4('张三');
    
    // p4.print();     // 错误写法 只能通过 Person4 来进行调用
    
    Person4.print();
    
    console.log(Person4.sex);
    

    // 5. 多态(父类定义一个方法不去实现,让继承他的子类去实现,每一个子类有不用的表现) 属于继承

    class Animal{
    
        name:string;
    
        constructor(name:string){
            this.name = name;
        }
    
        eat(){  // 具体吃什么? 让子类去实现
            
            console.log('吃。。。');
        }
    }
    
    class Dog extends Animal{
    
        constructor(name:string){
            super(name);
        }
    
        eat(){
            return this.name + '吃粮食';
        }
    }
    
    class Cat extends Animal{
    
        constructor(name:string){
            super(name);
        }
    
        eat(){
            return this.name + '吃鱼';
        }
    }
    
    console.warn('ts 多态')
    
    var dog = new Dog('小狗');
    console.log(dog.eat());
    
    var cat = new Cat('小猫');
    console.log(cat.eat());
    

    // 6. 抽象方法(提供其他类继承的基类, 不能直接被实例化)

    // abstract 关键字定义抽象类和抽象方法,抽象类中的方法不包含具体实现,必须在派生类中实现
    
    // 定义标准 Animal2 这个类要求他的子类必须要包含定义的抽象方法(eat方法)
    
    // 抽象方法必须定义在抽象类中
    
    abstract class Animal2{
    
        name:string;
    
        constructor(name:string){
    
            this.name = name;
        }
    
        abstract eat():any;
    }
    
    console.warn('ts 抽象方法')
    
    // var a = new Animal2();       // 错误的写法  无法创建抽象类的实例
    
    class Cat2 extends Animal2{
    
        constructor(name:string){
            super(name);
        }
    
        eat(){
            return this.name + '吃鱼';
        }
    }
    
    var cat2 = new Cat2('小花猫');
    console.log(cat2.eat());
    

    相关文章

      网友评论

          本文标题:04. typescript类

          本文链接:https://www.haomeiwen.com/subject/ioesictx.html