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
在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
相关文章推荐
- C++中拷贝构造函数的应用详解
- 构造函数不能声明为虚函数的原因及分析
- C++类成员构造函数和析构函数顺序示例详细讲解
- c++基础语法:构造函数与析构函数
- JavaScript 构造函数 面相对象学习必备知识
- JavaScript面向对象设计二 构造函数模式
- Javascript面向对象编程(二) 构造函数的继承
- 成员初始化列表与构造函数体中的区别详细解析
- C#中私有构造函数的特点和用途实例解析
- C#中派生类调用基类构造函数用法分析
- C#静态构造函数用法实例分析
- javascript 静态对象和构造函数的使用和公私问题
- Javascript 使用function定义构造函数
- JavaScript中的普通函数与构造函数比较
- jQuery构造函数init参数分析续
- jquery 构造函数在表单提交过程中修改数据
- php中拷贝构造函数、赋值运算符重载
- JS特殊函数(Function()构造函数、函数直接量)区别介绍
- 不用构造函数(Constructor)new关键字也能实现JavaScript的面向对象
- javascript 构造函数强制调用经验总结