//*************************************************
// 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 = [[Student alloc] 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 = [[Student alloc] initWithStuName:stuName stuAge:stuAge stuScore:stuScore stuHobby: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 = [[Teacher alloc] 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 = [[Teacher alloc] initWithTchName:tchName tchAge:tchAge tchHobby:tchHobby tchSex: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 = [[Student alloc] 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 = [[Teacher alloc] initWithTchName:@"张敏" tchAge:34 tchHobby:@"男" tchSex:@"女" stu1:stu];
[te setValue:@"紫霞" forKey:@"tchName"]; // key 指的是要修改的属性名
NSLog(@"%@", te.tchName);
NSLog(@"%@", [te valueForKey:@"tchName"]);// 对应getter
return 0;
}