【Objective-C】类方法与对象方法

一、方法

1. 基本概念:用类名来调用的方法叫做类方法;

2. 方法是类的行为,写在接口和实现两个文件中。在接口部分声明方法,在实现部分实现方法

  1)类方法与实例方法

   Objective-C中的类可以声明两种类型的方法:实例方法和类方法。实例方法就是一个方法,它在类的一个具体实例的范围内执行。也就是说,在你调用一个实例方法前,你必须首先创建类的一个实例。而类方法,比较起来,也就是说,不需要你创建一个实例。

   + 表示类方法 (静态方法),就是类调用方法,不依赖与任何对象的方法,类似于c语言的static关键字 static函数。

   - 表示是实例(对象)方法 (动态方法),只能是对象可调用,依赖与任何对象的方法。

  总结一下几点:

  1)类(static)方法(便利构造器)

     a. 类方法的调用

         [类名称 类方法名称];

     这里需要注意

     1)类方法可以调用类方法;

     2) 类方法不可以调用实例方法,但是类方法可以通过创建对象来访问实例方法;

     3)类方法不可以使用实例变量(成员变量)。类方法可以使用self,因为self不是实例变量;

     4)类方法作为消息,可以被发送到类或者对象里面去(实际上,就是可以通过类或者对象调用类方法的意思);

  2)实例方法

    b.实例方法的调用

     首先需要实例化该类

     例如:Human *man = [Human alloc] init];

     [类的实例  实例方法名称];

     例如:[man showSex];

     注意:此处实例化该类时,调用了该类的构造函数init,并且该类的构造函数调用[super init]的返回值不等于该类的self。

     定义子类的实例

     Woman *wife = [Woman alloc] init];

     此处实例化该类时,调用了该类的构造函数init,并且该类的构造函数调用[super init]的返回值 等于该类的self。

二、 类方法好处

1. 不依赖于对象,执行效率高;(由于直接用类名调用)

2.  能用类方法尽量使用

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

案例:

.h

/**
 * 内容:设计一个类Point2D,用来计算点与点之间的距离
 * 作用:熟悉oc与C语言的结合使用,以及类方法与实例方法的区别
 * 步骤:
 * 1> 属性:
 *    double radius;  (radius)
 *    Point2D *point; (Center of a circle)
 
 * 2> 方法
 *    属性响应的set、get方法
 *    设计一个对象方法同时设置x和y 
 *    设计一个对象方法判断跟其他点的距离
 *    设计一个类方法判断两个点的距离
 
 *    设计一个对象方法判断跟其他的圆是否相交(相交返回YES,否则返回NO)
 *    设计一个类方法判断跟其他的圆是否相交(相交返回YES,否则返回NO)
 
 * 3> 提示
 *    C语言的math.h中有一个函数double pow (double a, double b),计算a 的 b次方
 *    C语言的math.h中有一个函数double sqrt (double n); 计算根号n的值
 */

#import <Foundation/Foundation.h>
// import file
#import <math.h>

@interface Point2D : NSObject {
    double _x;
    double _y;
    
}

// Declaration method
// 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)distanceWithOtherPoint:(Point2D *)otherPoint;

// 计算两个点之间的距离(类方法)
+ (double)distanceBetweenPoint:(Point2D *)point1 andPoint2:(Point2D *)point2;

@end
.m
#import "Point2D.h"

@implementation Point2D

// Realization method X
- (void)setX:(double)x {
    _x = x;
    
}
- (double)x {
    return _x;
    
}


// Realization method Y
- (void)setY:(double)y {
    _y = y;
    
}
- (double)y {
    return  _y;
    
}

// Realization method Y and X
- (void)setX:(double)x andY:(double)y {
    self.x = x;
    self.y = y;
    
}

// Realization method distance
- (double)distanceWithOtherPoint:(Point2D *)otherPoint {
    // 计算距离其实就是平方和开根
    // 巧妙运用两个方法的相似之处就是计算两点之间的距离
    return [Point2D distanceBetweenPoint:self andPoint2:otherPoint];
}

// Realization method distance point to point
+ (double)distanceBetweenPoint:(Point2D *)point1 andPoint2:(Point2D *)point2 {
    // x1 - x2   y1 - y2
    double xDelta = point1.x - point2.x;
    double yDelta = point1.y - point2.y;
    
    // (x1 - x2)的平方   (y1 - y2)的平方
    double xDeltaPF = pow(xDelta, 2);
    double yDeltaPF = pow(yDelta, 2);
    
    double dis = sqrt(xDeltaPF + yDeltaPF);
    
    return dis;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Point2D.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Point2D *p1 = [[Point2D alloc] init];
        [p1 setX:0 andY:0];
        
        Point2D *p2 = [[Point2D alloc] init];
        [p2 setX:1 andY:1];
        
        double dis1 = [p1 distanceWithOtherPoint:p2];   // 注意方法的调用(类的实例调用)
        NSLog(@"两个点的距离是(对象方法): %f", dis1);
        
        double dis2 = [Point2D distanceBetweenPoint:p1 andPoint2:p2];  //注意方法的调用(类名调用)
        NSLog(@"两个点之间的距离是(类方法): %f", dis2);
        
    }
    return 0;
}

可以参考文章对oc类加以了解:http://blog.csdn.net/haojie2014/article/details/46873263


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值