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

LessonClassExtension

时间:2015-03-10 21:24:26      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

//

//  main.m

//  2-8 LessonClassExtension

//

//  Created by lanouhn on 15/3/10.

//  Copyright (c) 2015年 lanouhn. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Pen.h"

#import "Pencil.h"

#import "NSString+HelloWorld.h"

#import "Pen+sayHi.h"

#import "Boy.h"

#import "Woman.h"

#import "BaoMuDelegate.h"

#import "Doctor.h"

#import "Mother.h"

#import "Student.h"

#import "Teacher.h"

 

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

    @autoreleasepool {

        // insert code here...

        //NSDate, 日期类

        NSDate *date = [NSDate date];

        NSLog(@"%@", date);

        //格式: 年-月-日 时:分:秒 时区

        

        //NsTimeInterval:时间的计算单位, 秒, 实质是double

        //一分钟以后

        NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:60];

        NSLog(@"%@", date1);

        

        //昨天的现在时刻

        NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:-24 * 3600];

        NSLog(@"%@", date3);

        

        //明年的现在时刻

        

        NSDate *date4 = [NSDate dateWithTimeIntervalSinceNow:366 * 24 * 3600];

        NSLog(@"%@", date4);

        

        //昨天的现在时刻 与 明年的现在时刻 间隔是多少?

        NSTimeInterval seconds = [date3 timeIntervalSinceDate:date4];

        NSLog(@"%.2lf", seconds);

        

        //现在时刻 距离 1970年1月1日 间隔多少秒?

        NSTimeInterval seconds1 = [date timeIntervalSince1970];

        NSLog(@"%.2lf", seconds1);

        

        //时间戳: 某一时刻距离1970年1月1日的秒数

        //时间戳转日期

        NSTimeInterval interval1 = 1425956564.34;

        NSDate *date5 = [NSDate dateWithTimeIntervalSince1970:interval1];

        NSLog(@"%@", date5);

        

        //计算当前时间和一个固定时间的差值,如果差值在60秒内,输出“刚刚”,如果在60秒外3600秒内,输出“xx分钟前”,如果3600秒外,3600*24秒内,输出“xx小时前”。

        

        //固定时间

        NSDate *aDate = [date dateByAddingTimeInterval:3600 * 25];

        //当前时间与固定时间的间隔

        NSTimeInterval interval2 = [aDate timeIntervalSinceDate:date];

        if (interval2 <= 60) {

            NSLog(@"刚刚");

        } else if (interval2 < 3600) {

            NSLog(@"%d分钟前", (int)interval2 / 60);

        } else if (interval2 < 3600 * 24) {

            NSLog(@"%d小时前", (int)interval2 / 3600);

        } else {

            NSLog(@"%@", aDate);

        }

        

        //NSDateFormatter: 日期格式类

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        //设置日期格式

        //yyyy,YYYY:四位年份

        //yy,YY:两位年份

        //MM:两位月份, 不足补零

        //M:一位月份

        //注:月份必须大写

        //dd:两位日期, 不足补零

        //d:以为日期

        //注:日期必须小写, 大写为当前日期是这一年的第几天

        //hh:两位12进制小时, 不足补零

        //h:一位12进制小时

        //HH:两位24进制小时, 不足补零

        //H:一位24进制小时

        //mm:两位分钟, 不足补零

        //m:一位分钟

        //注:分钟必须小写

        //ss:两位秒数, 不足补零

        //s:一位秒数

        //注:秒数必须小写;

        

        

        [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

        

        //设置时区

        //NSTimeZone:时区类

        //NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"china"];

        NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8 * 60 * 60];

        [formatter setTimeZone:timeZone];

 

        

        

        //把某一时刻的日期, 按照规定好的格式, 转换成字符串

        NSString *string = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];

        NSLog(@"%@", string);

        

        //将字符串@“2014年05月01日 10点23分18秒”转换为NSDate对象

        

        NSString *string1 = @"2014年05月01日 10点23分18秒";

        NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];

        [formatter1 setDateFormat:@"yyyy年MM月dd日 hh时mm分ss秒"];

        [formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

        NSDate *newDate = [formatter1 dateFromString:string1];

        NSLog(@"%@", newDate);

        

        //类的扩充

        //1.对于已知类(可以查看.m文件)进行扩充

        //修改类的源文件

        Pen *pen = [[Pen alloc] init];

        [pen draw];

        

        //2.对于未知类(不能查看.m文件)进行扩充

        //a.继承(子类化), 子类可以重写父类的方法, 扩充自己的方法

        Pencil *pencil = [[Pencil alloc] init];

        [pencil draw];

        [pencil write];

        

        //b.Categroy, 分类, 类目

        //文件名:类名+分类名.h, 类名+分类名.m

        [NSString helloWorld];

        

        //使用分类为未知类进行扩充的步骤

        //1.新建文件

        //2.选择objictive-C file模板

        //3.填写分类名, 选择Category, 选择要扩充的类

        //4.在.h中写方法声明, 在.m中写方法实现

        //5.引入头文件, 使用扩展方法

        

        //为Pen使用分类的方式, 扩充一个sayHi方法

        [Pen sayHi];

        

        //分类

        //1.一般用于对系统类进行扩充方法

        //2.分类中不能够添加实例变量

        

        //c.Extension:延展, 为类扩充私有方法声明, 实例变量

        //文件:类名_延展名.h

        

        //延展的两种写法

        //1.通过模板创建.h, 然后再类的.m中引用

        //注;这种写法的弊端, 如果你把.h引用到其他文件中, 在其他文件中也可以调用方法

        

        //2.直接在.m中写, 写法

        //@interface 类名() {

        //      实例变量

        //}

        //      私有方法声明

        //@end

        

        //protocol:协议

        //遵守协议用<>, 写在@interface, 父类名的后面

        Boy *boy = [[Boy alloc] init];

        [boy buyFood];

        [boy buyiPhone];

        

        //delegate: 代理

        

        //步骤

        //找代理的类

        //1.制定协议

        //2.添加实例变量 注:id<协议名>

        //3.为实例变量写setter方法

        

        //想要成为代理的类

        //1.遵守协议

        //2.实现协议中必须实现的方法

        //3.

        

        

        //代理是一一对应的关系

        Woman *woman = [[Woman alloc] init];

        [woman setDelegate:boy];

        

        [woman makeFood];

        [woman takeCareOfChildren];

        

        Mother *mother = [[Mother alloc] init];

        Student *stu = [[Student alloc] init];

        Doctor *doc = [[Doctor alloc] init];

        Teacher *tea = [[Teacher alloc] init];

        

        [mother setDelegate:stu];

        [mother takeCareOfChildren];

        [mother makeFood];

        

        [mother setDelegate:doc];

        [mother takeCareOfChildren];

        [mother makeFood];

        

        [mother setDelegate:tea];

        [mother takeCareOfChildren];

        [mother makeFood];

        }

    return 0;

}

 

LessonClassExtension

标签:

原文地址:http://www.cnblogs.com/xiaoxuetongxie/p/4328290.html

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