【问题标题】:Abstract static method on abstract class抽象类上的抽象静态方法
【发布时间】:2020-04-28 01:45:23
【问题描述】:

我记得能够为抽象类提供接口,类似这样,但我无法在任何地方在线找到该代码。我不能在这里做:

interface AbstractParent { 
  staticMember: () => boolean
}

abstract class AbstractParent {  
  static example() { 
    return this.staticMember()
  }
}

class Utilize extends AbstractParent {  
  static staticMember() { 
    return true
  }
}

console.log(Utilize.example())

Playground

是否可以让抽象类需要静态方法?

【问题讨论】:

标签: typescript


【解决方案1】:

您也可以尝试将this.staticMember() 替换为Utilize.staticMember()Playground

理想情况下,您想使用static abstract staticMember。但不幸的是,它尚未得到支持,而且似乎没有实现此功能的好方法。

查看我们的这些讨论:

【讨论】:

    【解决方案2】:

    这可以正常工作,但不能确保 Utilize 实现 staticMember 方法。

    abstract class AbstractParent {  
      static staticMember: () => boolean
      static example() { 
        return this.staticMember()
      }
    }
    
    class Utilize extends AbstractParent {  
      static staticMember() { 
        return true
      }
    }
    
    console.log(Utilize.example())
    

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 2011-11-15
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      相关资源
      最近更新 更多