码迷,mamicode.com
首页 > 其他好文 > 详细

TypeScript-封装

时间:2019-04-09 22:12:15      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:with   type   判断   接口   rip   property   pes   private   sharp   

class People {
	private _name: string;
	age: number; 
	print() {
		return this._name + ":" + this.age + ":";
	}
    //加入封装,使外部可使用私有
    get name():string{
        return this._name;
    }
    set name(newName:string){
        this._name=newName;
        //可加判断
        if(newName =="aaa"){ 
        }else{ 
        }
    } 
}
let h = new People();
// h.name ="aaa";//Property ‘name‘ is private and only accessible within class ‘People‘
//接口
h.name = "sss";
h.age = 19;
alert(h.print());
 

  

TypeScript-封装

标签:with   type   判断   接口   rip   property   pes   private   sharp   

原文地址:https://www.cnblogs.com/allyh/p/10680132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!