【问题标题】:How to inherit classes with arguments in coffescript?如何在咖啡脚本中继承带有参数的类?
【发布时间】:2016-12-29 21:24:51
【问题描述】:

我有 Human 类,我希望 SocialBeing 类继承自 Human。但是在super(Human) 方法中,Human 类作为第一个位置参数传递给Human 类实例,这是错误的。从Human继承SocialBeing的正确方法是什么?

class Human  # As biological creature
    constructor: (@given_sex = null,
                  @age = null,  # Age of the person
                  @max_age = 85) ->  # Maximum allowed age of the person during person generation

        _alive = true

        alive: ->
            @_alive

        dead: ->
            not @alive()

        has_died: ->
            @_alive = false

        _available_sexes: {0: 'female', 1: 'male'}

        sex: ->
            _sex = @_available_sexes[@given_sex]

        @generate_human()

     generate_human: ->
            @_alive = true
            if @age is null
                @age = Math.floor(Math.random() * @max_age)
            if @given_sex is null
                @given_sex = Math.floor(Math.random() * 2)
            else if @given_sex not in [0,1]
                n = @given_sex
                err = 'Invalid sex value: ' + n
                console.log(err)
                throw new Error(err)


class SocialBeing extends Human  # Describes socialisation
    constructor: (@first_name = null,
                  @second_name = null,
                  @middle_name = null,
                  @other_name = null) ->
         super(Human)

         marital_status: null

h = new SocialBeing(first_name='Pal')  # In JavaScript thows an error

【问题讨论】:

    标签: class inheritance coffeescript


    【解决方案1】:

    我认为您以错误的方式使用super。 我已经把你的例子浓缩了一点

    class Human
         constructor: (@given_sex = null) ->  
              @generate_human()
    
         generate_human: ->
             if @given_sex is null
                 @given_sex = Math.floor(Math.random() * 2)
             else if @given_sex not in [0,1]
                 n = @given_sex
                 err = 'Invalid sex value: ' + n
                 console.log(err)
             else
                  console.log("my sex is", @given_sex)
    

    到目前为止,您的基类还可以。现在,对于您的派生类,您可以在调用 super 时显式设置参数

    class SocialBeing extends Human
         constructor: (@first_name = null) ->
              super 1
              #or
              super null
    

    但不知何故,这违背了基类构造函数参数的目的。通常的方法是将基类构造函数参数添加到派生类构造函数参数中

    class SocialBeing extends Human
         constructor: (sex = null, @first_name = null) ->
              super sex
    

    然后相应地实例化您的派生类

    h = new SocialBeing(sex = 1, first_name='Pal')
    

    而描述性常量也很好

    male = 0
    female = 1
    h = new SocialBeing(male, 'Pal')
    

    如果您觉得特别进步,或者添加 FB 的 58 种性别表达中的任何一种;-)

    现在显然将所有这些 ctor 参数通过管道传输到 super 中会变得有点麻烦。那么你可以做的是

    class Human
          constructor: (@given_sex = null,
                        @age = null,  # Age of the person
                        @max_age = 85)
    
    class SocialBeing extends Human
         constructor: (@first_name = null,
                       @second_name = null,
                       @middle_name = null,
                       @other_name = null) ->
              super arguments...
    

    但是有这么多参数,特别是可选的参数,我认为这是一种设计味道。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-04
      • 2013-06-28
      • 1970-01-01
      • 2013-01-09
      • 2015-10-23
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多