iOS笔记之_OC面向对象语法2

来源:互联网 发布:磁盘元数据测试0x32 编辑:程序博客网 时间:2024/06/05 00:47


一、Self关键字

self的用途:

 1> 谁调用了当前方法,self就代表谁

 *self出现在对象方法中,self就代表对象

 *self出现在类方法中,self就代表类

 2> 在对象方法利用"self->成员变量名"访问当前对象内部的成员变量

 3>[self 方法名]可以调用其他对象方法\类方法

#import <Foundation/Foundation.h>@interface Dog : NSObject- (void)bark;- (void)run;@end @implementation Dog- (void)bark{   NSLog(@"汪汪汪");}- (void)run{   [self bark];   //NSLog(@"汪汪汪");   NSLog(@"跑跑跑");}@end int main(){   Dog *d = [Dog new];      [d run];      return 0;}

常见错误:

低级错误:用self去调用函数。

类方法中用self调用对象方法,对象方法中用self调用类方法。

self死循环。

二、继承

1.继承的好处:

 1> 抽取重复代码

 2> 建立了类之间的关系

 3> 子类可以拥有父类中的所有成员变量和方法

 

 2.注意点

基本上所有类的根类是NSObject 。

子类方法和属性的访问过程:如果子类没有,就去访问父类的。

父类被继承了还是能照常使用的。

父类的静态方法。

画继承结构图,从子类抽取到父类。

NSObject的引出:全部OC类的最终父类,包含了一些常用方法,比如+new。

子类和父类不能有相同的成员变量

 

3.代码示例:

#import <Foundation/Foundation.h>/********Animal的声明*******/@interface Animal : NSObject{   int _age;   double _weight;} - (void)setAge:(int)age;- (int)age; - (void)setWeight:(double)weight;- (double)weight;@end /********Animal的实现*******/@implementation Animal- (void)setAge:(int)age{   _age = age;}- (int)age{   return _age;} - (void)setWeight:(double)weight{   _weight = weight;}- (double)weight{   return _weight;}@end /********Dog*******/// : Animal 继承了Animal,相当于拥有了Animal里面的所有成员变量和方法// Animal称为Dog的父类// Dog称为Animal的子类@interface Dog : Animal@end @implementation Dog@end /********Cat*******/@interface Cat : Animal@end @implementation Cat@end int main(){   Dog *d = [Dog new];      [d setAge:10];      NSLog(@"age=%d", [d age]);   return 0;}


4.使用场合

它的所有属性都是你想要的,一般就继承

它的部分属性是你想要的,可以抽取出另一个父类

5. .重写:子类重新实现父类中的某个方法,覆盖父类以前的做法

 注意

 1> 父类必须声明在子类的前面

 2> 子类不能拥有和父类相同的成员变量

 3> 调用某个方法时,优先去当前类中找,如果找不到,去父类中找

6.组合

当在一个类中需要另一个类中的属性时可以考虑组合。

#import <Foundation/Foundation.h>@interface Score : NSObject{   int _cScore;   int _ocScore;}@end @implementation Score@end @interface Student : NSObject{   // 组合   Score *_score;//   int _cScore;//   int _ocScore;   int _age;}@end

三、多态

1.没有继承就没有多态。

 2.代码的体现:父类类型的指针指向子类对象。

 3.好处:如果函数\方法参数中使用的是父类类型,可以传入父类、子类对象。

 4.局限性:

  父类类型的变量不能 直接调用子类特有的方法。必须强转为子类类型变量后,才能直接调用子类特有的方法

四、面向对象的综合练习

(1).设计一个类Point2D,用来表示二维平面中某个点

1> 属性

* double x

* double y

 

2> 方法

* 属性相应的set和get方法

* 设计一个对象方法同时设置x和y

* 设计一个对象方法计算跟其他点的距离

* 设计一个类方法计算两个点之间的距离

 

3> 提示

* C语言的math.h中有个函数:doublepow(double n, double m); 计算n的m次方

* C语言的math.h中有个函数:doublesqrt(double n); 计算根号n的值(对n进行开根)

 

(2)设计一个类Circle,用来表示二维平面中的圆

1> 属性

* double _radius (半径)

* Point2D *_point (圆心)

 

2> 方法

* 属性相应的set和get方法

* 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO)

* 设计一个类方法判断两个圆是否重叠(重叠返回YES,否则返回NO)

程序代码:

#import <Foundation/Foundation.h>#import <math.h> // 点@interface Point2D : NSObject{   double _x; // x值   double _y; // y值}// x值的getter和setter- (void)setX:(double)x;- (double)x; // y值的getter和setter- (void)setY:(double)y;- (double)y; // 同时设置x和y- (void)setX:(double)x andY:(double)y; // 计算跟其他点的距离- (double)distanceWithOther:(Point2D*)other; // 计算两个点之间的距离+ (double)distanceBetweenPoint1:(Point2D*)p1 andPoint2:(Point2D *)p2;@end @implementation Point2D// x值的getter和setter- (void)setX:(double)x{   _x = x;}- (double)x{   return _x;} // y值的getter和setter- (void)setY:(double)y{   _y = y;}- (double)y{   return _y;} // 同时设置x和y- (void)setX:(double)x andY:(double)y{   // 第1种思路   // _x = x;   // _y = y;      // 第2种思路         [selfsetX:x];         [selfsetY:y];} // 计算跟其他点的距离- (double)distanceWithOther:(Point2D*)other{   // 直接调用类方法即可   return [Point2D distanceBetweenPoint1:self andPoint2:other];} // 计算两个点之间的距离+ (double)distanceBetweenPoint1:(Point2D*)p1 andPoint2:(Point2D *)p2{   // 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根      // x1-x2   double xDelta = [p1 x] - [p2 x];   // (x1-x2)的平方   double xDeltaPingFang = pow(xDelta, 2);      // y1-y2   double yDelta = [p1 y] - [p2 y];   // (y1-y2)的平方   double yDeltaPingFang = pow(yDelta, 2);      return sqrt(xDeltaPingFang + yDeltaPingFang);}@end // 圆@interface Circle : NSObject{   double _radius; // 半径   Point2D *_point; // 圆心} // 半径的getter和setter- (void)setRadius:(double)radius;- (double)radius; // 圆心的getter和setter- (void)setPoint:(Point2D *)point;- (Point2D *)point;  // 跟其他圆是否重叠(重叠返回YES,否则返回NO)- (BOOL)isInteractWithOther:(Circle*)other;// 判断两个圆是否重叠(重叠返回YES,否则返回NO)+ (BOOL)isInteractBetweenCircle1:(Circle*)circle1 andCircle2:(Circle *)circle2; @end @implementation Circle// 半径的getter和setter- (void)setRadius:(double)radius{   _radius = radius;}- (double)radius{   return _radius;} // 圆心的getter和setter- (void)setPoint:(Point2D *)point{   _point = point;}- (Point2D *)point{   return _point;} // 跟其他圆是否重叠(重叠返回YES,否则返回NO)- (BOOL)isInteractWithOther:(Circle *)other{   return [Circle isInteractBetweenCircle1:self andCircle2:other];} // 判断两个圆是否重叠(重叠返回YES,否则返回NO)+ (BOOL)isInteractBetweenCircle1:(Circle*)circle1 andCircle2:(Circle *)circle2{   // 如果两个圆心的距离 >= 两个圆的半径和,就不重叠   // 如果两个圆心的距离 < 两个圆的半径和,就重叠      // 两个圆心   Point2D *point1 = [circle1 point];   Point2D *point2 = [circle2 point];   // 两个圆心的距离   double distance = [point1 distanceWithOther:point2];      // 半径和   double radiusSum = [circle1 radius] + [circle2 radius];      return distance < radiusSum;}@end int main(){   Circle *c1 = [Circle new];   // 设置半径   [c1 setRadius:2];   // 设置圆心   Point2D *p1 = [Point2D new];   [p1 setX:10 andY:10];   [c1 setPoint:p1];      Circle *c2 = [Circle new];   // 设置半径   [c2 setRadius:2];   // 设置圆心   Point2D *p2 = [Point2D new];   [p2 setX:13 andY:14];   [c2 setPoint:p2];      // 圆心距离是5  半径和是4  所以不重叠   BOOL b1 = [c1 isInteractWithOther:c2];      BOOL b2 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];      NSLog(@"%d %d", b1, b2);      return 0;}

程序输出结果为:0 0

 

 

0 0
原创粉丝点击