01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Accessing existing instance variables<br />

129<br />

Listing 6.5<br />

Creating the init method for the Teacher class<br />

- (id)initWithName:(NSString *)_name<br />

age:(NSNumber *)_age<br />

gender:(Gender)_gender<br />

classes:(NSArray *)_classes<br />

salary:(NSNumber *)_salary<br />

areaOfExpertise:(NSString *)_areaOfExpertise {<br />

if ((self = [super init])) {<br />

name = _name;<br />

age = _age;<br />

gender = _gender;<br />

classes = _classes;<br />

salary = _salary;<br />

areaOfExpertise = _areaOfExpertise;<br />

}<br />

}<br />

return self;<br />

Now you create a similar method for the Student class. The Student initializer takes<br />

the name, age, and gender parameters because it’s a subclass of Person as well, but it<br />

takes slightly different parameters than the Teacher class. Overall, the method is similar<br />

to the Teacher init method, as the following listing shows.<br />

Listing 6.6<br />

Creating the init method for the Student class<br />

- (id)initWithName:(NSString *)_name<br />

age:(NSNumber *)_age<br />

gender:(Gender)_gender<br />

classes:(NSArray *)_classes<br />

numberOfCredits:(NSNumber *)_numberOfCredits<br />

major:(NSString *)_major {<br />

if ((self = [super init])) {<br />

name = _name;<br />

age = _age;<br />

gender = _gender;<br />

classes = _classes;<br />

numberOfCredits = _numberOfCredits;<br />

major = _major;<br />

}<br />

}<br />

return self;<br />

Now that you have all of these methods filled in, you can create Person, Teacher, and<br />

Student objects. Before you start using these classes, let’s look into expanding the<br />

ways you can access all of the attributes of these classes.<br />

6.3 Accessing existing instance variables<br />

You now have these three classes, each with instance variables. You created some initialization<br />

methods to fill in the instance variables, but what do you do if you create a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!