【发布时间】: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