object-c demo例子

本文介绍了一个简单的Objective-C程序实例,展示了如何定义和使用自定义类Fraction,并通过编译脚本简化了编译过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先写一个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研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值