bousozoku said:I've dabbled in it but I'm not proficient. It's a good thing to learn prior to learning Objective-C.
scan said:unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method
scan said:unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method
[NSObject alloc];
[myInstance alterData];
+alloc;
-alterData;
Jordan72 said:Out of curiosity, where did you get your Smalltalk compiler, is it for Mac?
scan said:pardon my ignorance, but could someone give me an example of how instance and class methods are used and when to use them? or perhaps comparing it with Java?
I have another question about smalltalk. How do loops work? I know there is no loops in smalltalk but there is a do method I believe? i'm not sure how the syntax goes.
// Java loop
for(int x=1; x<=20; x++) {
System.out.println("" + x);
}
1 to: 20 do: [:x | x printNl ] !
mrichmon said:Smalltalk class methods are equivalent to static methods in Java.
Smalltalk instance (or object) methods are equivalent to object methods in Java.
Loops in Smalltalk are handled by passing a block which contains the instructions for the body of the loop as an argument to the do: method.
For example:
is written in Smalltalk by:Code:// Java loop for(int x=1; x<=20; x++) { System.out.println("" + x); }
Code:1 to: 20 do: [:x | x printNl ] !
scan said:yeah i'm using squeak. i've never had this much trouble with a programming language.
mrichmon said:The thing about Smalltalk is that you absolutely must understand object-oriented programming. Most other less pure OO languages (such as C++, Java, C#, to some extent Objective-C) you can get away with only vague ideas of OO design.
For example, sub-typing is not the same as inheritence. Polymorphism does not necessarily require class inheritence. In Java and C++ it is easy to consider these three concepts to be the same thing. In fact, there are important differences.
The best way to get the concepts straight is to read the first chapter or two of a decent Smalltalk book and really take time to understand the information. Many of the Smalltalk books are structured in the same way using the same examples to illustrate concepts in the early chapters. This is because many Smalltalk books follow the examples given in the Smalltalk blue book. So most any book will do. If pressed I would recommend "Smalltalk: An Introduction to Application Development Using VisualWorks" by Hopkins and Horan, or "Smalltalk-80: The Language" by Goldberg and Robson (otherwise known as the Purple book... the Purple book is a reprint of the first 3 parts from the Blue book).
scan said:yeah i'm using squeak. i've never had this much trouble with a programming language.
I'm still not quite sure about the class and instance metods. So if you create an object of some class, then it can only use the instance methods? so how would the class methods be used? i'm unclear about that
GeeYouEye said:Objective-C has full support for class methods, but currently little to no support for class variables (say you wanted to track the number of instances of a class), though it did in the past.
The Objective-C Programming Language said:Some classes declare static variables and provide class methods to manage them. (Declaring a variable static in the same file as the class definition limits its scope to just the classand to just the part of the class thats implemented in the file. Unlike instance variables, static variables cant be inherited by subclasses.)
Static variables help give the class object more functionality than just that of a factory producing instances; it can approach being a complete and versatile object in its own right. A class object can be used to coordinate the instances it creates, dispense instances from lists of objects already created, or manage other processes essential to the application. In the case when you need only one object of a particular class, you can put all the objects state into static variables and use only class methods. This saves the step of allocating and initializing an instance.
Note:*It is also possible to use external variables that werent declared static, but the limited scope of static variables better serves the purpose of encapsulating data into separate objects.
Jordan72 said:Hey, I haven't used the potential for a class object to contain variables, but there seems to be no limitations, Apple says it better than I can:
mrichmon said:Note that the text from Apple only applies to Objective-C. Many languages (including Smalltalk if I recall correctly) and certainly Java and C++ have sub-classes inheriting both class/static methods and class/static fields.
scan said:is there some general rule of thumb when to write class methods. I don't quite understnad why adn when they are used.