TypeScriptで名前空間(namespace)を利用して継承(extends)してみた

TypeScriptで名前空間をmoduleで書く必要があったらしいが、1.5.3からnamespaceで表現出来るらしいのでPlaygroundを使ってさくっと書いて動作確認してみた

TypeScript早わかりチートシート【1.5.3対応】 - Build Insider

クラスのサンプルはここから 継承 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C

namespace Nature {
    export class Person
    {
        /*
       private string name; // 名前
       private int    age;  // 年齢
       */
        constructor(public name:string ,public age: number){
        }
        
        get Name():string{
            return this.name;
        }
        set Name(name: string){
        }
        
        get Age():number{
            return this.age;
        }
        set Age(age: number){
        }
    }
    
}

namespace School{
    export class Student extends Nature.Person
    {
        /*
       private int    id;   // 学籍番号
       */
        constructor(public name: string,public age: number,public id: number)  {
            super(name,age);
        }
        
        get Id(): number{
            return this.id;
        }
        set Id(id: number){
        }
    }    
}

let p1 = new Nature.Person("麻倉葉",13);
alert(`${p1.Name}は${p1.Age}歳。`)

let s1 = new School.Student("小山田まん太",13,1);
alert(`学籍番号${s1.Id}、${s1.Name}は${s1.Age}歳。`);

出力されたJavaScript

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Nature;
(function (Nature) {
    var Person = (function () {
        /*
        private string name; // 名前
        private int    age;  // 年齢
        */
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        Object.defineProperty(Person.prototype, "Name", {
            get: function () {
                return this.name;
            },
            set: function (name) {
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(Person.prototype, "Age", {
            get: function () {
                return this.age;
            },
            set: function (age) {
            },
            enumerable: true,
            configurable: true
        });
        return Person;
    }());
    Nature.Person = Person;
})(Nature || (Nature = {}));
var School;
(function (School) {
    var Student = (function (_super) {
        __extends(Student, _super);
        /*
        private int    id;   // 学籍番号
        */
        function Student(name, age, id) {
            _super.call(this, name, age);
            this.name = name;
            this.age = age;
            this.id = id;
        }
        Object.defineProperty(Student.prototype, "Id", {
            get: function () {
                return this.id;
            },
            set: function (id) {
            },
            enumerable: true,
            configurable: true
        });
        return Student;
    }(Nature.Person));
    School.Student = Student;
})(School || (School = {}));
var p1 = new Nature.Person("麻倉葉", 13);
alert(p1.Name + "\u306F" + p1.Age + "\u6B73\u3002");
var s1 = new School.Student("小山田まん太", 13, 1);
alert("\u5B66\u7C4D\u756A\u53F7" + s1.Id + "\u3001" + s1.Name + "\u306F" + s1.Age + "\u6B73\u3002");

気になったのは、Personクラスの引数プロパティはprivateにしていると、別の名前空間extends 出来なかったところ。 深くほりさげるのはまた今度。

Playgroundサンプル [http://www.typescriptlang.org/play/index.html#src=namespace%20Nature%20%7B%0D%0A%09export%20class%20Person%0D%0A%09%7B%0D%0A%09%09%2F%0D%0A%09%09private%20string%20name%3B%20%2F%2F%20%E5%90%8D%E5%89%8D%0D%0A%09%09private%20int%20%20%20%20age%3B%20%20%2F%2F%20%E5%B9%B4%E9%BD%A2%0D%0A%09%09%2F%0D%0A%09%09constructor(public%20name%3Astring%20%2Cpublic%20age%3A%20number)%7B%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09get%20Name()%3Astring%7B%0D%0A%09%09%09return%20this.name%3B%0D%0A%09%09%7D%0D%0A%09%09set%20Name(name%3A%20string)%7B%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09get%20Age()%3Anumber%7B%0D%0A%09%09%09return%20this.age%3B%0D%0A%09%09%7D%0D%0A%09%09set%20Age(age%3A%20number)%7B%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%7D%0D%0A%0D%0Anamespace%20School%7B%0D%0A%09export%20class%20Student%20extends%20Nature.Person%0D%0A%09%7B%0D%0A%09%09%2F%0D%0A%09%09private%20int%20%20%20%20id%3B%20%20%20%2F%2F%20%E5%AD%A6%E7%B1%8D%E7%95%AA%E5%8F%B7%0D%0A%09%09%2F%0D%0A%09%09constructor(public%20name%3A%20string%2Cpublic%20age%3A%20number%2Cpublic%20id%3A%20number)%20%20%7B%0D%0A%09%09%09super(name%2Cage)%3B%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09get%20Id()%3A%20number%7B%0D%0A%09%09%09return%20this.id%3B%0D%0A%09%09%7D%0D%0A%09%09set%20Id(id%3A%20number)%7B%0D%0A%09%09%7D%0D%0A%09%7D%09%0D%0A%7D%0D%0A%0D%0Alet%20p1%20%3D%20new%20Nature.Person(%22%E9%BA%BB%E5%80%89%E8%91%89%22%2C13)%3B%0D%0Aalert(%60%24%7Bp1.Name%7D%E3%81%AF%24%7Bp1.Age%7D%E6%AD%B3%E3%80%82%60)%0D%0A%0D%0Alet%20s1%20%3D%20new%20School.Student(%22%E5%B0%8F%E5%B1%B1%E7%94%B0%E3%81%BE%E3%82%93%E5%A4%AA%22%2C13%2C1)%3B%0D%0Aalert(%60%E5%AD%A6%E7%B1%8D%E7%95%AA%E5%8F%B7%24%7Bs1.Id%7D%E3%80%81%24%7Bs1.Name%7D%E3%81%AF%24%7Bs1.Age%7D%E6%AD%B3%E3%80%82%60)%3B]