熟悉oc面向对象的小例子

OC面向对象小例子:

//
//  main.m
//  oc-面向对象小例子
//
//  Created by stevenchang on 9/16/15.
//  Copyright (c) 2015 cz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Point2D.h"
#import "Circle.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
//        NSLog(@"Hello, World!");
        Point2D *point1 = [[Point2D alloc] init];
        [point1 setX:3];
        [point1 setY:4];
        
        Point2D *point2 = [[Point2D alloc] init];
        [point2 setX:0];
        [point2 setY:0];
        
        double distince = [point1 distinceWithOtherPoint:point2];
        
        NSLog(@"两点之间的距离为:%f",distince);
        
        Point2D *point3 = [[Point2D alloc] init];
        [point3 setX:3 andY:4];
        Point2D *point4 = [[Point2D alloc] init];
        [point4 setX:-1 andY:-1];
        
        double distince1 = [Point2D distinceBetweenPoint:point3 andOtherPoint:point4];
        NSLog(@"两点之间的距离为:%f", distince1);
        
        NSLog(@"========================================");
        Circle *circle = [[Circle alloc] init];
        [circle setRadius:5];
        Point2D *point5 = [[Point2D alloc] init];
        [point5 setX:10 andY:19];
        [circle setPoint:point5];
        
        Circle *circle1 = [[Circle alloc] init];
        [circle1 setRadius:4];
        Point2D *point6 = [[Point2D alloc] init];
        [point6 setX:13 andY:14];
        [circle1 setPoint:point6];
//        Boolean flag = [circle isInteractWithOtherCircle:circle1];
        Boolean flag = [Circle isInteractBewteenCircle:circle andOtherCircle:circle1];
        if (flag) {
            NSLog(@"相交");
        } else {
            NSLog(@"不相交");
        }
        
    }
    return 0;
}

//
//  Point2D.h
//  oc-面向对象小例子
//
//  Created by stevenchang on 9/16/15.
//  Copyright (c) 2015 cz. All rights reserved.
//

//import应用Foundation
#import <Foundation/Foundation.h>

//声明一个Point2D类   : 表示继承
@interface Point2D : NSObject
{
    double _x;    // x坐标
    double _y;    // y坐标
}

//x getter & setter method
- (void)setX:(double)x;
- (double)x;

//y getter & setter method
- (void)setY:(double)y;
- (double)y;

//同时设置x&y
- (void)setX:(double)x andY:(double)y;    

//计算到其他点之间的距离
- (double)distinceWithOtherPoint:(Point2D *)other;

//计算两个点之间的距离
+ (double)distinceBetweenPoint:(Point2D *)point1 andOtherPoint:(Point2D *)point2;

@end

//
//  Point2D.m
//  oc-面向对象小例子
//
//  Created by stevenchang on 9/16/15.
//  Copyright (c) 2015 cz. All rights reserved.
//

#import "Point2D.h"
#import <math.h>

@implementation Point2D

//x getter & setter mehtod
- (void)setX:(double)x {
    _x = x;
}
- (double)x {
    return _x;
}

//y getter & setter method
- (void)setY:(double)y {
    _y = y;
}
- (double)y {
    return _y;
}

//同时设置x&y
- (void)setX:(double)x andY:(double)y {
    [self setX:x];
    [self setY:y];
}

//计算到其他点之间的距离
- (double)distinceWithOtherPoint:(Point2D *)other {
    double xData = self->_x - other->_x;
    double yData = self->_y - other->_y;
    
    return sqrt(pow(xData, 2) + pow(yData, 2));
}

//计算两个点之间的距离
+ (double)distinceBetweenPoint:(Point2D *)point1 andOtherPoint:(Point2D *)point2 {
    return [point1 distinceWithOtherPoint:point2];
}



@end

//
//  Circle.h
//  oc-面向对象小例子
//
//  Created by stevenchang on 9/16/15.
//  Copyright (c) 2015 cz. All rights reserved.
//

//import引用Foundation
#import <Foundation/Foundation.h>
#import "Point2D.h"

//声明一个Circle类  : 表示圆心
@interface Circle : NSObject
{
    double _radius;    // 半径
    Point2D *_point;   // 圆心
}

// radius getter & setter
- (void)setRadius:(double)radius;
- (double)radius;

//point getter & setter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;

//判断同另一个圆是否相交
- (BOOL)isInteractWithOtherCircle:(Circle *)other;

//判断两个圆是否相交
+ (BOOL)isInteractBewteenCircle:(Circle *)circle1 andOtherCircle:(Circle *)other;

@end

//
//  Circle.m
//  oc-面向对象小例子
//
//  Created by stevenchang on 9/16/15.
//  Copyright (c) 2015 cz. All rights reserved.
//

#import "Circle.h"

@implementation Circle

//radius getter & setter
- (void)setRadius:(double)radius {
    _radius = radius;
}
- (double)radius {
    return _radius;
}

//point getter & setter
- (void)setPoint:(Point2D *)point {
    _point = point;
}
- (Point2D *)point {
    return _point;
}

//判断同另一个圆是否相交
- (BOOL)isInteractWithOtherCircle:(Circle *)other {
    //半径之和
    double totalRadius = [self radius] + [other radius];
    
    //圆心距离
    double distince = [[self point] distinceWithOtherPoint:[other point]];
    return distince < totalRadius;  //YES:相交   NO:不想交
}

//判断两个圆是否相交
+ (BOOL)isInteractBewteenCircle:(Circle *)circle1 andOtherCircle:(Circle *)other {
    return [circle1 isInteractWithOtherCircle:other];
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值