[Newbies] Raw beginner asks.....

Ron Teitelbaum Ron at USMedRec.com
Wed May 10 19:11:46 UTC 2006


Hi Jeff,

So you thought you would start with an easy question!  Ok let me try to
help.  Forget about everything you know, forget about the interfaces you see
and all the cool pictures.  Forget about the class library and all the cool
things it can do for you and think about objects.

What's an object?  An object is like something in the real world.  With
objects when we build stuff we are building things.  Real things.  Some of
our things are coordinators or models or composites but forget about those
too and think about real things you see in your programming challenges.

If you can get to the ah-ha! point in object oriented programming you are
all set because in Smalltalk everything is objects.  In Smalltalk you get to
make up most of what you write, it's the ultimate creative language.  

So let's start with some basics. 

An object as we said is a real thing.  A class is a definition of that
thing; what it is, what it can do.  An instance is a living thing it's what
happens when you take a class and use it to create something.

So

If you subclass Object which is the class that everything inherits from.  We
will get to that in a second with an object say Person then you get a
definition of what a person should look like.

What does a person look like?

Well how about this?

Object subclass: #Person
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'People'

What is all this you say?  Well and instance variable is somewhere where you
can store information it's the data that a person knows.  Let's just leave
the rest for now.  What things does a person know?  How about name,
birthdate, marital status, sex.

So now we have: 

Object subclass: #Person
	instanceVariableNames: 'name birthdate maritalStatus sex'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'People'

That is our definition of the object Person and we call this a class.  Now
let's make an instance.

In a workspace type in: 
Person new
Highlight it and inspect it.

There you go what you see is an instance.

Right now your instance doesn't know how to do anything so we need to teach
it some tricks.

On the instance side when you are browsing your person class add in a few
methods under a new category called accessing (you will need to create the
category).

Person>>birthdate
	"return the birthdate of this person"
	^birthdate

Person>>birthdate: aDate
	"set the birthdate of this person to aDate"
	birthdate := aDate

Person>>age
	"return the age of this person"
	| yearsOld |
	self birthdate ifNil: [^'I don''t know']
	yearsOld := ((Date today - self birthdate) days / 365.25) floor.
	^yearsOld asString, 'Years Old'

Ok now we can do something.  If you ask your person it's age by typing in
the inspector on the left side:
self age.
Highlight it and print it. It can now respond to you.  If you don't set the
birthdate then it says "I don't know".
If you do set the birthdate by typing in the inspector of your instance the
following:
self birthdate: '5/10/200' asDate.  

Highlight it and do_it! Now ask age again.

Ok so what's inheritance? Well if we subclassed Person we get to keep the
definition of person the way we defined it but we get to either change what
it does (polymorphism) or add new behavior.

So let's try it,  

First make a subclass of Person.

Person subclass: #Child
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'People'

Now if you make a Child (Child new) it looks and acts just like a person
except it's called a Child that is because it inherited everything from the
superclass Person (kinda like real life).  Let's make it do something new.

First we will have to decide what class should be made when the object is
created; this is called a factory method.
http://www.google.com/search?hl=en&q=patterns+smalltalk+factory 

So on the class side we create a new method on Person.  (make sure you
change the browser to move to the class side)

Person class>>newForBirthdate: aBirthDate
	"Factory method for creating person objects"
	| anAge anInstance |
	anAge := ((Date today - aBbirthdate) days / 365.25) floor.	
	anInstance := anAge < 18 
		ifTrue: [Child new]
		ifFalse: [Person new].
	^anInstance birthdate: aBirthdate; yourself.

Ok so now if we make a new instance of someone that is under 18 we get a
child instance.  Notice that we used the same code again to calculate an
age.  So let's create a utility method to get the age from a date.

On the class side of Person add 

Person class>ageFromDate: aDate
	"return an integer representing the age as of today"

	^((Date today - aDate) days / 365.25) floor.

This method is on the class side because it's a utility method and notice
that it doesn't have the word self in it.  If there is no self in your
method it should be either a class method or you are using to many passed in
parameters or you are writing the method on the wrong class.  Always try to
solve the methods identity crisis (who am I, where do I belong)

Now you can replace where this code was used before in both places with
either:

self class ageFromDate: self birthdate

or

self ageFromDate: aBirthDate.

I'll let you decide which goes where.

So now that we have two possible objects we can start putting in some real
code.

Person>>doYouWantToBuyBeer
	"Respond to beer solicitation"
	self askBartenderForImportedBeer.

Child>>doYouWantToBuyBeer
	"Respond to beer solicitation"
	self goodChild ifTrue: [
		self notifyAthoritiesOfSomoneTryingToCoruptTheYouth.
	] ifFalse: [
		self offerUpFakeIdAndSayYes
	].

Ok well now you have some other methods to put in, but I hope you can see
the creativity that is possible with objects.  If this was no help at all
please let me know, and I didn't actually do any of this coding so if I made
a mistake or a typo my apologies up front!  If this was helpful and you have
more question, feel free to ask.

There are a number of little intricacies that I purposely put in but didn't
talk about and there are some great books
http://www.google.com/search?hl=en&lr=&safe=off&q=smalltalk+books and don't'
forget Stef's site http://www.iam.unibe.ch/~ducasse/FreeBooks.html to get
you started with Smalltalk.  Once you get to ah-ha!, it's all down hill.
Since Smalltalk is written in Smalltalk once you understand what you are
looking at, reading the class library is much easier.  

Think objects, and Happy Coding!!

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
Ron at USMedRec.com 

> -----Original Message-----
> From: beginners-bounces at lists.squeakfoundation.org [mailto:beginners-
> bounces at lists.squeakfoundation.org] On Behalf Of Jeffrey Haun
> Sent: Wednesday, May 10, 2006 1:54 PM
> To: beginners at lists.squeakfoundation.org
> Subject: [Newbies] Raw beginner asks.....
> 
> I'm just starting out in Squeak and having a hard time
> of it. For one thing, I have a little experience with
> other programming languages, but I can't make the
> connection to squeak. I don't know where to start and
> most book I have seen are not for the absolute Squeak
> beginner. I have the "bots" book, but that seem more
> like a version of LOGO. How do you write program in
> this environment??
> Help
> 
> Jeff
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners




More information about the Beginners mailing list