//
// main.m
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

import "Student.h"

import "Fractron.h"

int main(int argc, const char * argv[]) {

if 0

// 创建学生对象

Student *dawa = [[Student alloc] init];
// 赋值
// 消息语法
[dawa setName:@"12"];
[dawa setNumber:1];
[dawa setNickName:@"大娃"];
// 取值
NSLog(@"%@ %ld %@", [dawa name], [dawa number], [dawa nickName]);

// 新对象

Student *sanye = [[Student alloc] init];
// 点语法 能够方便访问和使用属性
// 赋值
sanye.name = @"13";
sanye.number = 2;
sanye.nickName = @"三爷";
// 取值
NSLog(@"%@ %ld %@", sanye.name, sanye.number, sanye.nickName);

endif


// 练习: 分数计算
Fractron *f1 = [[Fractron alloc] init];
f1.son = 1;
f1.mother = 2;
Fractron *f2 = [[Fractron alloc] init];
f2.son = 1;
f2.mother = 3;
// 加法(类)
Fractron *he = [Fractron addWhitF1:f1 f2:f2];
NSLog(@"%@ + %@ = %@",f1, f2, he);

// NSLog(@"%ld/%ld", he.son, he.mother);

// 加法(实例)
Fractron *newHe = [f1 addWithF:f2];
NSLog(@"%@ + %@ = %@", f1, f2, newHe);

return 0;

}



//
// Person.h
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Person : NSObject
//{
// // 成员变量
// NSString *_name;
// NSString *_sex;
// NSInteger _age;
// CGFloat _height;
//}
// detter/setter
// 属性 property
// 声明一对getter&setter方法
// 属性(property)的属性/特性(attributes)
// 1. 读写性 外观控制
// readwrite 可读可写(默认) 有getter和setter方法
// readonly 只读 只有一个getter方法
// setter setter 方法改名
// getter getter 方法改名
// 2. 原子性
// atomic 原子性 多线程保护(默认)
// nonatomic 非原子性 不需要保护(使用)
// 3. 语义特性 内存管理
// MRC模式
// assign 直接分配 -> 非对象 (默认)
// retain 内存特有 -> 所有对象
// copy 内存拷贝 -> NSString
// ARC模式
// strong 强引用
// weak 弱引用
// 关键字 特性 类型 属性名
@property (readwrite, getter=quzhi, setter=fuZhi:, nonatomic, copy) NSString *name;
@property (nonatomic, copy)NSString *sex;
@property (nonatomic, assign)NSInteger age;
@property (nonatomic, assign)CGFloat height;
/*

  • (void)setname:(NSString *)name;
  • (NSString *)name;
  • (void)setsex:(NSString *)sex;
  • (NSString *)sex;
  • (void)setage:(NSInteger)age;
  • (NSInteger)age;
  • (void)setheight:(CGFloat)height;
  • (CGFloat)height;
    */

// 自定义初始化

  • (instancetype)initWithName:(NSString *)name

                         sex:(NSString *)sex
                         age:(NSInteger)age
                      height:(CGFloat)height;

    // 便利构造器

  • (instancetype)personWithName:(NSString *)name

                           sex:(NSString *)sex
                           age:(NSInteger)age
                        height:(CGFloat)height;

    // 普通方法

  • (void)sayHi;

@end


//
// Person.m
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Person.h"

@implementation Person

pragma mark - setter&setter

pragma mark - <#注释#>

pragma mark -

// 属性的实现 synthesize(三色色子)
// 关键字 属性名 getter&setter操作的对应成员变量名
//@synthesize name = _name;
//@synthesize sex = _sex;
//@synthesize age = _age;
//@synthesize height = _height;
//
//@synthesize name = _name, sex = _sex, age = _age, height = _height;

/*

  • (void)setname:(NSString *)name
    {
    _name = name;
    }
  • (NSString *)name
    {
    return _name;
    }
  • (void)setsex:(NSString *)sex
    {
    _sex = sex;
    }
  • (NSString *)sex
    {
    return _sex;
    }
  • (void)setage:(NSInteger)age
    {
    _age = age;
    }
  • (NSInteger)age
    {
    return _age;
    }
  • (void)setheight:(CGFloat)height
    {
    _height = height;
    }
  • (CGFloat)height
    {
    return _height;
    }

*/
//自定义初始化

pragma mark - 自定义初始化方法

  • (instancetype)initWithName:(NSString )name sex:(NSString )sex age:(NSInteger)age height:(CGFloat)height
    {
    self = [super init];
    if (self) {

    [self fuZhi:name];
    self.name = name;
    [self setSex:sex];
    [self setAge:age];
    [self setHeight:height];

    }
    return self;
    }

pragma mark - 便利构造器

  • (instancetype)personWithName:(NSString *)name

                      sex:(NSString *)sex
                      age:(NSInteger)age
                   height:(CGFloat)height

    {
    Person *a = [[Person alloc]initWithName:name sex:sex age:age height:height];
    return a;
    }

pragma mark - 普通方法

  • (void)sayHi
    {

    // setter 方法
    [self fuZhi:@""];
    // getter 方法
    NSLog(@"%@", [self quzhi]);
    // 读写性中修改了方法外观后 点语法使用没有影响 消息语法调用时 需要使用修改后的方法名
    NSLog(@"Helllo");

    }

@end



//
// Teacher.h
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Teacher : NSObject
// 属性
@property (nonatomic, copy)NSString *name;
@property (nonatomic, assign)NSInteger age;
@property (nonatomic, copy)NSString *major;
@property (nonatomic, copy)NSString *course;
// 属性完成的三件事
// 1. 声明getter和setter
// 2. 实现getter和setter并指定操作的成员变量
// 3. 定义成员变量(以下划线加当前属性名)
// 属性的本质: getter和setter方法
@end


//
// Teacher.m
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Teacher.h"

@implementation Teacher

@end



//
// Practrace.h
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

import "Person.h"

import "Teacher.h"

@interface Practrace : NSObject
// 属性
// assign 直接分配 -> 非对象 (默认)
// retain 内存特有 -> 所有对象
// copy 内存拷贝 -> NSString

@property (nonatomic, copy)NSString *a;
@property (nonatomic, assign)NSInteger b;
@property (nonatomic, assign)CGFloat c;
@property (nonatomic, retain)Person *d;
@property (nonatomic, retain)Teacher *e;
@property (nonatomic, retain)NSArray *f;
@property (nonatomic, retain)NSDictionary *g;
@property (nonatomic, retain)NSSet *h;
@property (nonatomic, retain)NSNumber *i;
@property (nonatomic, retain)NSValue *j;
@property (nonatomic, assign)int *k;
@property (nonatomic, assign)char l;
@property (nonatomic, assign)BOOL m;
@end


//
// Practrace.m
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Practrace.h"

@implementation Practrace

@end



//
// Student.h
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, copy) NSString *name;// 姓名
@property (nonatomic, assign) NSInteger number;// 学号
@property (nonatomic, copy) NSString *nickName;// 昵称
@end


//
// Student.m
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Student.h"

@implementation Student

@end



//
// Fractron.h
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Fractron : NSObject
// 特征
@property (nonatomic, assign) NSInteger son;/*< 分子/
@property (nonatomic, assign) NSInteger mother;/*< 分母/
// 行为(方法)
/// 加法(类)

  • (Fractron )addWhitF1:(Fractron )f1

                     f2:(Fractron *)f2;

    /// 加法(实例)

  • (Fractron )addWithF:(Fractron )f;
    /// 减法

/// 乘法

/// 除发

@end


//
// Fractron.m
// OC_3属性
//
// Created by YIem on 15/11/26.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Fractron.h"

@implementation Fractron

// 加法

  • (Fractron )addWhitF1:(Fractron )f1

                 f2:(Fractron *)f2

    {
    // 创建新的分数 表述分数和
    Fractron *f = [[Fractron alloc] init];
    // [类 add:分数1 :分数2];
    // 分子
    f.son = f1.son f2.mother + f1.mother f2.son;
    // 分母
    f.mother = f1.mother * f2.mother;
    return f;
    }

// 实例版加法

  • (Fractron )addWithF:(Fractron )f
    {
    Fractron *newF = [[Fractron alloc] init];
    // [分数1 add: 分数2];
    // 哪个对象调用了方法 在方法内部 使用Self表示该对象
    // self -> 分数1
    // f -> 分数2

    // 分子
    newF.son = self.son f.mother + self.mother f.son;
    // 分母
    newF.mother = self.mother * f.mother;
    return newF;
    }

// 对象打印描述方法 控制对象在NSlong的输出样式 系统自动调用

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"%ld/%ld", self.son, self.mother];
    }

@end