2014/10/20学习心得--简单的Person程序

来源:互联网 发布:网络安全工程师pdf 编辑:程序博客网 时间:2024/05/22 09:03

1 ,学习基本OC语言的程序编写:

(1),先在Command Line Tool中创建PersonTest工程;

(2),在main.m文件中添加person类;

(3),在person.h中编写实例变量,及方法的声明:

#import <Foundation/Foundation.h>


@interface Person :NSObject


//声明事例变量


{

   int identify;

   int age;


}


//声明方法


-(id)initWithAge:(int)_age identify:(int)_identify;

-(int)getAge;

-(int)getIdentify;

-(void)setAge:(int)_age;


@end


(4),在person.m中具体实现方法:

#import “Person.h”
@implementation Person
-(id)initWithAge:(int)-age identify:(int)_identify{
if(self=[super init]){
age=_age;
identify=_identify;
}
return self;
}

-(int)getAge{
return age;
}

-(int)identify{
return identify;
}

-(void)setAge:(int)_age;{
age=_age;

}

(5),在main .m 文件中为整个程序的具体实现过程:

#import <Foundation/Foundation.h>

#import "Person.h"


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

{


    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Hello, World!");

        

    }

    

   Person *person=[[Personalloc]initWithAge:16identify:45678];

    NSLog(@"person.age is %d",[persongetAge]);

   int age=30;

    [personsetAge:age];

    NSLog(@"the new person.age is %d",[persongetAge]);

    [personsetAge:++age];

    NSLog(@"the second person.age is %d",[persongetAge]);

    NSLog(@"the first person.identify is %d",[persongetIdentify]);  

   return0;

}


(6),显示结果:


2014-10-20 10:43:04.953 PersonTest[793:303] Hello, World!

2014-10-20 10:43:04.954 PersonTest[793:303] person.age is 16

2014-10-20 10:43:04.954 PersonTest[793:303] the new person.age is 30

2014-10-20 10:43:04.954 PersonTest[793:303] the second person.age is 31

2014-10-20 10:43:04.954 PersonTest[793:303] the first person.identify is 45678

Program ended with exit code: 0







0 0
原创粉丝点击