熟悉oc面向对象的小例子
2015-09-16 22:47
447 查看
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;
}
//
// 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
相关文章推荐
- Lua编程示例(二):面向对象、metatable对表进行扩展
- C#中面向对象编程机制之多态学习笔记
- 浅谈Lua的面向对象特性
- Lua面向对象之类和继承浅析
- JavaScript面向对象的两种书写方法以及差别
- 浅谈c# 面向对象之类与对象
- C#面向对象特征的具体实现及作用详解
- C# 面向对象的基本原则
- 浅谈对c# 面向对象的理解
- Ruby面向对象编程详解
- php学习 面向对象 课件第1/2页
- PHP程序61条面向对象分析设计的经验小结
- 收集学习asp.net比较完整的面向对象开发流程
- javascript 面向对象编程 万物皆对象
- 不错的JavaScript面向对象的简单入门介绍第1/2页
- [推荐]javascript 面向对象技术基础教程
- javascript 面向对象的JavaScript类
- JavaScript中的面向对象介绍
- javascript实现面向对象类的功能书写技巧
- Javascript 面向对象 继承