OCday4 属性, 点语法, KVC
来源:互联网 发布:mac 使命召唤 编辑:程序博客网 时间:2024/05/26 09:57
//*************************************************
// Student.h 文件
#import <Foundation/Foundation.h>
@interface Student :NSObject
//{
// NSString *_stuName;
// NSInteger _stuAge;
// CGFloat _stuScore;
//}
// 声明属性
@property(readwrite,setter=nihao:, getter=buhao) NSString *stuName;
// 属性的属性设置成readonly之后,只有访问器, 没有设置器, 默认是readwrite
// setter=method 相当于给setter方法进行改名,方法名别忘加':'
// 系统一般对BOOL类型的属性进行改名,增加属性的可读性
// 属性的目的是为了简化程序员编码
// 声明属性的的关键词是property,后面对应的属性类型和属性名
// 属性一共完成了三件事:一是声明了设置器和访问器, 二是实现了设置器和访问器,三是声明了一个对应的成员变量, 属性名前加一个'_'(可见度是private)
// 属性的属性第二种:原子性设置
// 默认是atomic,可以实时监控事务, 但是我们只是做一个简单的赋值操作,不需要监控, 所以一般情况下设置成nonatomic,非原子性
@property(nonatomic,assign) NSInteger stuAge;
@property(nonatomic,assign) CGFloat stuScore;
@property(nonatomic,copy) NSString *stuHobby;
@property(nonatomic,copy) NSString *stuSex;
@property(nonatomic,retain)NSArray *arr;
@property(nonatomic,retain)Student *stu1;
// 属性的属性第三条语义设置
// assign 一般是CGFloat这种基本数据类型,在栈区, 我们使用assign
// copy 只有NSString *使用 (新的)
// retain 我们自己创建的类和系统的类所对应的对象使用retain (引用计数)
// 自定义初始化方法完整版
- (id)initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
stuSex:(NSString *)stuSex;
// 便利构造器
+ (id)StudentWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
stuSex:(NSString *)stuSex;
//
- (void)test;
@end
//*************************************************
// Student.m 文件
#import "Student.h"
@implementation Student
// 老版本里属性的实现,新版本不需要
//@synthesize stuAge = _stuAge;
//
- (void)test {
NSLog(@"%@",_stuName);
}
// 自定义初始化方法完整版
- (id)initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
stuSex:(NSString *)stuSex {
self = [[Studentalloc] init];
if (self) {
_stuAge = stuAge;
_stuHobby = stuHobby;
_stuName = stuName;
_stuScore = stuScore;
_stuSex = stuSex;
}
return self;
}
// 便利构造器
+ (id)StudentWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
stuSex:(NSString *)stuSex {
Student *stu = [[Studentalloc] initWithStuName:stuNamestuAge:stuAge stuScore:stuScorestuHobby:stuHobby stuSex:stuSex];
return stu;
}
@end
//*****************************************
// Teacher.h 文件
#import <Foundation/Foundation.h>
#import "Student.h"
@interface Teacher : NSObject
// 五条属性
@property (nonatomic, copy)NSString *tchName;
@property (nonatomic, assign)NSInteger tchAge;
@property (nonatomic, copy)NSString *tchHobby;
@property (nonatomic, copy)NSString *tchSex;
@property (nonatomic, retain)Student *stu1;
// 自定义初始化方法
- (id)initWithTchName:(NSString *)tchName tchAge:(NSInteger)tchAge tchHobby:(NSString*)tchHobby tchSex:(NSString *)tchSex stu1:(Student*)stu1;
// 便利构造器
+ (id)TeacherWithTchName:(NSString *)tchName tchAge:(NSInteger)tchAge tchHobby:(NSString *)tchHobby tchSex:(NSString *)tchSex stu1:(Student*)stu1;
@end
//*****************************************
// Teacher.m 文件
#import "Teacher.h"
@implementation Teacher
// 自定义初始化方法
- (id)initWithTchName:(NSString *)tchName tchAge:(NSInteger)tchAge tchHobby:(NSString *)tchHobby tchSex:(NSString *)tchSex stu1:(Student*)stu1 {
self = [[Teacheralloc] init];
if (self) {
_tchName = tchName;
_tchAge = tchAge;
_tchHobby = tchHobby;
_tchSex = tchSex;
_stu1 = stu1;
}
return self;
}
// 便利构造器
+ (id)TeacherWithTchName:(NSString *)tchName tchAge:(NSInteger)tchAge tchHobby:(NSString *)tchHobby tchSex:(NSString *)tchSex stu1:(Student*)stu1 {
Teacher *tch = [[Teacheralloc] initWithTchName:tchNametchAge:tchAge tchHobby:tchHobbytchSex:tchSex stu1:stu1];
return tch;
}
@end
//***************************************************
// main.m 文件
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Teacher.h"
int main(int argc,const char * argv[]) {
Student *stu = [[Studentalloc] init];
//// [stu setStuName:@"周星星"];
//// NSLog(@"%@", [stu stuName]);
//
//// [stu nihao:@"hello 周星星"];
//// NSLog(@"%@", [stu stuName]);
//
//// NSLog(@"%@", [stu buhao]);
//
// //
// Teacher *tea = [[Teacher alloc] initWithTchName:@"周星星" tchAge:28 tchHobby:@"演员" tchSex:@"男" stu1:stu];
// [tea setTchName:@"达叔"];
//
//
//
// // 点语法
// tea.tchName = @"星爷"; //语法糖, 与下面效果相同, 本质上还是调用setter
// [tea setTchName:@"星爷"];
//
// NSLog(@"%@", [tea tchName]);
// NSLog(@"%@", tea.tchName); // 本质上还是调用getter
// // 点语法区分setter和getter就是找,是否有等号, 有等号就是setter, 没有等号就是getter
// // 一般点语法用来操控属性里的getter和setter
// // 如果点多的话, 只有离'='最近的一个是setter, 其余的都是getter
//
// tea.tchHobby = @"学习";
// NSLog(@"%@", tea.tchHobby);
// KVC(Key-Value-Coding), 键值编码
//间接对属性进行赋值
Teacher *te = [[Teacheralloc] initWithTchName:@"张敏"tchAge:34tchHobby:@"男"tchSex:@"女"stu1:stu];
[te setValue:@"紫霞"forKey:@"tchName"];// key 指的是要修改的属性名
NSLog(@"%@", te.tchName);
NSLog(@"%@", [tevalueForKey:@"tchName"]);//对应getter
return 0;
}
- OCday4 属性, 点语法, KVC
- 08 属性 点语法 KVC
- OC04_属性,kvc,点语法
- day8: 属性、点语法、KVC
- OC基础:属性.点语法.KVC
- OC第八天 属性 点语法 KVC
- OC初期-08 属性,点语法,KVC
- OC 属性的属性 点语法的使用 KVC
- 随意细解:OC -- 属性、点语法、KVC
- 进击的KFC:OC(八)属性、点语法、KVC
- 点语法和KVC
- OC第八天 — 属性, 属性的属性, 点语法, KVC
- OC学习第八天_属性 属性的Attribute 点语法 KVC
- 点语法和KVC方法比较
- ObjectC----Property,Attribute,点语法,KVC,KVO
- oc 点语法 / KVC /readwrite/readonly/
- KVC和点语法的性能
- 属性与点语法
- VC控件MSComm编写串口通信程序
- HDU_1863《畅通工程》
- Leetcode Binary Tree Inorder Traversal 二叉树中序遍历
- Linux设备驱动程序学习———时间、延迟及延缓操作
- IOS怎么抓取网络请求包
- OCday4 属性, 点语法, KVC
- double截取小数点两位
- ubuntu core 文件产生
- udelay(); mdelay(); ndelay(); msleep();
- Objective C中数组排序几种情况的总结
- IOS开发Q&A-IOS8定位应用定位失败及音频录制的相关参数-1
- 二叉排序树
- cocos2d-x之Android播放视频c++代码
- Spring Web MVC 的HandlerMapping的使用之-------BeanNameUrlHandlerMapping