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

类目 延展 单例 协议

时间:2015-05-26 20:52:21      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

//类目
#import
<Foundation/Foundation.h> //声明一个NSString的分栏 @interface NSString (compareValue) - (NSComparisonResult)compareValue:(NSString *)string; @end ---------------------------------- #import "NSString+compareValue.h" @implementation NSString (compareValue) - (NSComparisonResult)compareValue:(NSString *)string{ if (self.integerValue > string.integerValue) { return NSOrderedDescending; } if (self.integerValue < string.integerValue) { return NSOrderedAscending; } return NSOrderedSame; } @end
#import <Foundation/Foundation.h>

@interface Student : NSObject

@property (nonatomic, retain)NSMutableArray *studyInfo;//表示学习科
+ (Student *)bestStudent;

@end

```````````````------------------
/*
 增加类目  已知 类 系统类
 延展  写在m文件私有  可操作类
      写在其他文件  封装
 kvo/kvc
*/
#import "Student.h"
@interface Student ()

@property (nonatomic, assign)NSInteger score;
- (NSInteger)compareScore:(Student *)other;

@end

@implementation Student

+ (Student *)bestStudent;{
    
    static Student *stu = nil;
    
    if (!stu) {
        
        stu = [[Student alloc]init];
        stu.studyInfo = [NSMutableArray array];
    }
    return stu;
}
//标准单例模式会将所有产生新对象导致释放值的方法重写
- (NSInteger)compareScore:(Student *)other{

    return self.score - other.score;
}
@end
------------------------------

#import <Foundation/Foundation.h>


 


@interface Person : NSObject


 


//应用里面的唯一用户 ,游戏里的主角


//唯一 、全局不释放、方便获取


 


//单例的获取方法


+ (Person *)heighestPerson;


 


@end


#import "Person.h"


#import "Student.h"


 


@interface Person ()//延展可操作类,提供私有化的方法和属性的申明


@property (assign, nonatomic)NSInteger age;


@end


 


@implementation Person


 


+ (Person *)heighestPerson{


 


    //建立静态对象,置为nil


    static Person *per = nil;


    


    if (!per) {//保证使用时初始化保证唯一,保证不释放


        


        per = [[Person alloc]init];//保证不释放


    }


    return per;


}


- (instancetype)init


{


    self = [super init];


    if (self) {


        


        NSArray *info = [Student bestStudent].studyInfo;


        NSLog(@"%@",info);


    }


    return self;


}


 


@end

 
//
//将特殊的属性和方法,放到独立的文件中,提高封装行
//提供私有化处理和方便性
#import "Person.h"

@interface Person ()//延展,提供私有化的方法和属性的申明
@property (assign, nonatomic)NSInteger age;
@end
#import <Foundation/Foundation.h>
#import "Working.h"

@interface Developer : NSObject<Working>
//支持协议,必须实现协议所规定的属性和方法
//属性直接声明
@property (nonatomic, assign)NSInteger salary;
//方法不需要声明,只要实现
@end

---------------
#import "Developer.h"

@implementation Developer

- (void)work{

    NSLog(@"work");
}
@end
---------------------
#import <Foundation/Foundation.h>   
//协议

//working协议,规定对象必须有salary(薪资标准)和work的能力
@protocol Working <NSObject>
@property (nonatomic, assign)NSInteger salary;

- (void)work;

@end

 

类目 延展 单例 协议

标签:

原文地址:http://www.cnblogs.com/myOcNote/p/4531513.html

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