17
Your First Class

So far, you have only used classes created by Apple. Now you get to write your own class. Remember that a class describes objects in two ways:

  • methods (both instance methods and class methods) implemented by the class

  • instance variables within each instance of the class

You’re going to write a Person class, similar to the struct Person you wrote in Chapter 10. Your class will be defined in two files: Person.h and Person.m. Person.h, known as the header or interface file, will contain the declarations of instance variables and methods. Person.m is known as the implementation file. This is where you write out the steps for, or implement, each method.

Create a new project: a Foundation Command Line Tool named BMITime.

To create a new class, select FileNewNew File.... From the Mac OS X section on the left, select Cocoa and choose the Objective-C class template. Name your class Person and make it a subclass of NSObject. Finally, make sure the BMITime target is checked and click Save.

Notice that the new class files, Person.h and Person.m, now appear in the project navigator. Open Person.h. Declare two instance variables and three instance methods:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

/​/​ ​T​h​e​ ​c​l​a​s​s​ ​P​e​r​s​o​n​ ​i​n​h​e​r​i​t​s​ ​a​l​l​ ​t​h​e​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​
/​/​ ​a​n​d​ ​m​e​t​h​o​d​s​ ​d​e​f​i​n​e​d​ ​b​y​ ​t​h​e​ ​c​l​a​s​s​ ​N​S​O​b​j​e​c​t​
@​i​n​t​e​r​f​a​c​e​ ​P​e​r​s​o​n​ ​:​ ​N​S​O​b​j​e​c​t​
{​
 ​ ​ ​ ​/​/​ ​I​t​ ​h​a​s​ ​t​w​o​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​
 ​ ​ ​ ​f​l​o​a​t​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;​
 ​ ​ ​ ​i​n​t​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​;​
}​

/​/​ ​Y​o​u​ ​w​i​l​l​ ​b​e​ ​a​b​l​e​ ​t​o​ ​s​e​t​ ​t​h​o​s​e​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​ ​u​s​i​n​g​ ​t​h​e​s​e​ ​m​e​t​h​o​d​s​
-​ ​(​v​o​i​d​)​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​(​f​l​o​a​t​)​h​;​
-​ ​(​v​o​i​d​)​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​(​i​n​t​)​w​;​

/​/​ ​T​h​i​s​ ​m​e​t​h​o​d​ ​c​a​l​c​u​l​a​t​e​s​ ​t​h​e​ ​B​o​d​y​ ​M​a​s​s​ ​I​n​d​e​x​
-​ ​(​f​l​o​a​t​)​b​o​d​y​M​a​s​s​I​n​d​e​x​;​
@​e​n​d​

Notice that you declared the instance variables inside of curly braces and you declared the methods after the variables and outside of the curly braces.

To the compiler, this code says I am defining a new class called Person that has all the methods and instance variables of the class NSObject. Furthermore, I’m adding in two new instance variables: a float called heightInMeters and an int called weightInKilos. Also, I’m going to add three instance methods, which will be implemented in Person.m.

Notice that setHeightInMeters: expects a float argument and does not return a value, setWeightInKilos: expects an int argument and does not return a value, and bodyMassIndex takes no arguments and returns a float.

Now open Person.m. Delete any existing methods and implement the methods you declared:

#​i​m​p​o​r​t​ ​"​P​e​r​s​o​n​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​P​e​r​s​o​n​

-​ ​(​v​o​i​d​)​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​(​f​l​o​a​t​)​h​
{​
 ​ ​ ​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​=​ ​h​;​
}​
-​ ​(​v​o​i​d​)​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​(​i​n​t​)​w​
{​
 ​ ​ ​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​=​ ​w​;​
}​
-​ ​(​f​l​o​a​t​)​b​o​d​y​M​a​s​s​I​n​d​e​x​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​/​ ​(​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​*​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​)​;​
}​

@​e​n​d​

Note that Xcode imported Person.h for you. Also note that the names of the methods you implement must exactly match the ones you declared in the header file. In Xcode, this is easy; as you start typing a method in the implementation file, Xcode will suggest names of methods you’ve already declared.

Now that you’ve implemented all the methods you declared in Person.h, your class is complete. Edit main.m to exercise it a bit:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​
#​i​m​p​o​r​t​ ​"​P​e​r​s​o​n​.​h​"​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​i​n​s​t​a​n​c​e​ ​o​f​ ​P​e​r​s​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​P​e​r​s​o​n​ ​*​p​e​r​s​o​n​ ​=​ ​[​[​P​e​r​s​o​n​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​i​v​e​ ​t​h​e​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​ ​i​n​t​e​r​e​s​t​i​n​g​ ​v​a​l​u​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​9​6​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​1​.​8​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​a​l​l​ ​t​h​e​ ​b​o​d​y​M​a​s​s​I​n​d​e​x​ ​m​e​t​h​o​d​
 ​ ​ ​ ​ ​ ​ ​ ​f​l​o​a​t​ ​b​m​i​ ​=​ ​[​p​e​r​s​o​n​ ​b​o​d​y​M​a​s​s​I​n​d​e​x​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​p​e​r​s​o​n​ ​h​a​s​ ​a​ ​B​M​I​ ​o​f​ ​%​f​"​,​ ​b​m​i​)​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program. Notice that you imported Person.h so that the compiler will know how your methods are declared before they are used in main().

Figure 17.1  Object diagram for BMITime

Object diagram for BMITime

Accessor methods

Note that when we did this same exercise with structs instead of objects, we accessed the data members of the structure directly in main():

 ​ ​ ​ ​p​e​r​s​o​n​.​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​=​ ​9​6​;​
 ​ ​ ​ ​p​e​r​s​o​n​.​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​=​ ​1​.​8​;​

In object-oriented thinking, we do our best to keep the instance variables of an object private; that is, they are known and accessed only by the object itself. However, because we want to give methods and functions outside of Person the ability to set a person’s weight and height, we created the methods setWeightInKilos: and setHeightInMeters:. We call these setter methods.

A setter method allows other methods to set the value of an instance variable. A getter method allows other methods to read the value of an instance variable. Setter and getter methods are also known as accessor methods, or just accessors.

Add declarations for the getter methods to Person.h:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

@​i​n​t​e​r​f​a​c​e​ ​P​e​r​s​o​n​ ​:​ ​N​S​O​b​j​e​c​t​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;​
 ​ ​ ​ ​i​n​t​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​;​
}​

/​/​ ​Y​o​u​ ​w​i​l​l​ ​b​e​ ​a​b​l​e​ ​t​o​ ​s​e​t​ ​t​h​o​s​e​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​
-​ ​(​f​l​o​a​t​)​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;​
-​ ​(​v​o​i​d​)​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​(​f​l​o​a​t​)​h​;​
-​ ​(​i​n​t​)​w​e​i​g​h​t​I​n​K​i​l​o​s​;​
-​ ​(​v​o​i​d​)​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​(​i​n​t​)​w​;​

-​ ​(​f​l​o​a​t​)​b​o​d​y​M​a​s​s​I​n​d​e​x​;​

@​e​n​d​

You might wonder why the names of the getter methods don’t include get to match the setter method names. This is an Objective-C naming convention. The name of the method for reading an instance variable is simply the name of that instance variable.

Now return to Person.m and implement your getter methods:

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​P​e​r​s​o​n​

-​ ​(​f​l​o​a​t​)​h​e​i​g​h​t​I​n​M​e​t​e​r​s​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;​
}​

-​ ​(​v​o​i​d​)​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​(​f​l​o​a​t​)​h​
{​
 ​ ​ ​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​=​ ​h​;​
}​

-​ ​(​i​n​t​)​w​e​i​g​h​t​I​n​K​i​l​o​s​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​;​
}​

-​ ​(​v​o​i​d​)​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​(​i​n​t​)​w​
{​
 ​ ​ ​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​=​ ​w​;​
}​
-​ ​(​f​l​o​a​t​)​b​o​d​y​M​a​s​s​I​n​d​e​x​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​/​ ​(​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​*​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​)​;​
}​

@​e​n​d​

Finally, in main.m, use those methods:

#​i​m​p​o​r​t​ ​"​P​e​r​s​o​n​.​h​"​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​i​n​s​t​a​n​c​e​ ​o​f​ ​P​e​r​s​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​P​e​r​s​o​n​ ​*​p​e​r​s​o​n​ ​=​ ​[​[​P​e​r​s​o​n​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​i​v​e​ ​t​h​e​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​ ​i​n​t​e​r​e​s​t​i​n​g​ ​v​a​l​u​e​s​
 ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​9​6​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​1​.​8​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​a​l​l​ ​t​h​e​ ​b​o​d​y​M​a​s​s​I​n​d​e​x​ ​m​e​t​h​o​d​
 ​ ​ ​ ​ ​ ​ ​ ​f​l​o​a​t​ ​b​m​i​ ​=​ ​[​p​e​r​s​o​n​ ​b​o​d​y​M​a​s​s​I​n​d​e​x​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​p​e​r​s​o​n​ ​(​%​d​,​ ​%​f​)​ ​h​a​s​ ​a​ ​B​M​I​ ​o​f​ ​%​f​"​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​p​e​r​s​o​n​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​]​,​ ​[​p​e​r​s​o​n​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​]​,​ ​b​m​i​)​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset