您的位置:首页 > 移动开发 > Objective-C

Objective-C入门05:自定义构造函数

2015-09-16 14:39 423 查看
Objective-C入门05:自定义构造函数


在main函数里面

#import <Foundation/Foundation.h>

#import "Hero.h"

#import "Monster.h"

int main(int argc,constchar * argv[]) {

    @autoreleasepool {
      Hero * tiMo = [  [Heroalloc
initWithXue : 1000 ] ;
      Monster * lanBaBa = [ [Monsteralloc
initWithGJL:10 ] ;
    }
   return 0 ;
}

在[b]Hero.h里面[/b]

#import <Foundation/Foundation.h>

@interface Hero : NSObject

- (id) initWithXue:(int)xue;

@end

在[b]Hero.m里面[/b]

#import "Hero.h"

@implementation Hero

- (id) initWithXue : (int)xue
{
   self = [
super 
init];
   if(
self )
    {
      NSLog(@"Hero的血量是%d"
, xue);

    }

    returnself;
}

@end

在[b]Monster.h里面[/b]

#import <Foundation/Foundation.h>
@interface Monster :NSObject

- (id )initWithGJL : (int )GJL;

@end

[b]在Monster.m里面[/b]

#import "Monster.h"

@implementation Monster

- (id )initWithGJL : (int )GJL
{
   self = [
super 
init ] ;
   if(
self )
    {
       NSLog(@"怪物的攻击力是%d",GJL);
    }

    returnself ;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息