简体   繁体   English

如何在smalltalk pharo中实现getter

[英]How to implement getter in smalltalk pharo

I have class Person with my setters and getters like this:我有这样的settersgetters类的Person类:

Object subclass: #Person
instanceVariableName: 'name document'
classVariableName ' '
package: 'foo'

name
^name

name: anObject
name:= anObject

document
^document

document: anObject
document:= anObject

Then i instance my class in my TestPerson然后我在我的TestPerson实例化我的类

setUp
p1:= Person name:'Alice' document:='12345'

So here i don't undestand how can i implement my getter to compare if my name is really Alice.所以在这里我不明白如何实现我的 getter 来比较我的名字是否真的是 Alice。 For example in Java it Will be like this:例如在 Java 中它会是这样的:

p1.getName().equals("Alice");
p1 name = 'Alice'

You really need to read a book on Smalltalk, you are missing the basics and "learning by stack overflow" does not seems to be the best way.你真的需要阅读一本关于 Smalltalk 的书,你缺少基础知识,“通过堆栈溢出学习”似乎不是最好的方法。

There is a MOOC: http://mooc.pharo.org/ .有一个 MOOC: http : //mooc.pharo.org/
There are a lot of free books here: http://books.pharo.org这里有很多免费书籍: http : //books.pharo.org
Here you have some free general books: http://stephane.ducasse.free.fr/FreeBooks.html这里有一些免费的普通书籍: http : //stephane.ducasse.free.fr/FreeBooks.html
And here you can find more general documentation: http://pharo.org/documentation (you can watch some introductory screencasts there).在这里你可以找到更多的通用文档: http : //pharo.org/documentation (你可以在那里观看一些介绍性的截屏视频)。

I wanted to explain why Esteban's advice is important, and why it is especially good advice in the case of Smalltalk.我想解释为什么 Esteban 的建议很重要,以及为什么在 Smalltalk 的情况下它是特别好的建议。

Many other languages use very basic programming concepts that every seasoned programmer would already know, but these are drowned into an ocean of special syntax, edge cases, exceptions, multiple layers of often inconsistent or unrelated detailed arbitrary language design rules.许多其他语言使用每个经验丰富的程序员都已经知道的非常基本的编程概念,但这些概念被淹没在特殊语法、边缘情况、异常、多层通常不一致或不相关的详细任意语言设计规则的海洋中。 Therefore, when you learn these languages the challenge is often indeed "how do i do X (a simple concept you already know) in language Y".因此,当你学习这些语言时,挑战往往确实是“我如何用 Y 语言做 X(你已经知道的一个简单概念)”。

This makes if fair to ask "how do I do X in language Y" (as you just did).这使得如果公平地问“我如何用 Y 语言做 X”(就像你刚刚做的那样)。 It also makes it difficult to use the books or documentation available about that language, because they are either going to try to teach you X all over again (but you already know X, you just want to know how to do it in Y!), or they are going to be a long list of special tips and tricks shining a light on all the special cases and idiosyncrasies of language Y (and they might actually not cover your particular question, or if they do, you would not easily find it in the materials) .这也使得使用有关该语言的书籍或文档变得困难,因为它们要么会尝试重新教你 X(但你已经知道 X,你只想知道如何在 Y 中做到这一点!) ,或者它们将是一长串特殊提示和技巧,阐明 Y 语言的所有特殊情况和特性(它们实际上可能没有涵盖您的特定问题,或者如果它们涵盖了,您将不会轻易找到它在材料中)。

Smalltalk is different. Smalltalk 则不同。 Because Smalltalk is built orthogonally on a very small, simple and consistent design of concepts and syntax.因为 Smalltalk 是建立在一个非常小的、简单和一致的概念和语法设计上的。 So, with Smalltalk, you can afford the time to read the book (it is short: the syntax famously fits on a postcard and the concepts are equally few and simple).因此,使用 Smalltalk,您可以花时间阅读本书(它很短:语法非常适合明信片,而且概念同样少而简单)。 The book will cover most and all of the special cases, because... There mostly aren't any such cases.这本书将涵盖大多数和所有的特殊情况,因为……大多数情况下没有这样的情况。 Your knowledge will then apply universally.然后,您的知识将普遍适用。 It will work horizontally (in all parts of the system) and vertically (at the highest and lowest levels of abstraction in the system).它将水平(在系统的所有部分)和垂直(在系统中的最高和最低抽象级别)工作。

It is such a liberating feeling to be able to focus on your own problem, knowing that the language supports you and does not get in the way, instead of wasting your mental energy forcing you to remember silly arbitrary things.能够专注于自己的问题,知道语言支持你并且不会妨碍你,而不是浪费你的精神能量迫使你记住愚蠢的任意事情,这是一种解放的感觉。

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

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