首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

object-c demo事例

2012-09-16 
object-c demo例子先写一个hello.m#import Foundation/Foundation.h??@interface Fraction:NSObject{int

object-c demo例子

先写一个hello.m

#import <Foundation/Foundation.h>?

?

@interface Fraction:NSObject{

int number;

int ids;

}

?

-(void) print;

-(void) setNumber:(int)n;

-(void) setIds:(int)ids;

@end

?

@implementation Fraction

-(void) print{

NSLog(@"(x=%i,y=%i)",number,ids);

}

?

-(void) setNumber:(int) n{

number=n;

}

?

-(void) setIds:(int)ids{

ids=ids;

}

@end

?

int main (int argc, const char *argv[]) {?

NSLog(@"Hello World!\n");?

// printf("%s\n","hello,ducker");

Fraction *fract=[[Fraction alloc] init];

[fract setNumber : 10];

printf("(number=%i)",fract->number);

[fract release];

return 0;?

}

?

写一个编译脚本oc

$ cat oc

#!/bin/bash

?

gcc -o $1 $2 \

-fconstant-string-class=NSConstantString \

-I /GNUstep/System/Library/Headers/ \

-L /GNUstep/System/Library/Libraries/ \

-lobjc \

-lgnustep-base

?

编译:

./oc hello hello.m

?

然后执行:

./hello.exe

$ ./hello.exe

?

2012-04-09 18:26:02.500 hello[2500] Hello World!

(number=10)

?

稍后继续object-c研究

热点排行