首页 > 其他 > 详细

TypeScript-封装

时间:2019-04-09 22:16:06      阅读:126      评论:0      收藏:0      [点我收藏+]
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-封装

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!