入门Objective-C语言需要适应

来源:互联网 发布:王自如 知乎 编辑:程序博客网 时间:2024/06/02 02:47
#import <Foundation/Foundation.h>@interface Foo : NSObject //类的声明所有类都继承自NSObject{}-(int) f:(int)x;-(float) g:(int)x :(int)y;  //参数的标签名称可以省略,但扔要有冒号@end@implementation Foo //类的实现-(int) f:(int)x{    NSLog(@"x is %d",x);    return 1;}-(float) g:(int)x :(int)y{    NSLog(@"x is %d , y is %d",x, y);    return 1.0;}@end@interface Foo2 : NSObject{}-(int) add:(int)v1 :(int)v2;@end@implementation Foo2-(int) add:(int)v1 :(int)v2{    return v1+v2;}  @endint main (int argc, const char * argv[]){   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //ARC   NSLog (@"hello world");//输出字符串      Foo *lu = [[Foo alloc] init]; //初始化构造一个对象   int ha = 4, he = 10;   [lu f:ha]; //调用对象的方法   [lu g:he :ha];      Foo2 *an = [[Foo2 alloc] init];   NSLog(@"%d", [an add:he :ha]); //直接输出对象方法的返回值      [pool drain]; //释放ARC   return 0;}

0 0
原创粉丝点击