52,对象方法

来源:互联网 发布:金山数据恢复多少钱 编辑:程序博客网 时间:2024/04/30 14:20

#import <Foundation/Foundation.h>


@interface Iphone : NSObject

{

    @public

    char *name;

    int size;

}


//1,没有返回值没有参数,对象函数调用属性

-(void)getIphoneInfo;

//2,没有返回值有参数

-(void)sendMessage:(char *)message;

//3,有返回值没有参数

-(int)getNum;

//4,有返回值有参数

-(NSString *)sendIphoneMessage:(int)number :(char *)message;

//5,带标记的方法

-(int)sumWithNum1:(int)num1 andNum2:(int)num2;



@end


@implementation Iphone


-(void)getIphoneInfo{

NSLog(@"Iphone Info:name = %s,size = %i",name,size);

}


-(void)sendMessage:(char *)content{

    NSLog(@"message = %s",content);

}


-(int)getNum{

    return 1;

}


-(NSString *)sendIphoneMessage:(int)number :(char *)content{

    NSString *message = [[NSStringalloc] initWithString:[NSStringstringWithFormat:@"number = %i,content = %s",number,content]];

    return message;

}


-(int)sumWithNum1:(int)num1 andNum2:(int)num2{

    return num1 + num2;

}


@end


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

    @autoreleasepool {

        Iphone *iphone = [Iphonenew];

        iphone->name = "ljs";

        iphone->size = 12;

        [iphone getIphoneInfo];

        [iphone sendMessage:"This is as message!"];

        NSLog(@"Num = %i",[iphonegetNum]);

        NSLog(@"Message = %@",[iphonesendIphoneMessage:221 :"hi"]);

        NSLog(@"result = %i",[iphonesumWithNum1:10andNum2:20]);

    }

    return 0;

}


0 0