面向对象三大特性

来源:互联网 发布:网络用语古是什么意思 编辑:程序博客网 时间:2024/05/24 07:21
一:封装
set方法:
1.作用:提供一个方法给外界设置成员变量值,可以在方法里面对参数进行相应的过滤
2.命名规范:
方法名必须以set开头
set后面跟上成员变量的名称,成员变量的首字母必须大写
返回值一定是void
一定要接收一个参数,而且参数类型跟成员变量类型一致
形参的名称不能跟成员变量名称一样
get方法:
1.作用:返回对象内部的成员变量
2.命名规范:
一定有返回值,返回值类型与成员变量类型一致
方法名跟成员变量名一样
不需要接受任何参数

 例如:
#import<Foundation/Foundation.h>

@interface Student : NSObject

{

    //成员变量不能使用@public

    int _age;//年龄

    int _no;//学号,只允许外界访问我的no,不允许外界修改我的no,所以只提供get方法

}

- (void)setAge:(int)newAge;

- (int)age;

- (int)no;

- (void)study;

@end


@implementation Student

- (void)setAge:(int)age

{

   //对传进来才参数进行过滤

    if(age<=0)

    {

        age=1;

    }

    _age=age;

}

- (int)age

{

    return _age;

}

- (int)no

{

    return _no;

}

- (void)study

{

    NSLog(@"%d岁的学生在学习,_age);

}


@end


int main()

{

    Student *stu = [Student new];

    [stu setAge:10];

    NSLog(@"学生的年龄是%d",[stu age]);

    return 0;

}

   封装的细节:

    成员变量的命名规范:一定要以_下划线开头

    作用:

1.让成员变量和get方法的名称区分开来

2.可以跟局部变量区分开,一看到下划线开头的变量,一般都是成员变量


   对象方法:

        1.对象方法只能以减号 - 开头

2.对象方法只能由对象来进行调用

3.对象方法中能访问当前对象的成员变量(实例变量)

    类方法:

1.类方法以+号开头

2.只能由类名来进行调用

3.类方法不能访问成员变量(实例变量)

    类方法的好处和使用场合:

        1.不依赖于对象,执行效率高

2.能用类的方法尽量使用类的方法,节省内存

3.场合,当方法内部不需要使用到成员变量的时候,就可以改为使用类方法

     4.可以允许对象方法和类方法同名

        self的用途:
             self概念:(相当于java中的this,this只能用在对象方法中,不能用在类方法中)
     1.指向了当前的对象,方法调用者,(谁调用这个方法,就是指向谁,self出现在类方法中就代表当前类,self出现在对象方法中就带表当前对象)
      2.在对象方法中可以利用“self->成员变量名”,访问当前对象内部的成员变量
      3.[self 方法名]可以调用其他对象方法和类方法
        self的使用注意:
              不要在本方法中使用self调用自己的方法,这样会引发程序死循环



二:继承
            好处:
1.抽取重复的代码
2.建立了类和类之间的关系
                3.子类可以拥有父类中的所有的成员变量和方法
           
         例子代码:

#import <Foundation/Foundation.h>

/*动物类的声明*/

@interface Animal : NSObject

{

    int _age;

    double _weight;

}


- (void)setAge:(int)age;

- (int)age;


- (void)setWeight:(double)weight;

- (double)weight;

@end

/*动物类的实现*/

@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里面的所有成员变量和方法*/

@interface Dog : Animal

@end


@implementation Dog

@end


/*Car类的声明和实现,继承于Animal,相当于拥有了Animal里面的所有成员变量和方法*/


@interface Car : Animal

@end


@implementation Car

@end


int main()

{

    Dog *d = [Dog new];

    [d setAge:10];

    

    NSLog(@"age=%d",[d age]);

    

    return 0;

}

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

  注意:
1.基本上所有类的根类是NSObject
2.父类的声明要放在子类的前面
3.OC中不允许子类和父类拥有相同名称的成员变量
4.调用某个方法的时候,优先去当前类中找,如果找不到,去父类中找。
          坏处:耦合性太强
 继承的使用场合:
1.当两个类拥有相同的属性和方法的时候就可以将相同的东西抽取到一个父类当中。
2.当A类完全拥有B类的属性和方法的时候,可以考虑让B类继承A类
3。当两个类使用继承关系不符合常理的时候,可以考虑将一个类组合到另外一个类中
          super关键字的使用:
                1.直接调用父类中的某个方法
2.super处在对象方法中,那么就会调用父类的对象方法。
3.super处在类方法中,那么就会调用对象的类方法
          super使用场合:
       子类重写父类的方法时候想保留父类的一些行为
三:多态
1.没有继承就没有多态,继承是多态的前提条件
2. 代码体现,父类类型的指针,指向子类的对象
3.好处:如果函数\方法参数中使用的时父类类型,可以传入父类和子类的对象
4.局限性:父类类型的变量不能直接调用子类特有的方法,必须强转为子类类型变量后才能直接调用子类特有的方法 
       多态的代码体现:

#import<Foundation/Foundation.h>

@interface Animal :NSObject

- (void)eat;

@end

@implementation Animal

- (void)eat

{

    NSLog(@"Animal--在吃东西");

}

@end


//


@interface Dog : Animal

-(void)run;

@end


@implementation Dog

- (void)run

{

    NSLog(@"Dog--跑起来");

}

- (void)eat

{

    NSLog(@"Dog --在吃东西--");

}

@end


@interface Cat : Animal



@end


@implementation Cat

-(void)eat

{

    NSLog(@"Cat-吃东西---");

}


@end

//喂动物

void feed(Animal *a)

{

    [a eat];

}


int main()

{

    Animal *aa = [Dog new];

    Dog *dd=(Dog *)aa;//aa强制转换为Dog类型

    [dd run];//这里会出现一个警告,因为Animal没有这个对象方法,需要将Animal转换为Dog类型

    [aa eat];//调用方法的时候会检测对象的真实形态,在调用对应的方法

    

    return 0;

}


多文件组合开发:


  每个类有两个文件,.h文件是包含这个类的所有声明,.m文件是包含这个类的所有视线,每个类的.m文件必须包含自己的.h文件,如果两个类是组合关系,组合类的.h文件中需要包含被组合的类的.h文件

  在main方法中需要包含所有类的声明(.h)文件

例如:

  Point2D.h:

   #import<Foundation/Foundation.h>


//Point2D类的声明

@interface Point2D : NSObject

{

    double _x;// 横坐标 x属性

    double _y;// 纵坐标 y属性

}


// xgeterseter方法

- (void)setX : (double)x;

- (double)x;


// ygeterseter方法

- (void)setY : (double)Y;

- (double)y;


// 设计一个对象方法同时设置xy

- (void)setX : (double)x andsetY :(double)y;


// 设置一个对象方法计算和其他点的距离

- (double)getdistance:(Point2D *)point2d;

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

+ (double)getdistance:(Point2D *)point2d andotherpoint2d : (Point2D *)otherpoint2d;

@end


Point2D.m:

#import "Point2D.h"

#import<math.h>


@implementation Point2D

// xgeterseter方法

- (void)setX : (double)x

{

    _x = x;

}

- (double)x

{

    return _x;

}


// ygeterseter方法

- (void)setY : (double)y

{

    _y = y;

}

- (double)y

{

    return _y;

}

// 设计一个对象方法同时设置xy

- (void)setX : (double)x andsetY :(double)y

{

    [self setX : x];

    [self setY : y];

}

// 设置一个对象方法计算和其他点的距离

- (double)getdistance:(Point2D *)point2d

{

     // 先计算两点x的距离

    double width=[self x]-[point2d x];

     // 在计算两点y的距离

    double height=[self y]-[point2d y];

     // 通过勾股定理算出两点的距离

    return sqrt(pow(width,2)+pow(height,2));

}

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

+ (double)getdistance:(Point2D *)point2d andotherpoint2d : (Point2D *)otherpoint2d;

{

    return [point2d getdistance : otherpoint2d];

}

@end


Circle.h:

#import<Foundation/Foundation.h>


#import "Point2D.h"

/**

 6.设计一个类Circle,用来表示二维平面中的圆

 1> 属性

 * double radius (半径)

 * Point2D *point (圆心)

 

 2> 方法

 * 属性相应的setget方法

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

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

 */


@interface Circle : NSObject

{

    double _radius; // 半径

    Point2D *_point; // 员心

}

// 半径的geterseter方法

- (void)setRadius : (double)radius;

- (double)radius;


//圆心的geterseter方法

- (void)setPoint : (Point2D *)point;

- (Point2D *)point;


//设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)判断对象是否一般以is开头

- (BOOL)isInteractWithOther: (Circle *)other;

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

+ (BOOL)isInteractBetweenCircle1 : (Circle *)circle1 andCircle2 : (Circle *)circle2;

@end


Circle.m:

#import "Circle.h"

@implementation Circle

// 半径的geterseter方法

- (void)setRadius : (double)radius

{

    _radius = radius;

}

-  (double)radius

{

    return _radius;

}


//圆心的geterseter方法

- (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 getdistance : point2];

    NSLog(@"%f",distance);

    // 半径和

    double radiusSum = [circle1 radius] + [circle2 radius];

    

    return distance < radiusSum;

}


@end

main.m:

#import<Foundation/Foundation.h>

#import "Point2D.h"

#import "Circle.h"

int main()

{

    Point2D *p=[Point2D new];

    [p setX : 15 andsetY : 15];

    Point2D *p2=[Point2D new];

    [p2 setX : 15 andsetY : 15];

    double width = [p getdistance : p2];

    double width2=[Point2D getdistance:p andotherpoint2d : p2];

    NSLog(@"点之间的距离%f,%f",width,width2);

    

    Circle *c=[Circle new];

    [c setPoint : p];

    [c setRadius : 5];

    

    Circle *c2=[Circle new];

    [c2 setPoint : p2];

    [c2 setRadius : 2];

    

    BOOL b = [c isInteractWithOther : c2];

    

    NSLog(@"%d",b);

}



0 0
原创粉丝点击