Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

29
Objective-C 1 CS151 Presentation: CS151 Presentation: Objective C Objective C Kai Tai Pang William Sze

Transcript of Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Page 1: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 1

CS151 Presentation:CS151 Presentation:Objective CObjective C

Kai Tai Pang

William Sze

Page 2: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 2

IntroductionIntroduction

Objective-C is implemented as set of extensions to the C language.

It's designed to give C a full capability for object-oriented programming, and to do so in a simple and straightforward way.

Its additions to C are few and are mostly based on Smalltalk, one of the first object-oriented programming languages.

Page 3: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 3

Why Objective CWhy Objective C

Objective-C incorporates C, you get all the benefits of C when working within Objective-C.

You can choose when to do something in an object-oriented way (define a new class, for example) and when to stick to procedural programming techniques (define a structure and some functions instead of a class).

Objective-C is a simple language. Its syntax is small, unambiguous, and easy to learn

Objective-C is the most dynamic of the object-oriented languages based on C. Most decisions are made at run time

Page 4: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 4

Object-Oriented ProgrammingObject-Oriented Programming

The insight of object-oriented programming is to combine state and behavior--data and operations on data--in a high-level unit, an object, and to give it language support.

An object is a group of related functions and a data structure that serves those functions. The functions are known as the object's methods, and the fields of its data structure are its instance variables.

Page 5: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 5

TheThe Objective-CObjective-C LanguageLanguage

The Objective-C language is fully compatible with ANSI standard C

Objective-C can also be used as an extension to C++.

Although C++ itself is a Object-Oriented Language, there are difference in the dynamic binding from Objective-C

Page 6: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 6

Objective-C Language (cont.)Objective-C Language (cont.)

Objective-C source files by a ``.m'' extension

“.h” file is the interface fileFor example:

– main.m – List.h (Interface of List class.) – List.m (Implementation of List class.)

Page 7: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 7

IDID

id is a data type used by Objective-C to define a pointer of an object (a pointer to the object’s data)

Any type of object, as long as it is an object, we can use the id data type.

For example, we can define an object by:

id anObject;nil is the reserved word for null object

Page 8: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 8

Dynamic TypingDynamic Typing

id data type has no information about the object Every object carries with it an isa instance

variable that identifies the object's class--what kind of object it is

Objects are thus dynamically typed at run time. Whenever it needs to, the run-time system can find the exact class that an object belongs to, just by asking the object

Page 9: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 9

MessagesMessages

To get an object to do something, you send it a message telling it to apply a method. In Objective-C, message expressions are enclosed in square brackets

[receiver message]The receiver is an object. The message is

simply the name of a method and any arguments that are passed to it

Page 10: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 10

Messages (cont.)Messages (cont.)

For example, this message tells the myRect object to perform its display method, which causes the rectangle to display itself

[myRect display];

[myRect setOrigin:30.0 :50.0]; The method setOrigin::, has two colons, one for

each of its arguments. The arguments are inserted after the colons, breaking the name apart

Page 11: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 11

PolymorphismPolymorphism

Each object has define its own method but for different class, they can have the same method name which has totally different meaning

The two different object can respond differently to the same message

Together with dynamic binding, it permits you to write code that might apply to any number of different kinds of objects, without your having to choose at the time you write the code what kinds of objects they might be

Page 12: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 12

InheritanceInheritance

Root class is typically NSObject

Inheritance is cumulative. A Square object has the methods and instance variables defined for Rectangle, Shape, Graphic, and NSObject, as well as those defined specifically for Square

Page 13: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 13

Inheritance (cont.)Inheritance (cont.)

Instance Variables: The new object contains not only the instance variables that were defined for its class, but also the instance variables defined for its superclass, all the way back to the root class

Methods: An object has access not only to the methods that were defined for its class, but also to methods defined for its superclass

Method Overriding: Implement a new method with the same name as one defined in a class farther up the hierarchy. The new method overrides the original; instances of the new class will perform it rather than the original

Page 14: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 14

Class ObjectsClass Objects

Compiler creates one class object to contain the information for the name of class and superclass

To start an object in a class:

id myRectx; myRect = [[Rectangle alloc] init]; The alloc method returns a new

instance and that instance performs an init method to set its initial state.

Page 15: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 15

Defining a ClassDefining a Class

In Objective-C, classes are defined in two parts: – An interface that declares the methods and

instance variables of the class and names its superclass

– An implementation that actually defines the class (contains the code that implements its methods)

Page 16: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 16

The InterfaceThe Interface

The declaration of a class interface begins with the compiler directive @interface and ends with the directive @end

@interface ClassName : ItsSuperclass { instance variable declarations

} method declarations

@end

Page 17: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 17

DeclarationDeclaration

Instance Variablesfloat width;float height; BOOL filled; NSColor *fillColor;

Methods: names of methods that can be used by class

objects, class methods, are preceded by a plus sign+ alloc

methods that instances of a class can use, instance methods, are marked with a minus sign: - (void) display;

Page 18: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 18

Declaration (cont.)Declaration (cont.)

Importing the Interface: The interface is usually included with the #import directive

#import "Rectangle.h" To reflect the fact that a class definition

builds on the definitions of inherited classes, an interface file begins by importing the interface for its superclass

Referring to Other Classes: If the interface mentions classes not in this hierarchy, it must declare them with the @class directive:

@class Rectangle, Circle;

Page 19: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 19

The ImplementationThe Implementation#import "ClassName.h" @implementation ClassName

method definitions @end

- makeIdenticalTwin {

if ( !twin ) { twin = [[Sibling alloc] init]; twin->gender = gender; twin->appearance = appearance; } return twin;

}

Page 20: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 20

Implementation (cont.)Implementation (cont.)

Example:@interface Worker : NSObject {

char *name; @private

int age; char *evaluation;

@protected id job; float wage;

@public id boss;

}

Page 21: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 21

Implementation (cont.)Implementation (cont.)

- promoteTo:newPosition

{

id old = job;

job = newPosition;

return old;

}

Page 22: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 22

Compile Objective-CCompile Objective-C Objective-C code can be compiled using the GNU C compiler gcc For instance, to compile List.m, use the following command:

gcc -c -Wno-import List.m The -c switch tells the compiler to produce an object

file, List.o, which can then later be linked into your program.

Link all of the implementations of your classes using gcc again. For example, to compile the files List.o, and main.o you could use the following command:

gcc -o prog -Wno-import List.o main.o -lobjc The -o prog tells gcc to create an executable

program with the name prog

Page 23: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 23

SummerySummery

It's designed to give C a full capability for object-oriented programming

Objective-C source files by a ``.m'' extension

“.h” file is the interface fileMost of the binding decision in Objective-C

can be made in run-time.

Page 24: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 24

ReferenceReference

Object-Oriented Programming in Objective-C

http://www.cs.indiana.edu/classes/c304/oop-intro.html Original Objective C Notes by Gerrit Huizenga

ftp://ftp.cs.tu-berlin.de/pub/

NeXT/documents/

developer/objective-c/OldObjC.ps.gz

http://www.cs.indiana.edu/classes/c304/ObjC.html

Page 25: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 25

Appendix: Sample ProgramAppendix: Sample Program// main.m

#import <objc/Object.h>

#import "List.h" // Note the new commenting style.

main() {

id list; // id is a new data type for objects.

list = [List new]; // create an instance of class List.

[list addEntry: 5]; // send a message to the object [list print];

[list addEntry: 6];

[list addEntry: 3];

[list print];

[list free]; // get rid of object

}

Page 26: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 26

#import <objc/Object.h>

// List.h - List is a subclass of the superclass Object

@interface List : Object

{

int list[100]; // These are instance variables.

int size;

}

/* Public methods */

- free;

- (int) addEntry: (int) num;

- print;

/* Private methods */

/* Other programs should not use these methods. */

- resetSize;

@end

Page 27: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 27

#import "List.h"

@implementation List

+ new // factory method

{

self = [super new];

[self resetSize];

return self;

}

- free

{

return [super free];

}

Page 28: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 28

- (int) addEntry: (int) num

{

list[size++] = num;

return size;

}

- print

{

int i;

printf("\n");

for (i = 0; i < size; ++i)

printf ("%i ", list[i]);

return self; // Always return self

// if nothing else makes sense.

}

Page 29: Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.

Objective-C 29

- resetSize

{

size = 0;

return self;

}