为什么这个类/实例变量没有被初始化? [英] Why this class/instance variable is not being intialized?
问题描述
我正在尝试使用 gnu-smalltalk.在带有变量的简单类的以下代码中,我发现它没有被初始化为给定的值:
I am trying to use gnu-smalltalk. In following code of a simple class with a variable, I find it is not getting initialized to the given value:
Object subclass: Myclass[
|mainval|
mainval := 555.
getmainval [^mainval]
]
gc := Myclass new.
gc getmainval printNl.
对象创建时没有任何错误.但是,输出是:
The object gets created without any error. But, the output is:
nil
虽然我原以为是 555.
while I had expected it to be 555.
如果我添加一个方法来为它赋值并在创建类的实例后调用它,它就可以工作.
If I add a method to assign a value to it and call it after creating an instance of the class, it works.
问题出在哪里,如何解决?谢谢.
Where is the problem and how can it be corrected? Thanks.
推荐答案
我认为您已经很接近了,但是您尝试了一个不起作用的快捷方式,因为您期望将值设置为类定义中的实例变量(在方法和本地有效性之外)将返回值.实际上,您想在方法中使用实例变量,例如init
表示对象的内部状态.
I think you were close but you tried a shortcut which does not work due to the fact that you are expecting that setting value to an instance variable within class definition (outside a method and local validity) will return value. In reality you want to use an instance variable within a method e.g. init
to express the inner state of an object.
正如我在之前的一些答案中所指出的,我不是 GNU Smalltalk 专家.我正在使用 Smalltalk/X-jv 分支进行编程.
As I have noted in some previous answers, I'm not a GNU Smalltalk expert. I'm using Smalltalk/X-jv branch for programming.
我冒昧地将您的代码重写为更多类似 GNU Smalltalk 的代码:
I have taken the liberty to rewrite your code to more GNU Smalltalk like code:
Object subclass: MyClass [
| mainVal |
<comment:
'testing main value'>
MyClass class >> new [
<category: 'instance creation'>
| myClass |
myClass := super new.
myClass init.
^ myClass
]
init [
<category: 'initialization'>
mainVal := 555.
]
mainVal [
^ mainVal
]
]
说明:
我正在定义 mainVal
实例变量.然后我重新定义了一个 new
类方法,它不需要,但为了说明目的,我这样做了.(new
消息继承自 Object
类)
I'm defining the mainVal
instance variable. Then I'm redefining a class method new
which is not needed but for illustration purposes I'm doing so. (The new
message is inherited from Object
class)
我在类方法new
中所做的.我将 new
消息发送到创建 anObject
实例的 superclass
,然后使用 myClass
初始化 myClass
code>init 然后返回它.
What I'm doing in the class method new
. I'm sending the new
message to the superclass
which creates instance of anObject
, then initializing the myClass
with init
and then returing it.
之后您可以看到 init
类,它将您的实例变量初始化为您希望的 555
值.
Afterwards you can see the init
class which initialize your instance variable to your wished value of 555
.
然后你有一个 getter(在 Smalltalk 中通常没有 get 前缀,但这只是"风格问题)mainVal
.
Then you have a getter (in Smalltalk it is usual to have it without the get prefix, but that is "only" matter of style) the mainVal
.
然后是调用对象并获取值的代码.
Then your code to call the object and get value.
gc := MyClass new.
gc mainVal
没有new
消息重新定义的最小示例:
Object subclass: MyClass [
| mainVal |
<comment:
'testing main value'>
init [
<category: 'initialization'>
mainVal := 555.
]
mainVal [
^ mainVal
]
]
您不能忘记的是,在 Smalltalk 中通常没有构造函数的默认调用,因此您必须手动发送 init
消息(另一种选择是重新定义 new
> 消息,就像我上面所做的那样).
What you must not forget that in Smalltalk there is usually no default calling of a constructor so you have to send the init
message manually (the other option is to redefine new
message like I did above).
然后使用它看起来像这样:
Then using it would look like this:
gc := MyClass new.
a MyClass
st> gc init
a MyClass
st> gc mainVal
555
这篇关于为什么这个类/实例变量没有被初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!