[英]How to access property in extended class in Coffeescript?
I want to use @Type
in a if statement, but it seems as if it is not recognizing @Type
. 我想在if语句中使用
@Type
,但好像它没有识别@Type
。 Is there a way to get @Type
so that I can use it in the superclass? 有没有办法获得
@Type
以便我可以在超类中使用它?
class opponent
constructor: (ID, Level, Name) ->
@ID = ID
@Level = Level
@Name = Name
@Health = if @Level is 1
@Level * 5
else if 2 <= @Level <= 4
(@Level * 6) - (@Level * 2)
@Luck = if @Type is "Snake"
Math.ceil(@Level * 1.25) + 5
else
Math.ceil(@Level * 1.25)
@attackDamage = 0
@defenseDoubled = false;
@Poisoned = false;
@Burned = false;
@Frozen = false;
@defend: ->
@Defense *= 2
@DefenseDoubled = true;
@undefend: ->
@Defense /= 2
@DefenseDoubled = false;
class Snake extends opponent
@Type: "Snake"
You can get to the "class" using the constructor
property : 您可以使用
constructor
属性访问 “类”:
Returns a reference to the
Object
function that created the instance's prototype.返回对创建实例原型的
Object
函数的引用。
So if s
is a snake, then s.constructor
is Snake
. 所以如果
s
是蛇,那么s.constructor
就是Snake
。 That means that you can do things like this: 这意味着你可以做这样的事情:
class A
m: -> console.log(@constructor.type)
class B extends A
@type = 'B'
class C extends A
@type = 'C'
(new B).m()
(new C).m()
and get 'B'
and 'C'
in the console. 并在控制台中获取
'B'
和'C'
。
Demo: http://jsfiddle.net/ambiguous/bE6jh/ 演示: http : //jsfiddle.net/ambiguous/bE6jh/
In your particular case, you'd want to look at @constructor.Type
inside the method that wants to know the type. 在您的特定情况下,您需要在想要知道类型的方法中查看
@constructor.Type
。
This doesn't answer your question (of how to make a subclass attribute visible to the parent class), but it should produce the same result - in a manner that easily expands. 这不回答你的问题(如何使子类属性对父类可见),但它应该以一种容易扩展的方式产生相同的结果。 You can add more subclasses without going back and changing
opponent
each time. 您可以添加更多子类,而无需每次都返回并更改
opponent
。
class opponent
constructor: (ID, Level, Name) ->
@ID = ID
@Level = Level
@Name = Name
@Health = if @Level is 1
@Level * 5
else if 2 <= @Level <= 4
(@Level * 6) - (@Level * 2)
@Luck = Math.ceil(@Level * 1.25)
@attackDamage = 0
# ...
class Snake extends opponent
constructor : (ID, Level, Name) ->
# use parent constructor to create the object
# and then customize the values for this class
super
@Luck = Math.ceil(@Level * 1.25) + 5
Your code does not work because in the parent class, you are referencing an instance variable, @Type
, but in the Snake subclass, you are defining a class variable, @Type: "Snake"
. 您的代码不起作用,因为在父类中,您引用的是实例变量
@Type
,但在Snake子类中,您正在定义一个类变量,@ @Type: "Snake"
。
To declare the Type
variable as instance variable, declare it without the @
sign: 要将
Type
变量声明为实例变量,请在没有@
符号的情况下声明它:
class Snake extends opponent
Type: "Snake"
To further expand on hpaulj's answer - you always want to keep your code DRY, so the logic that calculates the @Luck attribute should be only defined in the parent class. 为了进一步扩展hpaulj的答案 - 你总是希望保持你的代码DRY,所以计算@Luck属性的逻辑应该只在父类中定义。 Consider the following example:
请考虑以下示例:
class opponent
constructor: ( args... ) ->
# ... your logic
@Luck = Math.ceil(@Level * 1.25) + ( @LuckFactor? or 0 )
class Snake extends opponent
LuckFactor: 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.