zoukankan      html  css  js  c++  java
  • TypeScript学习笔记-声明合并

    类不能和其他的类或者变量合并

    /**
     * 声明合并
     * 若两个接口中存在相同的参数,那么这些相同的参数必须有相同的类型
     * 若两个接口中存在相同的函数,那么同名的函数声明都会被当成这个函数的重载,且后面的接口有更高的优先级
     */
    interface Box {
      height: number;
       number;
      clone(animal: Sheep): Sheep;
    }
    
    interface Box {
      scale: number;
      clone(animal: Dog): Dog;
    }
    
    let animal: Sheep;
    let box: Box = {  20, height: 20, scale: 30, clone(animal) {} };
    /**
     * 命名空间和类的合并
     * **********
     * 这让我们可以表示内部类
     * **********
     * 合并后的命名空间只能互相访问已经导出的成员
     */
    
    class Album {
      label: Album.AlbumLabel;
    }
    
    namespace Album {
      export class AlbumLabel {}
    }
    
    //创建一个函数后,增加它的属性,函数和命名空间的合并
    
    function buildLabel(name: string): string {
      return buildLabel.prefix + name + buildLabel.suffix;
    }
    
    namespace buildLabel {
      export let prefix = "Hello";
      export let suffix = "";
    }
    
    buildLabel("zhangsan");
    
    //枚举和命名空间的合并
    enum Color {
      red = 1,
      green = 2,
      blue = 4
    }
    
    namespace Color {
      export function mixColor(colorName: string) {
        if (colorName === "yellow") {
          return Color.red + Color.green;
        } else if (colorName == "white") {
          return Color.red + Color.green + Color.blue;
        } else if (colorName == "magenta") {
          return Color.red + Color.blue;
        } else if (colorName == "cyan") {
          return Color.green + Color.blue;
        }
      }
    }
  • 相关阅读:
    删除字符串组中相同元素,并删除值为空的元素 (转载,笔记)
    获取操作系统语言
    .net 传递中文参数解决办法
    古怪问题:vs2003程序 在繁体平台下控件位置发生变化
    Godaddy邮箱C#发送邮件设置
    无法显示隐藏文件的解决方法
    虚拟机文件
    sql 2000 修复问题
    看QQ是否在线
    sql 知识摘录
  • 原文地址:https://www.cnblogs.com/goOtter/p/9773204.html
Copyright © 2011-2022 走看看