简体   繁体   English

如何在 Pharo 中更改类的实例变量?

[英]How to change the instance variable of a class in Pharo?

I created a class named Die (singular of Dice) and set an instance variable side .我创建了一个名为Die (骰子的单数)的类并设置了一个实例变量side

Object subclass: #Die
    instanceVariableNames: 'side'
    classVariableNames: ''
    package: 'MyPackage'

I initialized the side variable to 6 by default:我默认将side变量初始化为 6:

initialize

 side := 6. 

However I would like the ability to change the instance variable after the object has been created, like so:但是,我希望能够在创建对象后更改实例变量,如下所示:

myDie := Die new.
myDie side: 10. " it doesn't to work "

It seems to me that instance variables can only be changed by class methods.在我看来,实例变量只能通过类方法来改变。 Is there a way to change it like above?有没有办法像上面那样改变它?

Also, I have a method roll that returns a random number between 1 and the specified side.另外,我有一个方法roll返回 1 和指定边之间的随机数。 If I create two dice and want to roll two dice at the same time, or maybe three dice at the same time, should I create a different class, something like papercup feed that with the number of dices and implement the roll method on the papercup instead?如果我创建两个骰子并想同时掷两个骰子,或者同时掷三个骰子,我是否应该创建一个不同的类,比如papercup送入的骰子数量并在纸杯上实现滚动方法反而?

Thank you.谢谢你。

You are on a right track.你走在正确的轨道上。 When you execute当你执行

myDie side: 10.

You send a message side: to myDie which is an instance of a class Die .您将消息side:发送到myDie ,它是类Die的实例。 To "respond" to this message, the class die (or its superclasses) should implement a side: method.要“响应”此消息,类 die(或其超类)应实现side:方法。 Ideally, this method is going to look like this:理想情况下,此方法将如下所示:

side: aNumber

    side := aNumber

You see, takes encapsulation very seriously, so instance variables can be directly accessed only by the object itself, and not by anyone from the outside.你看, 非常重视封装,所以实例变量只能被对象本身直接访问,而不能被外部的任何人访问。 This is why you always have to create accessors, which is a good practice followed to a various extent by many programming languages.这就是为什么您总是必须创建访问器的原因,这是许多编程语言在不同程度上遵循的一种很好的做法。

Regarding your second question: it's a good idea to have a Papercup class, but if you don't need extensive features, the exact task can be achieved by the following code:关于你的第二个问题:有一个 Papercup 类是个好主意,但如果你不需要广泛的功能,可以通过以下代码实现确切的任务:

papercup := OrderedCollection new.
papercup
    add: die1;
    add: die2;
    add: die3.
papercup collect: #roll

This way you will roll all the 3 dies and return a collection for 3 numbers ( collect: is the same as map() in many programming languages).这样,您将掷出所有 3 个骰子并返回 3 个数字的集合( collect:与许多编程语言中的map()相同)。 I provided a shorthand version, whereas the full way to write a collect is: papercup collect: [ :die | die roll]我提供了一个速记版本,而写收集的完整方法是: papercup collect: [ :die | die roll] papercup collect: [ :die | die roll] when you specify a complete block (closure) and send the roll message to each die. papercup collect: [ :die | die roll]当您指定一个完整的块(关闭)并将roll消息发送到每个骰子时。

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

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