标签:
// // main.m // OC7类 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> #import "Fraction.h"//导入头文件 #import "Complex.h" #define Choose 0 int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); Fraction *myFraction = [Fraction new]; if (Choose) { // [myFraction setNumberator1:11]; myFraction.denominator1 = 11; // [myFraction setDenominator1:3]; myFraction.numberator1 = 3; } else { [myFraction setNumberator1:1 Denominator:7]; } NSLog(@"The value of myFraction"); [myFraction print]; NSLog(@"The value of myFranction:%i/%i",myFraction.numberator1,myFraction.denominator1); // add 传入对象参数 Fraction *afraction = [Fraction new]; Fraction *bfraction = [Fraction new]; [afraction setNumberator1:1 Denominator:4]; [bfraction setNumberator1:1 Denominator:2]; [afraction print]; NSLog(@"+"); [bfraction print]; NSLog(@"="); // // [afraction add:bfraction]; // [afraction print]; // //// reduce 分数相约到最简形式 // [afraction reduce]; // [afraction print]; // // 在方法中分配和返回对象 Fraction *resultFraction; resultFraction = [afraction newAdd:bfraction]; [resultFraction print]; Fraction *subRusult = [bfraction subtract:afraction]; [subRusult print]; // 多态 不同类使用相同方法 Fraction *f1 = [Fraction new]; Fraction *f2 = [Fraction new]; Fraction *fracResult; Complex *c1 = [Complex new]; Complex *c2 = [Complex new]; Complex *complexResult; // [f1 setTo:1 over:10]; [f1 setNumberator1:1 Denominator:10]; [f2 setNumberator1:2 Denominator:15]; [c1 setReal:18.0 andImaginary:2.5]; [c2 setReal:-5.0 andImaginary:3.2]; [c1 print]; NSLog(@" +");[c2 print]; NSLog(@"------------"); complexResult = [c1 add:c2]; // [complexResult reduce]; [complexResult print]; NSLog(@"\n"); // 将两个分数相加的显示 [f1 print];NSLog(@" +");[f2 print]; NSLog(@"----"); fracResult = [f1 newAdd:f2]; [fracResult print]; // id 可以用来存储属于任何类的对象 id dataValue; NSLog(@"---------------id类型"); dataValue = f1; [dataValue print]; dataValue = c1; [dataValue print]; // // self.dataValue; //id变量无法使用点运算符 // 处理动态类型的方法 if ([dataValue isKindOfClass:[Complex class]])//对象是不是Complex或其子类的成员,继承层次关系 { NSLog(@"dataValue ‘s SuperClass is %@",[dataValue class]); }; if ([dataValue isMemberOfClass:[Complex class]]){};//对象是不是Complex的成员,直接成员关系 if ([f1 respondsToSelector:@selector(setReal:andImaginary:)]){};//对象是否能够响应selector所指定的方法 if ([dataValue instancesRespondToSelector:@selector(setReal:andImaginary:)]){};//指定的类实例是否能响应selector if ([dataValue isSubclassOfClass:[Complex class]]){};//对象是否是指定类的子类 [dataValue performSelector:@selector(setValue:forKey:)];//应用selector指定的方法 [dataValue performSelector:@selector(setNumberator1:Denominator:) withObject:f1];//使用selector指定的方法,传递参数 [dataValue performSelector:@selector(setNumberator1:Denominator:) withObject:f1 withObject:f2]; //使用@try处理异常 Fraction *f = [Fraction new]; // [f noSuchMethod]; NSLog(@"Execution continues"); @try { // [f setReal:18.0 andImaginary:2.5]; } @catch (NSException *exception) { NSLog(@"Caught %@%@",[exception name],[exception reason]); } @finally { NSLog(@"Execution continues"); } } return 0; }
// // Fraction.h // OC7类 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> //接口文件 @interface Fraction : NSObject #pragma mark - 属性 自动生成 Setter && Get @property int numberator1; @property int denominator1; #pragma mark - Setter && Get //-(void)setNumerator:(int)n; //-(void)setDenominator:(int)d; //-(int)numerator; //-(int)denominator; -(double) convertToNum; -(void)print; /** * 多参数函数 */ -(void)setNumberator1:(int) n Denominator:(int)d; /** * add */ -(void) add:(Fraction *)f; /** * 相约数 */ -(void) reduce; /** * 记数 */ -(int)showpage; /** * new add */ -(Fraction *) newAdd:(Fraction *)f; /** * 减去消息接受着的参数 */ -(Fraction *) subtract :(Fraction *)f; /** * 乘 */ -(Fraction *) multiply :(Fraction *) f; /** * 除 */ -(Fraction *) divide :(Fraction *)f; @end
// // Fraction.m // OC7类 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "Fraction.h"//双引号实用于本地文件(自己创建的文件),而不是系统文件 //实现文件 实现部分包含的私有信息 /** * static 关键字 局部静态变量 记录所有对象调用函数的次数 局部变量 只记录每个对象自己调用函数的次数 */ static int hitCount = 0;//调用次数 @implementation Fraction //{ // int numberator; // int denominator; //} /** * 如果省略@synthesize指令,那么编译器会自动分别命名底层的实例变量_numberator1,_denominator1. */ @synthesize numberator1,denominator1; //-(void) setNumerator:(int)n //{ // numberator = n; //} // //-(void) setDenominator:(int)d //{ // denominator = d; //} //********setter getter的参数名 不要跟 实例名称 相同 ********* -(void)setNumberator:(int)numberator { numberator1 = numberator; } -(void) setDenominator:(int)denominator { denominator1 = denominator; } //-(void) setDenominator:(int)denominator //自动生成Setter //{ // denominator = denominator; //} -(int)numerator { return numberator1; } -(int)denominator { return denominator1; } -(void)print { NSLog(@"%i/%i",numberator1,denominator1); } -(double)convertToNum { if (denominator1 != 0) { return (double) numberator1 / denominator1; } else { return NAN; } } -(void)setNumberator1:(int)n Denominator:(int)d { denominator1 = d; numberator1 = n; } -(void) add:(Fraction *)f { numberator1 = numberator1 * f.denominator1 +denominator1 *f.numberator1; denominator1 = denominator1 * f.denominator1; // self 关键字 当前方法的接受着 [self reduce]; } /* * new add */ -(Fraction *) newAdd:(Fraction *)f { Fraction *result = [Fraction new]; result.numberator1 = numberator1 * f.denominator1 + denominator1 * f.numberator1; result.denominator1 = denominator1 * f.denominator1; [result reduce]; return result; } /** * sub */ -(Fraction *) subtract:(Fraction *)f { Fraction *result = [Fraction new]; result.numberator1 = numberator1 * f.denominator1 - denominator1 * f.numberator1; result.denominator1 = denominator1 * f.denominator1; [result reduce]; return result; } -(void) reduce { //局部变量 int u = numberator1; int v = denominator1; int temp; while (v != 0) { temp = u % v; u = v ; v = temp ; } numberator1 /= u; denominator1 /= u; } -(int)showpage { int hitCount = 0; hitCount ++; return hitCount; } @end
// // Complex.h // OC7类 // // Created by Zoujie on 15/8/30. // Copyright (c) 2015年 Zoujie. All rights reserved. // /** * 多态,动态类型,动态绑定 * 使不同的类共享相同的方法名称的能力称为多态 */ #import <Foundation/Foundation.h> @interface Complex : NSObject @property double real,imaginary; -(void)print; -(void)setReal:(double)a andImaginary:(double)b; -(Complex *) add:(Complex *)f; @end
// // Complex.m // OC7类 // // Created by Zoujie on 15/8/30. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "Complex.h" @implementation Complex @synthesize real,imaginary; -(void)print { NSLog(@"%g + %gi",real,imaginary); } -(void) setReal:(double)a andImaginary:(double)b { real = a ; imaginary = b; } -(Complex *)add:(Complex *)f { Complex *result = [[Complex alloc]init]; result.real = f.real+real; result.imaginary = f.imaginary + imaginary; return result; } @end
标签:
原文地址:http://my.oschina.net/u/2319073/blog/499302