标签:
//
// ViewController.m
// OC11分类和协议
//
// Created by Zoujie on 15/9/13.
// Copyright (c) 2015年 Zoujie. All rights reserved.
//
#import "ViewController.h"
#import "Fraction.h"
#import "GraphicObject.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//协议
{
id currentObject;
}
@end
id <Drawing> currentObject;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 分类
Fraction *a = [Fraction new];
Fraction *b = [Fraction new];
Fraction *result;
Fraction *tempFraction = [Fraction new];
Fraction *intvertResult = [Fraction new];
[a setTo:1 over:3];
[b setTo:2 over:5];
[a print]; NSLog(@" +");[b print];NSLog(@"-----");
result = [a add:b];
[result print];
NSLog(@"\n");
[a print]; NSLog(@" -");[b print];NSLog(@"-----");
result = [a sub:b];
[result print];
NSLog(@"\n");
[a print]; NSLog(@" *");[b print];NSLog(@"-----");
result = [a mul:b];
[result print];
NSLog(@"\n");
[a print]; NSLog(@" /");[b print];NSLog(@"-----");
result = [a div:b];
[result print];
NSLog(@"\n");
NSLog(@"----------------------------------");
intvertResult = [tempFraction invert:result];//对象接受 返回值 (小坑)
[intvertResult print];
NSLog(@"\n");
// 协议 代理
if ([currentObject conformsToProtocol:@protocol(Drawing)] == YES)//conformsToProtocol 检查某个方法是否遵循某项协议
{
[currentObject outline];
};
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// // GraphicObject.h // OC11分类和协议 // // Created by Zoujie on 15/9/13. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> @protocol Drawing//协议名称 -(void) paint; -(void) erase; @optional//可选择是否实现 -(void) outline; @end @interface GraphicObject : NSObject <Drawing> @end
// // GraphicObject.m // OC11分类和协议 // // Created by Zoujie on 15/9/13. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "GraphicObject.h" @implementation GraphicObject @end
// // Fraction.h // OC11分类和协议 // // Created by Zoujie on 15/9/13. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> @interface Fraction : NSObject @property int numerator,denominator; -(void) setTo:(int) n over:(int) d; //-(Fraction *) add:(Fraction *) f; -(void) reduce; -(double) convertToNum; -(void) print; @end //分类 #import "Fraction.h" @interface Fraction (MathOps)//为Fraction类定义新的分类,名称为MathOps -(Fraction *) add:(Fraction *)f; -(Fraction *) mul:(Fraction *)f; -(Fraction *) sub:(Fraction *)f; -(Fraction *) div:(Fraction *)f; //11.5 -(Fraction *) invert:(Fraction *)f;//结果倒置 @end
//
// Fraction.m
// OC11分类和协议
//
// Created by Zoujie on 15/9/13.
// Copyright (c) 2015年 Zoujie. All rights reserved.
//
#import "Fraction.h"
@implementation Fraction
-(void)setTo:(int)a over:(int)b
{
_numerator = a;
_denominator = b;
}
-(void)print
{
NSLog(@"%d/%d",_numerator,_denominator);
}
-(void) reduce
{
//局部变量
int u = _numerator;
int v = _denominator;
int temp;
while (v != 0) {
temp = u % v;
u = v ;
v = temp ;
}
_numerator /= u;
_denominator /= u;
}
-(double)convertToNum
{
if (_denominator != 0)
{
return (double) _numerator / _denominator;
}
else
{
return NAN;
}
}
@end
@implementation Fraction (MathOps)
//相加
-(Fraction *) add:(Fraction *)f
{
Fraction *result = [Fraction new];
result.numerator = (_numerator *f.denominator) + (_denominator * f.numerator);
result.denominator = _denominator * f.denominator;
[result reduce];
return result;
}
//相减
-(Fraction *) sub:(Fraction *)f{
Fraction *result = [Fraction new];
result.numerator = (_numerator * f.denominator) - (_denominator *f.numerator);
result.denominator = _denominator *f.denominator;
[result reduce];
return result;
}
//相乘
-(Fraction *) mul:(Fraction *)f{
Fraction *result = [Fraction new];
result.numerator = _numerator *f.numerator;
result.denominator = _denominator *f.denominator;
[result reduce];
return result;
}
//相除
-(Fraction *) div:(Fraction *)f{
Fraction *result = [Fraction new];
result.numerator = _numerator *f.denominator;
result.denominator = _denominator * f.numerator;
[result reduce];
return result;
}
//结果倒置
-(Fraction *) invert:(Fraction *)f
{
Fraction *result = [Fraction new];
result.numerator = f.denominator ;
result.denominator = f.numerator;
[result reduce];
return result;
}
@end
标签:
原文地址:http://my.oschina.net/u/2319073/blog/505654