美文网首页我爱编程
学习typescript 1

学习typescript 1

作者: 淡然7698 | 来源:发表于2018-07-23 22:23 被阅读21次
    #!/usr/bin/env ts-node
    {
      function createPrefix(n: number) {
        return "----".repeat(n);
      }
      class Person {
        public children: Person[] = [];
        constructor(public name: string) {}
        sayHi() {
          console.log("大家好我是" + this.name);
        }
        addChild(child: Person): void {
          this.children.push(child);
        }
        introduceFamily(n: number = 1): void {
          // n = n || 1;
          console.log(`${createPrefix(n - 1)}${this.name}`);
          this.children.forEach(child => {
            child.introduceFamily(n + 1);
          });
        }
      }
    
      let grandPa = new Person("王麻子");
      let child1 = new Person("王子");
      let child2 = new Person("王大锤");
      let person11 = new Person("王毛");
      let person12 = new Person("王水");
      let person21 = new Person("王农药");
      let person22 = new Person("王荣药");
    
      grandPa.addChild(child1);
      grandPa.addChild(child2);
      child1.addChild(person11);
      child1.addChild(person12);
      child2.addChild(person21);
      child2.addChild(person22);
      grandPa.introduceFamily();
    }
    

    知识点
    1 n?: number 可选参数
    2 class 类

    相关文章

      网友评论

        本文标题:学习typescript 1

        本文链接:https://www.haomeiwen.com/subject/lzjsmftx.html