开发者

Problems with classes (super new)

开发者 https://www.devze.com 2022-12-26 04:49 出处:网络
I\'ve problems to figure it out what\'s happening in the following exercise, I\'m learning Smalltalk, so I\'m newbie.

I've problems to figure it out what's happening in the following exercise, I'm learning Smalltalk, so I'm newbie.

Class A>>new ^super new initialize.

开发者_StackOverflow社区

A>>initialize a:=0.

Class B>>new: aParameter |instance| instance := super new. instance b: instance a + aParameter. ^instance

B>>initialize b:=0.

The problem says what happen when the following code is executed:

B new:10.

But I can't not figure it out why instance variable does not belong to A class.

Thanks


I discovered the issue, the missing of super initialize, when the code was executed the initializer of "instance := super new." was the initializer of class B, that's why the A instance var was not initialized, the following code fix the issue:

B>>initialize

super initialize.

b:=0.

0

精彩评论

暂无评论...
验证码 换一张
取 消