Egret 类的创建和继承

class test extends egret.DisplayObjectContainer {
    /**
     * 类的创建
     */
    //属性
    name: string;
    age: number;
    ts: test;
    //可传参的构造方法
    public constructor(name: string, age: number) {
        super();
        this.name = name;
        this.age = age;
    }
    //普通函数
    public print() {
        return this.name + ":" + this.age;
    }
    public check() {
        //使用:利用对象调用函数的执行
        let tc = new test("sss", 12);
        alert(tc.print());
        //2

    }

}
let tc = new test("sss", 12);
alert(tc.print());
/**
 * 类的继承
 */
class Person {
    name: string;
    age: number;
    public constructor() { }
    //普通函数
    public print() {
        return this.name + ":" + this.age;
    }
    public check() {
        //使用

    }
}
class Student extends Person {
    school: string;
    public print() {
        return this.name + ":" + this.age + ":" + this.school;
    }
}
//调用
var s = new Student();
s.name = "ss";
s.age = 900;
s.school = "aa";
alert(s.print());
//或者通过构造函数直接传参,就不需要对象做赋值操作
//Person
/*
public constructor(name: string, age: number) {   
    this.name = name;
    this.age = age;
}
//student
public constructor(school:string) {   
    this.school = school;
    super("aaa",100);
}*/

评论

暂无评论