Class is a blueprint for instances.
Instance: a specific allocation of a class
Method: a function that an object knows how to perform
Instance Variable(or ''ivar"): a specific piece of data belonging to an object.
Encapsulation: keep implementation private and separate from interface
Polymorphism: different objects, same interface
Inheritance: hierarchical organization, share code, customize or extend behaviors.
Objective-C
Strict superset of C
- Mix C with ObjC
- Or even C++ with ObjC (usually referrend to as ObjC++)
A very simple language, but some new syntax
Single inheritance, classes inherit from one and only one superclass
Protocols define behavior that cross classes
Dynamic runtime
Loosely typed, if you'd like
Classes and Objects
- Classes declare state and behavior
- State (data) is maintained using instance variables
- Behavior is implemented using methods
- Instance variables typically hidden Accessible only using getter/setter methods
Dot Syntax
convenient shorthand for invoking accessor methods
float height = [person height];
foat height = person.height;
[person setHeight:newHeight];
person.height = newHeight;
[[person child] setHeight:newHeight];
// exactly the same as
person.child.height = newHeight;
Dynamic and static typing
- Dynamically-typed object
id anObject
- Statically-typed object
Person *anObject
Objects represented in format strings using %@
学员评论