- 浏览: 56946 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
luozhong915127:
呵呵额,有点功夫。
【贪吃蛇—Java程序员写Android游戏】系列 3. 用J2ME实现Android的Snake Sample详解 -
zhy4013:
我是刚刚接触安卓,我也不知道google-market_lic ...
Android SDK 2.3与Eclipse最新版开发环境搭建(一) -
deaboway:
zhy4013 写道LZ,我按照你说的方式手动下载,现在就go ...
Android SDK 2.3与Eclipse最新版开发环境搭建(一) -
zhy4013:
LZ,我按照你说的方式手动下载,现在就google-marke ...
Android SDK 2.3与Eclipse最新版开发环境搭建(一) -
qiushily2030:
应该是SDK没有正常的下载
Android SDK 2.3与Eclipse最新版开发环境搭建(一)
Objective-C 入门教程
大纲
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
printf( "hello world/n"
);
§
return
0;
}
hello world
§
#import <Foundation/NSObject.h>
§
§
@interface Fraction: NSObject {
§
int
numerator;
§
int
denominator;
§
}
§
§
-(void) print;
§
-(void) setNumerator: (int) d;
§
-(void) setDenominator: (int) d;
§
-(int) numerator;
§
-(int) denominator;
§
@end
§
#import "Fraction.h"
§
#import <stdio.h>
§
§
@implementation Fraction
§
-(void) print {
§
printf( "%i/%i",
numerator, denominator );
§
}
§
§
-(void) setNumerator: (int) n {
§
numerator =
n;
§
}
§
§
-(void) setDenominator: (int) d {
§
denominator =
d;
§
}
§
§
-(int) denominator {
§
return
denominator;
§
}
§
§
-(int) numerator {
§
return
numerator;
§
}
@end
§
#import <stdio.h>
§
#import "Fraction.h"
§
§
int main( int argc, const char *argv[] ) {
§
// create a new
instance
§
Fraction *frac =
[[Fraction alloc] init];
§
§
// set the
values
§
[frac setNumerator:
1];
§
[frac setDenominator:
3];
§
§
// print
it
§
printf( "The fraction is:
" );
§
[frac
print];
§
printf( "/n"
);
§
§
// free
memory
§
[frac
release];
§
§
return
0;
}
The fraction is: 1/3
§
...
§
-(void) setNumerator: (int) n andDenominator: (int)
d;
...
§
...
§
-(void) setNumerator: (int) n andDenominator: (int) d
{
§
numerator =
n;
§
denominator =
d;
§
}
...
§
#import <stdio.h>
§
#import "Fraction.h"
§
§
int main( int argc, const char *argv[] ) {
§
// create a new
instance
§
Fraction *frac =
[[Fraction alloc] init];
§
Fraction *frac2 =
[[Fraction alloc] init];
§
§
// set the
values
§
[frac setNumerator:
1];
§
[frac setDenominator:
3];
§
§
// combined
set
§
[frac2 setNumerator: 1
andDenominator: 5];
§
§
// print
it
§
printf( "The fraction is:
" );
§
[frac
print];
§
printf( "/n"
);
§
§
// print
it
§
printf( "Fraction 2 is: "
);
§
[frac2
print];
§
printf( "/n"
);
§
§
// free
memory
§
[frac
release];
§
[frac2 release];
§
§
return
0;
}
§
The fraction is: 1/3
Fraction 2 is: 1/5
§
...
§
-(Fraction*) initWithNumerator: (int) n denominator: (int)
d;
...
§
...
§
-(Fraction*) initWithNumerator: (int) n denominator: (int) d
{
§
self = [super
init];
§
§
if ( self )
{
§
[self setNumerator: n
andDenominator: d];
§
}
§
§
return
self;
§
}
...
§
#import <stdio.h>
§
#import "Fraction.h"
§
§
int main( int argc, const char *argv[] ) {
§
// create a new
instance
§
Fraction *frac =
[[Fraction alloc] init];
§
Fraction *frac2 =
[[Fraction alloc] init];
§
Fraction *frac3 =
[[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§
// set the
values
§
[frac setNumerator:
1];
§
[frac setDenominator:
3];
§
§
// combined
set
§
[frac2 setNumerator: 1
andDenominator: 5];
§
§
// print
it
§
printf( "The fraction is:
" );
§
[frac
print];
§
printf( "/n"
);
§
§
printf( "Fraction 2 is: "
);
§
[frac2
print];
§
printf( "/n"
);
§
§
printf( "Fraction 3 is: "
);
§
[frac3
print];
§
printf( "/n"
);
§
§
// free
memory
§
[frac
release];
§
[frac2
release];
§
[frac3
release];
§
§
return
0;
}
§
The fraction is: 1/3
§
Fraction 2 is: 1/5
Fraction 3 is: 3/10
§
#import <Foundation/NSObject.h>
§
§
@interface Access: NSObject {
§
@public
§
int
publicVar;
§
@private
§
int
privateVar;
§
int
privateVar2;
§
@protected
§
int
protectedVar;
§
}
@end
§
#import "Access.h"
§
§
@implementation Access
@end
§
#import "Access.h"
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
Access *a = [[Access
alloc] init];
§
§
//
works
§
a->publicVar =
5;
§
printf( "public var:
%i/n", a->publicVar );
§
§
// doesn't
compile
§
//a->privateVar =
10;
§
//printf( "private var:
%i/n", a->privateVar );
§
§
[a
release];
§
return
0;
}
public var: 5
§
#import <Foundation/NSObject.h>
§
§
static int count;
§
§
@interface ClassA: NSObject
§
+(int) initCount;
§
+(void) initialize;
@end
§
#import "ClassA.h"
§
§
@implementation ClassA
§
-(id) init {
§
self = [super
init];
§
count++;
§
return
self;
§
}
§
§
+(int) initCount {
§
return
count;
§
}
§
§
+(void) initialize {
§
count =
0;
§
}
@end
§
#import "ClassA.h"
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
ClassA *c1 = [[ClassA
alloc] init];
§
ClassA *c2 = [[ClassA
alloc] init];
§
§
// print
count
§
printf( "ClassA count:
%i/n", [ClassA initCount] );
§
§
ClassA *c3 = [[ClassA
alloc] init];
§
§
// print count
again
§
printf( "ClassA count:
%i/n", [ClassA initCount] );
§
§
[c1
release];
§
[c2
release];
§
[c3
release];
§
§
return
0;
}
§
ClassA count: 2
ClassA count: 3
§
#import <Foundation/NSException.h>
§
§
@interface CupWarningException: NSException
@end
§
#import "CupWarningException.h"
§
§
@implementation CupWarningException
@end
§
#import <Foundation/NSException.h>
§
§
@interface CupOverflowException: NSException
@end
§
#import "CupOverflowException.h"
§
§
@implementation CupOverflowException
@end
§
#import <Foundation/NSObject.h>
§
§
@interface Cup: NSObject {
§
int
level;
§
}
§
§
-(int) level;
§
-(void) setLevel: (int) l;
§
-(void) fill;
§
-(void) empty;
§
-(void) print;
@end
§
#import "Cup.h"
§
#import "CupOverflowException.h"
§
#import "CupWarningException.h"
§
#import <Foundation/NSException.h>
§
#import <Foundation/NSString.h>
§
§
@implementation Cup
§
-(id) init {
§
self = [super
init];
§
§
if ( self )
{
§
[self setLevel:
0];
§
}
§
§
return
self;
§
}
§
§
-(int) level {
§
return
level;
§
}
§
§
-(void) setLevel: (int) l {
§
level =
l;
§
§
if ( level > 100 )
{
§
// throw
overflow
§
NSException *e =
[CupOverflowException
§
exceptionWithName:
@"CupOverflowException"
§
reason: @"The
level is above 100"
§
userInfo:
nil];
§
@throw
e;
§
} else if ( level >= 50
) {
§
// throw
warning
§
NSException *e =
[CupWarningException
§
exceptionWithName:
@"CupWarningException"
§
reason: @"The
level is above or at 50"
§
userInfo:
nil];
§
@throw
e;
§
} else if ( level < 0 )
{
§
// throw
exception
§
NSException *e =
[NSException
§
exceptionWithName:
@"CupUnderflowException"
§
reason: @"The
level is below 0"
§
userInfo:
nil];
§
@throw
e;
§
}
§
}
§
§
-(void) fill {
§
[self setLevel: level +
10];
§
}
§
§
-(void) empty {
§
[self setLevel: level -
10];
§
}
§
§
-(void) print {
§
printf( "Cup level is:
%i/n", level );
§
}
@end
§
#import "Cup.h"
§
#import "CupOverflowException.h"
§
#import "CupWarningException.h"
§
#import <Foundation/NSString.h>
§
#import <Foundation/NSException.h>
§
#import <Foundation/NSAutoreleasePool.h>
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
NSAutoreleasePool *pool =
[[NSAutoreleasePool alloc] init];
§
Cup *cup = [[Cup alloc]
init];
§
int
i;
§
§
// this will work
§
for ( i = 0; i < 4; i++
) {
§
[cup
fill];
§
[cup
print];
§
}
§
§
// this will throw
exceptions
§
for ( i = 0; i < 7; i++
) {
§
@try
{
§
[cup
fill];
§
} @catch (
CupWarningException *e ) {
§
printf( "%s: ", [[e name] cString]
);
§
} @catch (
CupOverflowException *e ) {
§
printf( "%s: ",
[[e name] cString] );
§
} @finally
{
§
[cup
print];
§
}
§
}
§
§
// throw a generic
exception
§
@try
{
§
[cup setLevel: -1];
§
} @catch ( NSException *e
) {
§
printf( "%s: %s/n",
[[e name] cString], [[e reason] cString] );
§
}
§
§
// free memory
§
[cup
release];
§
[pool
release];
}
§
Cup level is: 10
§
Cup level is: 20
§
Cup level is: 30
§
Cup level is: 40
§
CupWarningException: Cup level is: 50
§
CupWarningException: Cup level is: 60
§
CupWarningException: Cup level is: 70
§
CupWarningException: Cup level is: 80
§
CupWarningException: Cup level is: 90
§
CupWarningException: Cup level is: 100
§
CupOverflowException: Cup level is: 110
CupUnderflowException: The level is below 0
§
#import <Foundation/NSObject.h>
§
§
@interface Fraction: NSObject {
§
int
numerator;
§
int
denominator;
§
}
§
§
-(Fraction*) initWithNumerator: (int) n denominator: (int)
d;
§
-(void) print;
§
-(void) setNumerator: (int) d;
§
-(void) setDenominator: (int) d;
§
-(void) setNumerator: (int) n andDenominator: (int)
d;
§
-(int) numerator;
§
-(int) denominator;
@end
§
#import "Fraction.h"
§
#import <stdio.h>
§
§
@implementation Fraction
§
-(Fraction*) initWithNumerator: (int) n denominator: (int) d
{
§
self = [super
init];
§
§
if ( self )
{
§
[self setNumerator: n
andDenominator: d];
§
}
§
§
return
self;
§
}
§
§
-(void) print {
§
printf( "%i / %i",
numerator, denominator );
§
}
§
§
-(void) setNumerator: (int) n {
§
numerator =
n;
§
}
§
§
-(void) setDenominator: (int) d {
§
denominator =
d;
§
}
§
§
-(void) setNumerator: (int) n andDenominator: (int) d
{
§
numerator =
n;
§
denominator =
d;
§
}
§
§
-(int) denominator {
§
return
denominator;
§
}
§
§
-(int) numerator {
§
return
numerator;
§
}
@end
§
#import <Foundation/NSObject.h>
§
§
@interface Complex: NSObject {
§
double
real;
§
double imaginary;
§
}
§
§
-(Complex*) initWithReal: (double) r andImaginary: (double)
i;
§
-(void) setReal: (double) r;
§
-(void) setImaginary: (double) i;
§
-(void) setReal: (double) r andImaginary: (double)
i;
§
-(double) real;
§
-(double) imaginary;
§
-(void) print;
§
@end
§
#import "Complex.h"
§
#import <stdio.h>
§
§
@implementation Complex
§
-(Complex*) initWithReal: (double) r andImaginary: (double) i
{
§
self = [super
init];
§
§
if ( self )
{
§
[self setReal: r
andImaginary: i];
§
}
§
§
return
self;
§
}
§
§
-(void) setReal: (double) r {
§
real =
r;
§
}
§
§
-(void) setImaginary: (double) i {
§
imaginary =
i;
§
}
§
§
-(void) setReal: (double) r andImaginary: (double) i
{
§
real =
r;
§
imaginary =
i;
§
}
§
§
-(double) real {
§
return
real;
§
}
§
§
-(double) imaginary {
§
return imaginary;
§
}
§
§
-(void) print {
§
printf( "%_f + %_fi",
real, imaginary );
§
}
§
@end
§
#import <stdio.h>
§
#import "Fraction.h"
§
#import "Complex.h"
§
§
int main( int argc, const char *argv[] ) {
§
// create a new
instance
§
Fraction *frac =
[[Fraction alloc] initWithNumerator: 1 denominator: 10];
§
Complex *comp = [[Complex
alloc] initWithReal: 10 andImaginary: 15];
§
id
number;
§
§
// print
fraction
§
number =
frac;
§
printf( "The fraction is:
" );
§
[number
print];
§
printf( "/n"
);
§
§
// print
complex
§
number =
comp;
§
printf( "The complex
number is: " );
§
[number
print];
§
printf( "/n"
);
§
§
// free
memory
§
[frac
release];
§
[comp
release];
§
§
return
0;
}
§
The fraction is: 1 / 10
The complex number is: 10.000000 + 15.000000i
§
#import <Foundation/NSObject.h>
§
§
@interface Rectangle: NSObject {
§
int
width;
§
int
height;
§
}
§
§
-(Rectangle*) initWithWidth: (int) w height: (int)
h;
§
-(void) setWidth: (int) w;
§
-(void) setHeight: (int) h;
§
-(void) setWidth: (int) w height: (int) h;
§
-(int) width;
§
-(int) height;
§
-(void) print;
@end
§
#import "Rectangle.h"
§
#import <stdio.h>
§
§
@implementation Rectangle
§
-(Rectangle*) initWithWidth: (int) w height: (int) h
{
§
self = [super
init];
§
§
if ( self )
{
§
[self setWidth: w height:
h];
§
}
§
§
return
self;
§
}
§
§
-(void) setWidth: (int) w {
§
width =
w;
§
}
§
§
-(void) setHeight: (int) h {
§
height =
h;
§
}
§
§
-(void) setWidth: (int) w height: (int) h {
§
width =
w;
§
height =
h;
§
}
§
§
-(int) width {
§
return
width;
§
}
§
§
-(int) height {
§
return
height;
§
}
§
§
-(void) print {
§
printf( "width = %i,
height = %i", width, height );
§
}
@end
§
#import "Rectangle.h"
§
§
@interface Square: Rectangle
§
-(Square*) initWithSize: (int) s;
§
-(void) setSize: (int) s;
§
-(int) size;
@end
§
#import "Square.h"
§
§
@implementation Square
§
-(Square*) initWithSize: (int) s {
§
self = [super
init];
§
§
if ( self )
{
§
[self setSize:
s];
§
}
§
§
return
self;
§
}
§
§
-(void) setSize: (int) s {
§
width =
s;
§
height =
s;
§
}
§
§
-(int) size {
§
return
width;
§
}
§
§
-(void) setWidth: (int) w {
§
[self setSize:
w];
§
}
§
§
-(void) setHeight: (int) h {
§
[self setSize:
h];
§
}
@end
§
#import "Square.h"
§
#import "Rectangle.h"
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
Rectangle *rec =
[[Rectangle alloc] initWithWidth: 10 height: 20];
§
Square *sq = [[Square
alloc] initWithSize: 15];
§
§
// print
em
§
printf( "Rectangle: "
);
§
[rec
print];
§
printf( "/n"
);
§
§
printf( "Square: "
);
§
[sq
print];
§
printf( "/n"
);
§
§
// update
square
§
[sq setWidth:
20];
§
printf( "Square after
change: " );
§
[sq
print];
§
printf( "/n"
);
§
§
// free
memory
§
[rec
release];
§
[sq
release];
§
§
return
0;
}
§
Rectangle: width = 10, height = 20
§
Square: width = 15, height = 15
Square after change: width = 20, height = 20
-(BOOL) isKindOfClass: classObj
is object a descendent or member of classObj -(BOOL) isMemberOfClass: classObj
is object a member of classObj -(BOOL) respondsToSelector: selector
does the object have a method named specifiec by the
selector +(BOOL) instancesRespondToSelector:
selector
does an object created by this class have the ability to respond to
the specified selector -(id) performSelector: selector
invoke the specified selector on the object §
#import "Square.h"
§
#import "Rectangle.h"
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
Rectangle *rec =
[[Rectangle alloc] initWithWidth: 10 height: 20];
§
Square *sq = [[Square
alloc] initWithSize: 15];
§
§
//
isMemberOfClass
§
§
// true
§
if ( [sq isMemberOfClass:
[Square class]] == YES ) {
§
printf( "square is a
member of square class/n" );
§
}
§
§
//
false
§
if ( [sq isMemberOfClass:
[Rectangle class]] == YES ) {
§
printf( "square is a
member of rectangle class/n" );
§
}
§
§
//
false
§
if ( [sq isMemberOfClass:
[NSObject class]] == YES ) {
§
printf( "square is a
member of object class/n" );
§
}
§
§
//
isKindOfClass
§
§
// true
§
if ( [sq isKindOfClass:
[Square class]] == YES ) {
§
printf( "square is a
kind of square class/n" );
§
}
§
§
//
true
§
if ( [sq isKindOfClass:
[Rectangle class]] == YES ) {
§
printf( "square is a
kind of rectangle class/n" );
§
}
§
§
//
true
§
if ( [sq isKindOfClass:
[NSObject class]] == YES ) {
§
printf( "square is a
kind of object class/n" );
§
}
§
§
//
respondsToSelector
§
§
//
true
§
if ( [sq
respondsToSelector: @selector( setSize: )] == YES ) {
§
printf( "square
responds to setSize: method/n" );
§
}
§
§
//
false
§
if ( [sq
respondsToSelector: @selector( nonExistant )] == YES ) {
§
printf( "square
responds to nonExistant method/n" );
§
}
§
§
//
true
§
if ( [Square
respondsToSelector: @selector( alloc )] == YES ) {
§
printf( "square class
responds to alloc method/n" );
§
}
§
§
//
instancesRespondToSelector
§
§
//
false
§
if ( [Rectangle
instancesRespondToSelector: @selector( setSize: )] == YES )
{
§
printf( "rectangle
instance responds to setSize: method/n" );
§
}
§
§
//
true
§
if ( [Square
instancesRespondToSelector: @selector( setSize: )] == YES )
{
§
printf( "square
instance responds to setSize: method/n" );
§
}
§
§
// free
memory
§
[rec
release];
§
[sq
release];
§
§
return
0;
}
§
square is a member of square class
§
square is a kind of square class
§
square is a kind of rectangle class
§
square is a kind of object class
§
square responds to setSize: method
§
square class responds to alloc method
square instance responds to setSize: method
§
#import "Fraction.h"
§
§
@interface Fraction (Math)
§
-(Fraction*) add: (Fraction*) f;
§
-(Fraction*) mul: (Fraction*) f;
§
-(Fraction*) div: (Fraction*) f;
§
-(Fraction*) sub: (Fraction*) f;
@end
§
#import "FractionMath.h"
§
§
@implementation Fraction (Math)
§
-(Fraction*) add: (Fraction*) f {
§
return [[Fraction alloc]
initWithNumerator: numerator * [f denominator] +
§
denominator
* [f numerator]
§
denominator:
denominator * [f denominator]];
§
}
§
§
-(Fraction*) mul: (Fraction*) f {
§
return [[Fraction alloc]
initWithNumerator: numerator * [f numerator]
§
denominator:
denominator * [f denominator]];
§
§
}
§
§
-(Fraction*) div: (Fraction*) f {
§
return [[Fraction alloc]
initWithNumerator: numerator * [f denominator]
§
denominator:
denominator * [f numerator]];
§
}
§
§
-(Fraction*) sub: (Fraction*) f {
§
return [[Fraction alloc]
initWithNumerator: numerator * [f denominator] -
§
denominator
* [f numerator]
§
denominator:
denominator * [f denominator]];
§
}
@end
§
#import <stdio.h>
§
#import "Fraction.h"
§
#import "FractionMath.h"
§
§
int main( int argc, const char *argv[] ) {
§
// create a new
instance
§
Fraction *frac1 =
[[Fraction alloc] initWithNumerator: 1 denominator: 3];
§
Fraction *frac2 =
[[Fraction alloc] initWithNumerator: 2 denominator: 5];
§
Fraction *frac3 = [frac1
mul: frac2];
§
§
// print
it
§
[frac1
print];
§
printf( " * "
);
§
[frac2
print];
§
printf( " = "
);
§
[frac3
print];
§
printf( "/n"
);
§
§
// free
memory
§
[frac1
release];
§
[frac2
release];
§
[frac3
release];
§
§
return
0;
}
1/3 * 2/5 = 2/15
§
#import <Foundation/NSObject.h>
§
§
@interface MyClass: NSObject
§
-(void) publicMethod;
@end
§
#import "MyClass.h"
§
#import <stdio.h>
§
§
@implementation MyClass
§
-(void) publicMethod {
§
printf( "public method/n"
);
§
}
§
@end
§
§
// private methods
§
@interface MyClass (Private)
§
-(void) privateMethod;
§
@end
§
§
@implementation MyClass (Private)
§
-(void) privateMethod {
§
printf( "private method/n"
);
§
}
@end
§
#import "MyClass.h"
§
§
int main( int argc, const char *argv[] ) {
§
MyClass *obj = [[MyClass
alloc] init];
§
§
// this
compiles
§
[obj
publicMethod];
§
§
// this throws errors when
compiling
§
//[obj
privateMethod];
§
§
// free
memory
§
[obj
release];
§
§
return
0;
}
public method
§
#import "Fraction.h"
§
§
@interface FractionB: Fraction
§
-(void) print;
§
@end
§
#import "FractionB.h"
§
#import <stdio.h>
§
§
@implementation FractionB
§
-(void) print {
§
printf( "(%i/%i)",
numerator, denominator );
§
}
@end
§
#import <stdio.h>
§
#import "Fraction.h"
§
#import "FractionB.h"
§
§
int main( int argc, const char *argv[] ) {
§
Fraction *frac =
[[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§
// print
it
§
printf( "The fraction is:
" );
§
[frac
print];
§
printf( "/n"
);
§
§
// make FractionB pose as
Fraction
§
[FractionB poseAsClass:
[Fraction class]];
§
§
Fraction *frac2 =
[[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§
// print
it
§
printf( "The fraction is:
" );
§
[frac2
print];
§
printf( "/n"
);
§
§
// free
memory
§
[frac
release];
§
[frac2
release];
§
§
return
0;
}
§
The fraction is: 3/10
The fraction is: (3/10)
§
@protocol Printing
§
-(void) print;
@end
§
#import <Foundation/NSObject.h>
§
#import "Printing.h"
§
§
@interface Fraction: NSObject <Printing, NSCopying>
{
§
int
numerator;
§
int
denominator;
§
}
§
§
-(Fraction*) initWithNumerator: (int) n denominator: (int)
d;
§
-(void) setNumerator: (int) d;
§
-(void) setDenominator: (int) d;
§
-(void) setNumerator: (int) n andDenominator: (int)
d;
§
-(int) numerator;
§
-(int) denominator;
@end
§
#import "Fraction.h"
§
#import <stdio.h>
§
§
@implementation Fraction
§
-(Fraction*) initWithNumerator: (int) n denominator: (int) d
{
§
self = [super
init];
§
§
if ( self )
{
§
[self setNumerator: n
andDenominator: d];
§
}
§
§
return
self;
§
}
§
§
-(void) print {
§
printf( "%i/%i",
numerator, denominator );
§
}
§
§
-(void) setNumerator: (int) n {
§
numerator =
n;
§
}
§
§
-(void) setDenominator: (int) d {
§
denominator =
d;
§
}
§
§
-(void) setNumerator: (int) n andDenominator: (int) d
{
§
numerator =
n;
§
denominator =
d;
§
}
§
§
-(int) denominator {
§
return
denominator;
§
}
§
§
-(int) numerator {
§
return
numerator;
§
}
§
§
-(Fraction*) copyWithZone: (NSZone*) zone {
§
return [[Fraction
allocWithZone: zone] initWithNumerator: numerator
§
denominator:
denominator];
§
}
@end
§
#import <Foundation/NSObject.h>
§
#import "Printing.h"
§
§
@interface Complex: NSObject <Printing> {
§
double
real;
§
double
imaginary;
§
}
§
§
-(Complex*) initWithReal: (double) r andImaginary: (double)
i;
§
-(void) setReal: (double) r;
§
-(void) setImaginary: (double) i;
§
-(void) setReal: (double) r andImaginary: (double)
i;
§
-(double) real;
§
-(double) imaginary;
@end
§
#import "Complex.h"
§
#import <stdio.h>
§
§
@implementation Complex
§
-(Complex*) initWithReal: (double) r andImaginary: (double) i
{
§
self = [super
init];
§
§
if ( self )
{
§
[self setReal: r
andImaginary: i];
§
}
§
§
return
self;
§
}
§
§
-(void) setReal: (double) r {
§
real =
r;
§
}
§
§
-(void) setImaginary: (double) i {
§
imaginary =
i;
§
}
§
§
-(void) setReal: (double) r andImaginary: (double) i
{
§
real =
r;
§
imaginary =
i;
§
}
§
§
-(double) real {
§
return
real;
§
}
§
§
-(double) imaginary {
§
return
imaginary;
§
}
§
§
-(void) print {
§
printf( "%_f + %_fi",
real, imaginary );
§
}
@end
§
#import <stdio.h>
§
#import "Fraction.h"
§
#import "Complex.h"
§
§
int main( int argc, const char *argv[] ) {
§
// create a new
instance
§
Fraction *frac =
[[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
Complex *comp = [[Complex
alloc] initWithReal: 5 andImaginary: 15];
§
id <Printing>
printable;
§
id <NSCopying,
Printing> copyPrintable;
§
§
// print
it
§
printable =
frac;
§
printf( "The fraction is:
" );
§
[printable
print];
§
printf( "/n"
);
§
§
// print
complex
§
printable =
comp;
§
printf( "The complex
number is: " );
§
[printable
print];
§
printf( "/n"
);
§
§
// this compiles because
Fraction comforms to both Printing and NSCopyable
§
copyPrintable =
frac;
§
§
// this doesn't compile
because Complex only conforms to Printing
§
//copyPrintable =
comp;
§
§
// test
conformance
§
§
//
true
§
if ( [frac
conformsToProtocol: @protocol( NSCopying )] == YES ) {
§
printf( "Fraction
conforms to NSCopying/n" );
§
}
§
§
//
false
§
if ( [comp
conformsToProtocol: @protocol( NSCopying )] == YES ) {
§
printf( "Complex
conforms to NSCopying/n" );
§
}
§
§
// free
memory
§
[frac
release];
§
[comp
release];
§
§
return
0;
}
§
The fraction is: 3/10
§
The complex number is: 5.000000 + 15.000000i
Fraction conforms to NSCopying
§
...
§
-(void) dealloc {
§
printf( "Deallocing
fraction/n" );
§
[super
dealloc];
§
}
...
§
#import "Fraction.h"
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
Fraction *frac1 =
[[Fraction alloc] init];
§
Fraction *frac2 =
[[Fraction alloc] init];
§
§
// print current
counts
§
printf( "Fraction 1 retain
count: %i/n", [frac1 retainCount] );
§
printf( "Fraction 2 retain
count: %i/n", [frac2 retainCount] );
§
§
// increment
them
§
[frac1 retain]; //
2
§
[frac1 retain]; //
3
§
[frac2 retain]; //
2
§
§
// print current
counts
§
printf( "Fraction 1 retain
count: %i/n", [frac1 retainCount] );
§
printf( "Fraction 2 retain
count: %i/n", [frac2 retainCount] );
§
§
// decrement
§
[frac1 release]; //
2
§
[frac2 release]; //
1
§
§
// print current
counts
§
printf( "Fraction 1 retain
count: %i/n", [frac1 retainCount] );
§
printf( "Fraction 2 retain
count: %i/n", [frac2 retainCount] );
§
§
// release them until they
dealloc themselves
§
[frac1 release]; //
1
§
[frac1 release]; //
0
§
[frac2 release]; //
0
}
§
Fraction 1 retain count: 1
§
Fraction 2 retain count: 1
§
Fraction 1 retain count: 3
§
Fraction 2 retain count: 2
§
Fraction 1 retain count: 2
§
Fraction 2 retain count: 1
§
Deallocing fraction
Deallocing fraction
§
#import <Foundation/NSObject.h>
§
#import <Foundation/NSString.h>
§
§
@interface AddressCard: NSObject {
§
NSString *first;
§
NSString
*last;
§
NSString
*email;
§
}
§
§
-(AddressCard*) initWithFirst: (NSString*) f
§
last:
(NSString*) l
§
email:
(NSString*) e;
§
-(NSString*) first;
§
-(NSString*) last;
§
-(NSString*) email;
§
-(void) setFirst: (NSString*) f;
§
-(void) setLast: (NSString*) l;
§
-(void) setEmail: (NSString*) e;
§
-(void) setFirst: (NSString*) f
§
last: (NSString*)
l
§
email: (NSString*)
e;
§
-(void) setFirst: (NSString*) f last: (NSString*)
l;
§
-(void) print;
@end
§
#import "AddressCard.h"
§
#import <stdio.h>
§
§
@implementation AddressCard
§
-(AddressCard*) initWithFirst: (NSString*) f
§
last:
(NSString*) l
§
email:
(NSString*) e {
§
self = [super
init];
§
§
if ( self )
{
§
[self setFirst: f
last: l email: e];
§
}
§
§
return
self;
§
}
§
§
-(NSString*) first {
§
return
first;
§
}
§
§
-(NSString*) last {
§
return
last;
§
}
§
§
-(NSString*) email {
§
return
email;
§
}
§
§
-(void) setFirst: (NSString*) f {
§
[f
retain];
§
[first
release];
§
first =
f;
§
}
§
§
-(void) setLast: (NSString*) l {
§
[l
retain];
§
[last
release];
§
last =
l;
§
}
§
§
-(void) setEmail: (NSString*) e {
§
[e
retain];
§
[email
release];
§
email =
e;
§
}
§
§
-(void) setFirst: (NSString*) f
§
last: (NSString*)
l
§
email: (NSString*) e
{
§
[self setFirst:
f];
§
[self setLast:
l];
§
[self setEmail:
e];
§
}
§
§
-(void) setFirst: (NSString*) f last: (NSString*) l
{
§
[self setFirst:
f];
§
[self setLast:
l];
§
}
§
§
-(void) print {
§
printf( "%s %s
<%s>", [first cString],
§
[last
cString],
§
[email cString]
);
§
}
§
§
-(void) dealloc {
§
[first
release];
§
[last
release];
§
[email
release];
§
§
[super
dealloc];
§
}
@end
§
#import "AddressCard.h"
§
#import <Foundation/NSString.h>
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
NSString *first
=[[NSString alloc] initWithCString: "Tom"];
§
NSString *last =
[[NSString alloc] initWithCString: "Jones"];
§
NSString *email =
[[NSString alloc] initWithCString: "tom@jones.com"];
§
AddressCard *tom =
[[AddressCard alloc] initWithFirst: first
§
last:
last
§
email:
email];
§
§
// we're done with the
strings, so we must dealloc them
§
[first
release];
§
[last
release];
§
[email
release];
§
§
// print to show the
retain count
§
printf( "Retain count:
%i/n", [[tom first] retainCount] );
§
[tom
print];
§
printf( "/n"
);
§
§
// free
memory
§
[tom
release];
§
§
return
0;
}
§
Retain count: 1
Tom Jones <tom@jones.com>
§
#import <Foundation/NSString.h>
§
#import <Foundation/NSAutoreleasePool.h>
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
NSAutoreleasePool *pool =
[[NSAutoreleasePool alloc] init];
§
NSString *str1 =
@"constant string";
§
NSString *str2 = [NSString
stringWithString: @"string managed by the pool"];
§
NSString *str3 =
[[NSString alloc] initWithString: @"self managed string"];
§
§
// print the
strings
§
printf( "%s retain count:
%x/n", [str1 cString], [str1 retainCount] );
§
printf( "%s retain count:
%x/n", [str2 cString], [str2 retainCount] );
§
printf( "%s retain count:
%x/n", [str3 cString], [str3 retainCount] );
§
§
// free
memory
§
[str3
release];
§
§
// free
pool
§
[pool
release];
§
return
0;
}
§
constant string retain count: ffffffff
§
string managed by the pool retain count: 1
self managed string retain count: 1
§
...
§
+(Fraction*) fractionWithNumerator: (int) n denominator: (int)
d;
§
...
§
...
§
+(Fraction*) fractionWithNumerator: (int) n denominator: (int) d
{
§
Fraction *ret = [[Fraction
alloc] initWithNumerator: n denominator: d];
§
[ret
autorelease];
§
§
return
ret;
§
}
...
§
#import <Foundation/NSAutoreleasePool.h>
§
#import "Fraction.h"
§
#import <stdio.h>
§
§
int main( int argc, const char *argv[] ) {
§
NSAutoreleasePool *pool =
[[NSAutoreleasePool alloc] init];
§
Fraction *frac1 =
[Fraction fractionWithNumerator: 2 denominator: 5];
§
Fraction *frac2 =
[Fraction fractionWithNumerator: 1 denominator: 3];
§
§
// print frac
1
§
printf( "Fraction 1: "
);
§
[frac1
print];
§
printf( "/n"
);
§
§
// print frac
2
§
printf( "Fraction 2: "
);
§
[frac2
print];
§
printf( "/n"
);
§
§
// this causes a
segmentation fault
§
//[frac1
release];
§
§
// release the pool and
all objects in it
§
[pool
release];
§
return
0;
}
§
Fraction 1: 2/5
Fraction 2: 1/3
§
#import <Foundation/NSArray.h>
§
#import <Foundation/NSString.h>
§
#import <Foundation/NSAutoreleasePool.h>
§
#import <Foundation/NSEnumerator.h>
§
#import <stdio.h>
§
§
void print( NSArray *array ) {
§
NSEnumerator *enumerator =
[array objectEnumerator];
§
id
obj;
§
§
while ( obj = [enumerator
nextObject] ) {
§
printf( "%s/n", [[obj
description] cString] );
§
}
§
}
§
§
int main( int argc, const char *argv[] ) {
§
NSAutoreleasePool *pool =
[[NSAutoreleasePool alloc] init];
§
NSArray *arr = [[NSArray
alloc] initWithObjects:
§
@"Me",
@"Myself", @"I", nil];
§
NSMutableArray *mutable =
[[NSMutableArray alloc] init];
§
§
// enumerate over
items
§
printf( "----static
array/n" );
§
print( arr
);
§
§
// add
stuff
§
[mutable addObject:
@"One"];
§
[mutable addObject:
@"Two"];
§
[mutable
addObjectsFromArray: arr];
§
[mutable addObject:
@"Three"];
§
§
// print
em
§
printf( "----mutable
array/n" );
§
print( mutable
);
§
§
// sort then
print
§
printf( "----sorted
mutable array/n" );
§
[mutable
sortUsingSelector: @selector( caseInsensitiveCompare: )];
§
print( mutable
);
§
§
// free
memory
§
[arr
release];
§
[mutable
release];
§
[pool
release];
§
§
return
0;
}
§
----static array
§
Me
§
Myself
§
I
§
----mutable array
§
One
§
Two
§
Me
§
Myself
§
I
§
Three
§
----sorted mutable array
§
I
§
Me
§
Myself
§
One
§
Three
Two
§
#import <Foundation/NSString.h>
§
#import <Foundation/NSAutoreleasePool.h>
§
#import <Foundation/NSDictionary.h>
§
#import <Foundation/NSEnumerator.h>
§
#import <Foundation/Foundation.h<
§
#import <stdio.h>
§
§
void print( NSDictionary *map ) {
§
NSEnumerator *enumerator =
[map keyEnumerator];
§
id
key;
§
§
while ( key = [enumerator
nextObject] ) {
§
printf( "%s =>
%s/n",
§
[[key description]
cString],
§
[[[map
objectForKey: key] description] cString] );
§
}
§
}
§
§
int main( int argc, const char *argv[] ) {
§
NSAutoreleasePool *pool =
[[NSAutoreleasePool alloc] init];
§
NSDictionary *dictionary =
[[NSDictionary alloc] initWithObjectsAndKeys:
§
@"one", [NSNumber
numberWithInt: 1],
§
@"two", [NSNumber
numberWithInt: 2],
§
@"three", [NSNumber
numberWithInt: 3],
§
nil];
§
NSMutableDictionary
*mutable = [[NSMutableDictionary alloc] init];
§
§
// print
dictionary
§
printf( "----static
dictionary/n" );
§
print( dictionary
);
§
§
// add
objects
§
[mutable setObject: @"Tom"
forKey: @"tom@jones.com"];
§
[mutable setObject: @"Bob"
forKey: @"bob@dole.com" ];
§
§
// print mutable
dictionary
§
printf( "----mutable
dictionary/n" );
§
print( mutable
);
§
§
// free memory
§
[dictionary
release];
§
[mutable
release];
§
[pool
release];
§
§
return
0;
}
§
----static dictionary
§
1 => one
§
2 => two
§
3 => three
§
----mutable dictionary
§
bob@dole.com => Bob
tom@jones.com => Tom
Last modified: April 13, 2004.
此对象是否是 classObj
的子孙或一员
此对象是否是 classObj
的一员
此对象是否有叫做 selector
的
method
此对象是否是由有能力响应指定 selector
的对象所产生
唤起此对象的指定 selector
中文翻译:William Shih
(xamous)
,January 7, 2005
相关推荐
Objective-C语言入门教程&深入浅出,理论实践相结合 Objective-C语言入门教程&深入浅出,理论实践相结合 Objective-C语言入门教程&深入浅出,理论实践相结合 Objective-C语言入门教程&深入浅出,理论实践相结合 ...
2. HelloWorld程序:这是任何编程语言入门教程中的经典示例,用于展示如何输出最基本的文本信息。Objective-C中的HelloWorld程序通常包含导入Foundation框架、创建一个主函数以及打印输出。 3. 类(Classes):在...
综上所述,《Objective-C基础教程》作为一本介绍Objective-C语言及其在iOS开发中应用的专业书籍,不仅适合初学者入门学习,也对有经验的开发者有着重要的参考价值。通过深入学习本书,读者可以全面掌握Objective-C的...
### Objective-C 入门教程知识点概述 #### 一、Objective-C 文件类型与扩展名 - **.h 头文件**:此类文件包含了类、类型、函数及常量的声明。通常用于提供接口定义,以便其他模块引用。 - **.m 源代码文件**:这些...
### Objective-C入门教程:探索iOS开发的基石 #### 引言 Objective-C,作为苹果公司早期为Mac OS X和iOS操作系统开发的主要编程语言之一,其重要性不言而喻。尽管Swift的出现使得Objective-C逐渐退居二线,但在...
《Objective-C基础教程》是一本面向初学者的编程书籍,主要涵盖了Objective-C语言的基本概念、语法和编程实践,尤其适合那些想要踏入iOS应用开发领域的学习者。Objective-C是Apple公司开发的面向对象的编程语言,它...
Objective-C入门教程通常会强调语言的基础知识,包括Objective-C的基本语法、数据结构、面向对象编程的原则以及如何利用Objective-C进行iOS应用的开发。在教程中,开发者会被引导了解如何使用Xcode这一集成开发环境...
### 学习Objective-C:入门教程 #### Objective-C简介与特性 Objective-C是一种面向对象的编程语言,由ANSI C语言扩展而来,主要用于苹果公司的Mac OS X和iOS操作系统上应用程序的开发。它结合了C语言的高效性与...
Objective-C 入门教程.txt
本教程将通过一系列的PPT课件,引导你快速入门Objective-C,这些课件覆盖了从基础到高级的重要概念,是《iPhone与iPad开发实战》一书中关于语言学习的前半部分内容。 首先,我们从"第一讲 Objective-C概述"开始,这...
Objective-C入门系列ppt_1 没有比ppt更有内涵的了
艾悠乐校验,根据文件描述,可能是一个提供在线教育或技术分享的平台,它在这里提供了Objective-C的入门教程。这个教程是为那些对iOS开发感兴趣的初学者准备的,目的是帮助他们构建iOS应用,并且在这个过程中成为...
介绍objective-c的基础知识,帮助我们快速入门。
希望这个简单的Objective-C语言教程能够为你提供一个入门的起点。Objective-C是一种面向对象的编程语言,广泛应用于Mac OS和iOS开发。通过学习Objective-C的基本语法、类和对象、控制流程和方法等内容,你将能够编写...
《Objective-C基础教程(第二版)》英文版是一本针对Objective-C编程语言的入门指导书,旨在帮助读者掌握这门广泛用于苹果平台应用程序开发的语言。Objective-C作为C语言的超集,在OS X和iOS系统的应用开发中扮演着...
总的来说,Objective-C基础教程为初学者提供了一个全面的入门指导,从语言基础到面向对象编程思想,再到实际应用,都有详细的介绍。对于希望从事苹果平台开发的程序员来说,学习Objective-C是掌握iOS和macOS开发的...
Objective-C 基础教程第二版,高清扫描版,Objective-C入门必备!
《Learn Objective-C on the Mac》由Mark Dalrymple撰写,是一本旨在帮助开发者入门Objective-C的经典教程。 在本书中,读者将学习到以下核心知识点: 1. **Objective-C语法**:Objective-C的语法基于C语言,但...