(精华2020年6月3日更新) TypeScript中类的使用(封装,继承,多态)

基本使用

class Cat {
    name:string;
    color:string;
    constructor(name1:string,color:string){
        this.name = name1;
        this.color= color
    }
    eat(){
        console.log('eat');
    }
}

var c1 = new Cat('哈哈','黄色');

继承

class Animal {
    name:string;
    color:string;
    constructor(name:string,color:string){
        this.name = name1;
        this.color= color
    }
    eat(){
        console.log('吃肉');
    }
}

class Dog extends Animal {
    static likes='喜欢运动';
    weight:string;
    constructor(weight:string,name:string,color:string) {
        super(name,color); //关键字, 调用父类构造函数和方法
        this.weight = weight;
    }
    run(){
        console.log('喜欢跑');
    }
}

修饰属性

class Animal {
    public name:string;  //公共的, 在当前类里面、 子类  、类外面都可以访问
    private color:string;  // 私有的  在当前类里面可以访问,子类、类外部都没法访问
    protected gender:string; //保护类型  在当前类里面、子类里面可以访问 ,在类外部没法访问
    constructor(name1:string,color:string,gender:string){
        this.name = name1;
        this.color= color;
        this.gender = gender;
    }
    public eat(){
        console.log('吃肉------');
        console.log(this.name)  // 公共的 在当前类里面
        console.log(this.color)  // 私有的 在当前类里面
        console.log(this.gender)  // 保护类型 在当前类里面
        console.log('---------');
    }
}

class Dog extends Animal {
    static likes='喜欢运动';
    weight:string;
    constructor(weight:string,uname:string,color:string,gender:string) {
        super(uname,color,gender); //关键字, 调用父类构造函数和方法
        this.weight = weight;
    }
    run(){
        console.log('喜欢跑');
        console.log(this.name)  //public 子类 可以访问
        // console.log(this.color)  //private 子类 不可以访问
        console.log(this.gender)  //protected 子类 可以访问
    }
}

多态

class Animal {
    name:string;
    color:string;
    constructor(name1:string,color:string){
        this.name = name1;
        this.color= color
    }
    eat(){
       //具体吃什么 ,不知道,具体吃什么?继承它的子类去实现 ,每一个子类的表现不一样
        console.log('吃的方法');
    }
}

class Dog extends Animal {
    static likes='喜欢运动';
    weight:string;
    constructor(weight:string,uname:string,color:string) {
        super(uname,color); //关键字, 调用父类构造函数和方法
        this.weight = weight;
    }
    eat(){
        return this.name +'吃粮食'
    }
}

class Cat extends Animal {
    static likes='喜欢运动';
    weight:string;
    constructor(weight:string,uname:string,color:string) {
        super(uname,color); //关键字, 调用父类构造函数和方法
        this.weight = weight;
    }
    eat(){
        return this.name +'吃老鼠'
    }
}

抽象类

 namespace AbstractFun {
    abstract class Animal{
        public name:string;
        constructor(name:string){
            this.name=name;
        }
        abstract eat():any;  //抽象方法不包含具体实现并且必须在派生类中实现。
        run(){
            console.log('其他方法可以不实现')
        }
    }
    
    // var a = new Animal('猫类') //错误  抽象类不能实例化,
    
    class Dog extends Animal {
        constructor(pname:any){
            super(pname);
        }
       //抽象类的子类必须实现抽象类里面的抽象方法
        eat(){
            console.log(this.name+'吃粮食')
        }
    }
    
    var d = new Dog('小花');
    d.eat();
}

命名空间的使用

export namespace A {
    export class Animal {
        name:string;
        color:string;
        constructor(name1:string,color:string){
            this.name = name1;
            this.color= color
        }
        eat(){
            console.log('吃肉');
        }
    }
    
    export class Dog extends Animal {
        static likes='喜欢运动';
        weight:string;
        constructor(uname:string,color:string,weight:string) {
            super(uname,color); //关键字, 调用父类构造函数和方法
            this.weight = weight;
        }
        run(){
            console.log('喜欢跑');
        }
    }
}


export namespace B {
    export class Animal {
        name:string;
        color:string;
        constructor(name1:string,color:string){
            this.name = name1;
            this.color= color
        }
        eat(){
            console.log('吃肉');
        }
    }
    
    export class Cat extends Animal {
        weight:string;
        constructor(uname:string,color:string,weight:string) {
            super(uname,color); //关键字, 调用父类构造函数和方法
            this.weight = weight;
        }
        run(){
            console.log('喜欢吃老鼠');
        }
    }
}

var d1 = new A.Dog('小狗','白色','10')
console.log(d1.run())

var c1 = new B.Cat('小猫','白色','20')
console.log(c1.run())
//导出命名空的使用
import {A,B} from './modules/animal';

var ad1 = new A.Dog('小狗','白色','10')
console.log(ad1.run())

var ac1 = new B.Cat('小猫','白色','20')
console.log(ac1.run())

接口

//接口规范
interface labelVal {
    label:string; 
    size:number
}

// type 别名
type a1 = string; //基础类型
type a2 = string | number; //联合类型
type a3 = [string,number]; //元主类型

type labelObj = {
    label:string; 
    size:number
}

function print(obj:labelVal){
   console.log(obj);
}

let myObj={ size:10,label:"Size 10 Object" };
print(myObj)

//接口就相当于是一种约束,让大家都遵循


function print2(obj:labelObj){
    console.log(obj);
 }

 print2({ size:10,label:"type 别名" })

--------------------------------
interface FullName{
    firstName:string;   //注意;结束
    secondName:string;
    // secondName?:string; //可选属性,可传可不传
    hello(str:string):string
}

function printName(name:FullName){
    // 必须传入对象  firstName  secondName
    console.log(name.firstName+'--'+name.secondName);
    console.log(name)
}
// printName('1213');  //错误
/*传入的参数必须包含 firstName  secondName*/
var obj={   
    firstName:'张',
    secondName:'三',
    hello(str:string):string{
        return obj.firstName+obj.secondName;
    }
};
printName(obj)

猜你喜欢

转载自blog.csdn.net/weixin_41181778/article/details/106511044