码迷,mamicode.com
首页 > 其他好文 > 详细

Object-C类、方法、构造函数(2)

时间:2016-01-25 19:14:26      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

Object-C 代码分为三部分:.h文件、.m文件及调用文件

.h源文件

#import <Foundation/Foundation.h>

@interface Student:NSObject
{
NSString *studentName;
NSInteger age;
}
-(void) printInfo;
-(void) setStudentName:(NSString*) name;
-(NSString*) studentName;
-(NSInteger) age;

@end

 .m源文件

#import "Student.h"

@implementation Student

-(void) printInfo
{
NSLog(@"姓名:%@ 年龄:%d岁",studentName,studentAge);
}
-(void) setStudentName:(NSString*) name
{
studentName=name;
}
-(void)setAge:(NSInteger) age
{
studentAge=age;
}
-(NSString*) studentName
{
return studentName;
}
-(NSInteger) age
{
return studentAge;
}
@end

 调用源文件

Student *student=[[Student alloc]init];  //init相当于实例化
[student setStudentName:@"张三"];
[student setAge:10];
[student printInfo];
[student release]; //资源释放

 

实例方法和构造函数的总结

技术分享

 

Object-C类、方法、构造函数(2)

标签:

原文地址:http://www.cnblogs.com/nidongde/p/5158017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!