OC中结构体作为对象属性
2016-01-04 20:24
253 查看
在OC中结构体有时候也作为对象的属性
类的定义
类方法
对象的实例化及方法的实现
类的定义
#import <Foundation/Foundation.h> typedef struct{ int year; int month; int day; } Date; @interface Student : NSObject { @public NSString *_name; Date _birthday; } -(void) say; @end
类方法
#import "Student.h" @implementation Student -(void) say{ NSLog(@"name=%@;year=%d,month=%d,day=%d",_name,_birthday.year,_birthday.month,_birthday.day); } @end
对象的实例化及方法的实现
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[[Student alloc] init]; stu->_name=@"喵星人"; //方法1 // stu->_birthday=(Date){1995,2,15}; //方法2 // stu->_birthday.year=1995; // stu->_birthday.month=2; // stu->_birthday.day=25; //方法3 Date date={1995,2,25}; stu->_birthday=date; [stu say]; } return 0; }
相关文章推荐
- GCD线程
- Hibernate中编程式事物的简单使用
- Problem 2163 多米诺骨牌(单调栈)
- Hibernate中编程式事物的简单使用
- 我理解的云计算与大数据
- 深度学习(二十六)Network In Network学习笔记-ICLR 2014
- SQlite 使用笔记
- 黑马程序员——java基础之其他对象
- iOS dSYM文件结构剖析(上)
- lintcode 中等题:subSets 子集
- 解决Xcode7真机调试出现:The account “” has no team with ID “”
- The art of computer programming Donald E. Knuth volumn one third edition读书笔记1
- 大牛博客
- SQLite-常规用法
- 不要向你的公司提加工资
- SQL优化(三) PostgreSQL Table Partitioning
- Remove Linked List Elements
- 网络字节序和本机字节序
- 树莓派的使用
- .NET学习(十三)上传图片或文件