ts中的类

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”中访问。

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值