简体   繁体   English

如何在Smalltalk中访问类变量

[英]How to access class variables in Smalltalk

I am trying to access a class variable in one of my classes in Smalltalk. 我试图在Smalltalk的一个类中访问一个类变量。

I have two classes: Class1 and Class2. 我有两个类:Class1和Class2。

Class1 has the following variables: year month day hour minute. Class1具有以下变量:年月日小时分钟。 Class2 has the following variables: start-time end-time. Class2具有以下变量:start-time end-time。 In the initialize method for Class2 I have the following: 在Class2的initialize方法中,我有以下内容:

start-time := Class1 new.
end-time := Class1 new.

Now I want to assign the year 2012 to start-time, how do I access the year variable in the Class1 object start-time? 现在我想将2012年分配给开始时间,如何在Class1对象的启动时间中访问year变量?

Since you are sending new message to the classes I will assume that you are interested in instance variables , and not class variables (shared variables) (see Pharo Object Model in Updated Pharo By Example to understand the differences). 既然你发送new消息类我会假设你有兴趣的实例变量 ,而不是类变量 (共享变量)(见菲罗对象模型更新菲罗通过实例来理解的差异)。

In Pharo all class/instance variables are private, thus the way to access them is to create accessors. 在Pharo中,所有类/实例变量都是私有的,因此访问它们的方法是创建访问器。

Add to your Class1 methods 添加到Class1方法

Class1>>year
    ^ year

Class1>>year: aYear
    year := aYear

And then you can send the message to the class with the appropriate value: 然后您可以使用适当的值将消息发送到类:

Class2>>initialize
    startTime := Class1 new.
    startTime year: 2012.

    "or by using a cascade"
    startTime := Class1 new
        year: 2012;
        yourself.

If for whatever reason you needed to access a variable without accessors, you can use metaprogramming: 如果由于某种原因你需要访问没有访问器的变量,你可以使用元编程:

startTime instVarNamed: #year "meta-getter"
startTime instVarNamed: #year put: 2012 "meta-setter"

Finally, 'start-time' is not a valid variable name. 最后, 'start-time'不是有效的变量名。

I am trying to access a class variable in one of my classes in Smalltalk. 我试图在Smalltalk的一个类中访问一个类变量。

Are you sure that you want Class variables in this case? 你确定在这种情况下你想要Class变量吗? A Class variable (or attribute is held once and only once. It is accessible to all the instances of that Class, and all the instances of all it's sub-classes, as well as being accessible to the sub-classes themselves. 一个Class变量(或属性只保存一次。该类的所有实例都可以访问它,所有子类的所有实例都可以访问,并且子类本身也可以访问它。

If what you want is to spawn many of the objects, each noting a different time, or startTime and endTime, then you need to use the more ordinary instance variables. 如果你想要的是产生许多对象,每个对象注意到不同的时间,或者startTime和endTime,那么你需要使用更普通的实例变量。

If however, you want to store one time, and only one-time, then yes, you can store the information in the Class itself. 但是,如果您想存储一次,而且只存储一次,那么您可以将信息存储在类本身中。

I have two classes: Class1 and Class2. 我有两个类:Class1和Class2。

I'll call Class1 " Time " and I'll call Class2 " StartEndTime " 我将Class1称为“ Time ”,我将Class2称为“ StartEndTime

Time has the following variables: year month day hour minute . 时间有以下变量: year month day hour minute StartEndTime has the following variables: startTime endTime . StartEndTime具有以下变量: startTime endTime In the initialize method for StartEndTime I have the following: StartEndTime的initialize方法中,我有以下内容:

startTime := Time new. endTime := Time new.

Now I want to assign the year 2012 to startTime, how do I access the year variable in the object startTime? 现在我想将2012年分配给startTime,如何在对象startTime中访问year变量?

The convention is to name getter accessor methods with the same name as the attribute. 惯例是命名具有与属性相同名称的getter访问器方法。 In which case the Time object instances will have a year getter method, which returns the year of the Time object. 在这种情况下,Time对象实例将具有year getter方法,该方法返回Time对象的年份。

startTime year would then return the variable named year 然后startTime year将返回名为year的变量

Similarly, setter accessor methods have the same name as their attribute, but are suffixed with a ' : ' 类似地,setter访问器方法与其属性具有相同的名称,但后缀为' : '

startTime year: 2012 would then set the variable named year to 2012 . startTime year: 2012将名为year的变量设置为2012

Putting these into an initialize method would mean: 将这些放入initialize方法意味着:

StartEndTime >> initialize
"Returns an initialized StartEndTime"
    startTime := Time new.
    endTime := Time new.
    ^self

Time >> year: anInt
"Returns receiver with its year set to the year argument."
   year := anInt.
   ^self

In the Workspace (or Playground) 在工作区(或游乐场)

"create a new StartEndTime instanceobject"
aStartEndTime := StartEndTime new initialize.
aStartEndTime startTime: 2012.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM