黑马程序员技术交流社区

标题: MRC Student类 [打印本页]

作者: king、小白    时间: 2016-5-7 14:11
标题: MRC Student类
Student.h
#import<Foundation/Foundation.h>
@interface Student:NSObject
/*@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int age;
@property(nonatomic,assign)int score;*/
{
NSString *_name;
int _age;
int _score;
}
-(void)setName:(NSString *)name;
-(NSString *)NSString *name;

-(void)setAge:(int)age;
-(int)intage;

-(void)setScore:(int)score;
-(int)intscore;

-(id)initWithName:(NSString *)name andAge:(int)age andScore:(int)score;

-(void)printinfo;
@end
Student.m
#import"Student.h"
@implementation Student
-(void)setName:(NSString *)name
{
if(_name != name){
[_name release];
_name=[name retain];
}
}
-(NSString *)NSString *name
{
return _name;
}

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

-(void)setScore:(int)score{
_score=score;
}
-(int)intscore
{
return _score;
}

-(id)initWithName:(NSString *)name andAge:(int)age andScore:(int)score
{
if(self = [super init]){
_name =name;
_age = age;
_score = score;
}
return self;
}

-(void)printinfo{
NSLog(@"My Name Is %@ Age Is %d Score Is %d",self.name,self.age,self.score);
}
-(NSString *)description
{
return[NSString stringWithFormat:@"xingming=%@,nianling=%d,chengji=%d",_name,_age,_score];
}
-(void)dealloc
{
[_name release];
[super dealloc];
NSLog(@"%@",__func__);
}

mian.m
#import<Foundation/Foundation.h>
#import"Student.h"
int main(){
Student *stu1=[[Student alloc]initWithName:@"s1" andAge:11 andScore:87];
Student *stu2=[[Student alloc]initWithName:@"s2" andAge:22 andScore:97];
Student *stu3=[[Student alloc]initWithName:@"s3" andAge:33 andScore:100];
[stu1 printinfo];
[stu2 printinfo];
[stu3 printinfo];



[stu1 release];
[stu2 release];
[stu3 release];
return 0;
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2