ts中的类

  • Post author:
  • Post category:其他


ts中定义类

class Person{
    name:string;
    constructor(name:string){
        this.name=name
    };
    run():void{
        alert(this.name)
    }
}
var p=new Person('里斯');
p.run()

ts类中实现set和get

class Person{
    name:string;
    constructor(name:string){
        this.name=name
    };
    getName():string{
        return this.name
    };
    setName(name:string):void{//变量要写类型,这个容易忘,就是要靠多敲多练。
        this.name=name
    }
}
var p=new Person('宽真')
alert(p.getName())
p.setName('大神')
alert(p.getName())

2.ts中实现继承 extends super

父类中的方法,子类可以直接使用

    class Person{
        name:string;
        constructor(name:string){
            this.name=name
        };
        run():string{
            return `${this.name}在跑步`
        }
    }
    var p=new Person('王五')
    console.log(p.run())
    class Web extends Person{
        constructor(name:string){
            super(name)//初始化父类的构造函数
        };
    }
    let w=new Web('小新')
    console.log(w.run())

ts中继承的探讨 父类的方法和子类的方法一致,子类会替换父类

    class Person{
        name:string;
        constructor(name:string){
            this.name=name
        };
        run():string{
            return `${this.name}在游泳`
        }
    }
    class Web extends Person{
        constructor(name:string){
            super(name)
        }
        run(){
            return `${this.name}在奔跑`
            // super.run()//这种貌似是es6中直接引用父类的方法,但是这里是不行的。
        }
        work(){
            console.log(`${this.name}在工作`)
        }
    }
    let w=new Web('赵六');
    console.log(w.run());
    w.work();

3.类里面的修饰符

// public:己,子,外

// protected:己,子

// private:己

// 属性不加修饰符,默认是公有

public修饰符
    class Person{
        public name:string
        constructor(name:string){
            this.name=name
        }
        run():string{
            return `${this.name}在运动`
        }
    }
    let p=new Person('李四');
    console.log(p.name)
    console.log(p.run())
    class Web extends Person{
        constructor(name:string){
            super(name)
        }
        work():string{
            return `${this.name}在工作`
        }
    }
    let web=new Web('成龙')
    console.log(web.name)
    console.log(web.work())
protected修饰符,外部不能访问
        class Person{
            protected name:string
            constructor(name:string){
                this.name=name
            }
            run():string{
                return `${this.name}在运动`
            }
        }
        let p=new Person('李四');
        // console.log(p.name)//[ts] 属性“name”受保护,只能在类“Person”及其子类中访问。
        // console.log(p.run())


        class Web extends Person{
            constructor(name:string){
                super(name)
            }
            work():string{
                return `${this.name}在工作`
            }
        }
        let web=new Web('成龙')
        console.log(web.name)//[ts] 属性“name”受保护,只能在类“Person”及其子类中访问。
        console.log(p.name)//[ts] 属性“name”受保护,只能在类“Person”及其子类中访问。
        
 private

            class Person{
                private name:string
                constructor(name:string){
                    this.name=name
                }
                run():string{
                    return `${this.name}在运动`
                }
            }
            let p=new Person('李四');
            // console.log(p.name)//[ts] 属性“name”受保护,只能在类“Person”及其子类中访问。
            // console.log(p.run())


            class Web extends Person{
                constructor(name:string){
                    super(name)
                }
                work():string{
                    return `${this.name}在工作`//[ts] 属性“name”为私有属性,只能在类“Person”中访问。
                }
            }
            let web=new Web('成龙')
            console.log(web.name)//[ts] 属性“name”为私有属性,只能在类“Person”中访问。
            console.log(p.name)//[ts] 属性“name”为私有属性,只能在类“Person”中访问。



版权声明:本文为u010856177原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。