【问题标题】:Namespaces or categories in TypeScript classTypeScript 类中的命名空间或类别
【发布时间】:2019-08-01 13:02:12
【问题描述】:

我有一个基类,其方法可以访问不同类型的对象:

- findPropertyAInItemY(items)
- getPropertyBInItemZ()
- getItemY()
- getAlltems()

他们有时访问私有类变量,有时不访问。这些方法有时有参数,有时没有。有不同的情况...

这些方法名称变得很长,但它们准确地描述了它们返回的内容。但是方法很多。因此,我希望有这样的结构:

- Properties.getPropertyA()
- Properties.findPropertyB()
- Items.getItemY()
- Item.getAllItems()

在我的基类中。这有可能吗?

【问题讨论】:

标签: typescript


【解决方案1】:

我不能百分百确定你想要达到什么目标,但你可以通过多种方式得到你想要的。尽管static 版本看起来与您所要求的完全一样,但通常最好在您的构造函数中接受一个满足依赖关系的类的实例(但这会扩大问题的范围)。

class Properties {
  getPropertyA() {

  }

  findPropertyB() {

  }

  static getPropertyC() {

  }
}

class Example {
  private properties = new Properties();

  example() {
    this.properties.getPropertyA();
  }

  example2() {
    Properties.getPropertyC();
  }
}

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2013-03-25
    • 2019-03-18
    • 2012-06-19
    相关资源
    最近更新 更多