A unique occurrence of a class is an instance, and the actions that are performed on the instance are called methods. In some cases, a method can be applied to an instance of the class or to the class itself. For example, washing you car applies to an instance (in fact, all the methods listed in Table 3.1 would be considered instance methods). Finding out how many types of cars a manufacturer makes would apply to the class, so it would be a class method. Suppose you have two cars that came off the assembly line that are seemingly identical: They both have the same interior, same paint color, and so on. They might start out the same, but as each car is by its respective owner, it acquires its own unique characteristics. For example, one car might end up with a scratch on it and the other might have more miles on it. Each instance or object contains not only information about its initial characteristics it acquired from the factory, but also its current characteristics. Those characteristics can change dynamically. As you drive your car, the gas tank becomes depleted, the car gets dirtier, and the tires get a little more worn.
Applying a method to an object can affect the state of that object. If your method is to "fill up my car with gas," after that method is performed your car's gas tank will be full. The method will have affected the state of the car's gas tank.
The key concepts here are that objects are unique representations from a class, and each object contains some information (data) that is typically private to that object. The methods provide the means of accessing and changing that data.
The Objective-C programming language has the following particular syntax for applying methods to classes and instances:
[ ClassOrInstance method ];
In this syntax, a left bracket is followed by the name of a class or instance of that class, which is followed by one or more spaces, which is followed by the method you want to perform. Finally, it is closed off with a right bracket and a terminating semicolon. When you ask a class or an instance to perform some action, you say that you are sending it a message; the recipient of that message is called the receiver. So, another way to look at the general format described previously is as follows:
[ receiver message ] ;
Let's go back to the previous list and write everything in this new syntax. Before you do that, though, you need to get your new car. Go to the factory for that, like so:
yourCar = [Car new]; get a new car
You send a message to the Car class (the receiver of the message) asking it to give you a new car. The resulting object (which represents your unique car), is then stored in the variable yourCar. From now on, yourCar can be used to refer to your instance of the car, which you got from the factory.
Because you went to the factory to get the car, the method new is called a factory or class method. The rest of the actions on your new car will be instance methods because they apply to your car. Here are some sample message expressions you might write for your car:
[yourCar prep]; get it ready for first-time use [yourCar drive]; drive your car [yourCar wash]; wash your car [yourCar getGas]; put gas in your car if you need it [yourCar service]; service your car [yourCar topDown]; if it's a convertible [yourCar topUp];
Your sister Sue can use the same methods for her own instance of a car:
[suesCar drive]; [suesCar wash]; [suesCar getGas];
This is one of the key concepts behind object-oriented programming (that is, applying the same methods to different objects), and you'll learn more about that later.
You probably won't need to work with cars in your programs. Your objects are likely to be computer-oriented things, such as windows, rectangles, pieces of text, or maybe even a calculator or a playlist of songs. And just like the methods used for your cars, your methods might look similar, as in the following:
[myWindow erase]; |
Clear the window |
[myRect getArea]; |
Calculate the area of the rectangle |
[userText spellCheck]; |
Spell check some text |
[deskCalculator clearEntry]; |
Clear the last entry |
[favoritePlaylist showSongs]; |
Show the songs in a playlist of favorites |