1 / 23

Classes and Objects in Objective-C

Classes and Objects in Objective-C. FA 172 Intro to Mobile App Development. Agenda. Object-oriented programming OOP in Objective-C Classes Instances and objects Properties and methods. Definition of a program, revisited.

lok
Download Presentation

Classes and Objects in Objective-C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Objects inObjective-C FA 172 Intro to Mobile App Development

  2. Agenda • Object-oriented programming • OOP in Objective-C • Classes • Instances and objects • Properties and methods

  3. Definition of a program, revisited • Traditional definition of a program: sequence of instructions to be executed on a computer • Under the object-oriented programming (OOP) paradigm: a program, when it executes, is a collection of interacting objects • Programming in the OOP paradigm means specifying what data are in these objects and how these objects behave

  4. So… what is an object? • An object is a thing that has type, identity, state, and behavior • Type: it belongs to a class of similar things • Identity: it is an instance distinct from other objects • State: it has a set of properties that take on values • Behavior: it can act or carry out methods

  5. Object examples • Light Bulb • state? • behavior? • Car • state? • behavior? • Bank Account • state? • behavior?

  6. Class: Light Bulb • State • lit or not (on or off) • Behavior • turn on • turn off • check whether lit

  7. Class: Bank Account • State • balance • Behavior • deposit • withdraw • inquire balance

  8. Class: Car • State • distance travelled • gas left • Behavior • drive • load gas • check gas level • check odometer

  9. Objective-C:interface versus implementation • .h file contains an interface declaring properties and methods of a class • the interface is the public façade of an object • .m file contains an implementation of the class • code for the methods plus other “private” data • the implementation contains details encapsulated within the object, hidden from users • To create files in Xcode while project is open,File->New->File, then choose Obective-C class

  10. Interface for the Car class (Car.h) #import <Foundation/Foundation.h> @interface Car : NSObject @property double gasLeft; @property intdistanceTravelled; -(void) driveDistance:(int) dist; -(void) loadGas:(double) gas; @end

  11. Implementation (Car.m) #import "Car.h" @implementation Car -(void) driveDistance:(int) dist { self.distanceTravelled =self.distanceTravelled + dist; double gasUsed = dist/8.5; self.gasLeft = self.gasLeft - gasUsed; } -(void) loadGas:(double) gas { self.gasLeft = self.gasLeft + gas; } @end

  12. Using the Car class #import "Car.h" ... NSLog(@"Test code for a car object"); Car *myCar = [Car alloc]; [myCar loadGas:50.0]; [myCar driveDistance:10]; NSLog(@"gas: %6.2f, distance: %d",myCar.gasLeft, myCar.distanceTravelled); [myCar driveDistance:94]; [myCar loadGas:10.0]; NSLog(@"gas: %6.2f, distance: %d",myCar.gasLeft, myCar.distanceTravelled);

  13. Encapsulation and direct data update • Oftentimes, it is appropriate for properties to be “readonly” and updated only through appropriate methods • Example: • gasLeft property should be updated only as a result of driveDistance or loadGas • Try adding: myCar.gasLeft = 100.0; at the end of the code, and then print myCar.gasLeft

  14. Solution: specify property attributes in .h and .m files • In .h file, replace property declarations with: • @property(readonly) double gasLeft;@property(readonly) intdistanceTravelled; • In .m file, add the following after #import line: • @interface Car()@property(readwrite) double gasLeft;@property(readwrite) intdistanceTravelled;@end • Notice that direct update of properties are no longer allowed

  15. Syntax • Property declaration • @property(<attribute>,…) <type> <name>; • Method declaration (no arguments) • -(<type>)<name>; • Method declaration (one argument) • -(<type>)<name>:(<type>)<name>;

  16. Syntax • Object creation/instantiation • <var-name> = [<class-name> alloc]; • Referring to a property of an object • From within the class: self.<property-name> • For an object variable: <var-name>.<property-name> • Invoking methods: • [<var-name> <method-name>]; • [<var-name> <method-name>:<expression>];

  17. Naming conventions • Variable names and method names • Camel case: begin with small letter, capitalize first letters of succeeding words • Examples: distanceTravelled, myCar, loadGas • Class names • Capitalize first letters of all words within the name • Examples: BankAccount, Car, LightBulb

  18. void • The methods loadGas and driveDistance have the following signatures • -(void)driveDistance:(int) dist • -(void)loadGas:(double) gas • Here, void means “no return value” • Some methods return a value • Example: add a method to the Car class with the following signature • -(double) distanceTravelledInMiles

  19. Returning a value from a method • In Car.h: • -(double) distanceTravelledInMiles; • In Car.m • -(double) distanceTravelledInMiles{ double miles = self.distanceTravelled*0.62; return miles;} • In your test code, • NSLog(@”my car travelled %6.2 miles”, [myCardistanceTravelledInMiles]);

  20. Initialization • It is common to provide initialization code intended for a newly created object • Set initial values for properties • By convention, these methods should begin with the word init • Also by convention (and for reasons too technical to discuss at this point),use _<propertyname> (e.g., _gasLeft)instead of self.<propertyname> (e.g. self.gasLeft),when referring to the properties

  21. init method examples -(id) init { self = [super init]; _gasLeft = 0; _distanceTravelled = 0; return self; } -(id) initWithGas:(double) amt { self = [super init]; _gasLeft = amt; _distanceTravelled = 0; return self; }

  22. Implementing initialization methods • Make sure to place method declarationsof init and initWithGas in Car.h • Method implementations should be in Car.m • In your test code, add the following: • Car *car2 = [[Car alloc] init];Car *car3 = [[Car alloc] initWithGas:20.0];[car3 driveDistance:15];// some code to print gas levels of car2 & car3

  23. Summary • Introduction to Object-Oriented Programming • Objective-C class creation in Xcode(Car class: Car.h, Car.m) • OOP concepts tackled • classes, objects, properties, methods, initialization • Naming conventions

More Related