TypeScript -访问修饰符

class test extends egret.DisplayObjectContainer {
	public constructor() {
		/**
		 * 1.不添加构造函数constructor
		 */
		// var t = new Teacher();
		// t.age = 30;
		// t.name = "111";
		// t.school = "ss";
		// alert(t.print());
		/**
		 * 2.添加构造函数
		 * 此时将People内的name设置为私有,则会显示error ,因为无法直接使用
		 */
		super();
		let t=new Teacher("sssssss");
		alert(t.print());
	}
}
/**
 * public :若没有出现访问修饰符,默认的是public:公共的,项目内都可调用
 * private:private表示私有,除了class自己之外,任何其他脚本都不可以直接使用
 */
class People {
	public name: string;
	age: number;
	print() {
		return this.name + ":" + this.age;
	}
	constructor( name: string, age: number) {
		this.name = name;
		this.age = age;
	}
}
class Teacher extends People {
	school: string;
	print() {
		return this.name + ":" + this.age + ":" + this.school;
	}
	constructor(school: string) {
		super("aaa", 1111);
		this.school = school;
	}
} 

  

猜你喜欢

转载自www.cnblogs.com/allyh/p/10680125.html