1class test extends egret.DisplayObjectContainer { 2 /** 3 * 类的创建 4 */ 5 //属性 6 name: string; 7 age: number; 8 ts: test; 9 //可传参的构造方法 10 public constructor(name: string, age: number) { 11 super(); 12 this.name = name; 13 this.age = age; 14 } 15 //普通函数 16 public print() { 17 return this.name + ":" + this.age; 18 } 19 public check() { 20 //使用:利用对象调用函数的执行 21 let tc = new test("sss", 12); 22 alert(tc.print()); 23 //2 24 25 } 26 27} 28let tc = new test("sss", 12); 29alert(tc.print()); 30/** 31 * 类的继承 32 */ 33class Person { 34 name: string; 35 age: number; 36 public constructor() { } 37 //普通函数 38 public print() { 39 return this.name + ":" + this.age; 40 } 41 public check() { 42 //使用 43 44 } 45} 46class Student extends Person { 47 school: string; 48 public print() { 49 return this.name + ":" + this.age + ":" + this.school; 50 } 51} 52//调用 53var s = new Student(); 54s.name = "ss"; 55s.age = 900; 56s.school = "aa"; 57alert(s.print()); 58//或者通过构造函数直接传参,就不需要对象做赋值操作 59//Person 60/* 61public constructor(name: string, age: number) { 62 this.name = name; 63 this.age = age; 64} 65//student 66public constructor(school:string) { 67 this.school = school; 68 super("aaa",100); 69}*/
Egret 类的创建和继承
Stella981
2021-10-11
1305 0 0
点赞
收藏
评论区
加载中...