简体   繁体   中英

How to implement getter in smalltalk pharo

I have class Person with my setters and getters like this:

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

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. For example in Java it Will be like this:

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.

There is a MOOC: http://mooc.pharo.org/ .
There are a lot of free books here: http://books.pharo.org
Here you have some free general books: 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).

I wanted to explain why Esteban's advice is important, and why it is especially good advice in the case of 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".

This makes if fair to ask "how do I do X in language Y" (as you just did). 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) .

Smalltalk is different. Because Smalltalk is built orthogonally on a very small, simple and consistent design of concepts and syntax. 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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