标签:
/*
1.设计2个类,类之间的关系自拟(比如继承、组合)
1> 车
(1)属性
* 轮子数
* 速度
(2)方法
* 属性相应的set和get方法
2> 客车
(1)属性
* 轮子数
* 速度
* 座位数
(2)方法
* 属性相应的set和get方法
*/
// 客车 是一种 车,因此用继承关系
#import <Foundation/Foundation.h>
// 车
@interface Car : NSObject
{
int _wheels; // 轮子数
int _speed; // 速度
}
// 速度的getter和setter
- (int)speed;
- (void)setSpeed:(int)speed;
// 轮子数的getter和setter
- (int)wheels;
- (void)setWheels:(int)wheels;
@end
@implementation Car
// 速度的getter和setter
- (int)speed
{
return _speed;
}
- (void)setSpeed:(int)speed
{
_speed = speed;
}
// 轮子数的getter和setter
- (int)wheels
{
return _wheels;
}
- (void)setWheels:(int)wheels
{
_wheels = wheels;
}
@end
// 客车
@interface Bus : Car
{
int _seats; // 座位数
}
// 座位数的getter和setter
- (int)seats;
- (void)setSeats:(int)seats;
@end
@implementation Bus
// 座位数的getter和setter
- (int)seats
{
return _seats;
}
- (void)setSeats:(int)seats
{
_seats = seats;
}
@end
/*
2.设计2个类,类之间的关系自拟(比如继承、组合)
1> 身材数据
(1)属性
* 身高
* 体重
* 手长
* 脚长
(2)方法
* 属性相应的set和get方法
2> 人
(1)属性
* 年龄
* 身高
* 体重
* 手长
* 脚长
(2)方法
* 属性相应的set和get方法
*/
// 人 拥有一份 身材数据 ,所以用组合关系
// 身材数据
@interface BodyData : NSObject
{
int _height; // 身高
int _weight; //体重
int _handLength; // 手长
int _legLength; // 腿长
}
// 身高的getter和setter
- (int)height;
- (void)setHeight:(int)height;
// 体重的getter和setter
- (int)weight;
- (void)setWeight:(int)weight;
// 手长的getter和setter
- (int)handLength;
- (void)setHandLength:(int)handLength;
// 腿长的getter和setter
- (int)legLength;
- (void)setLegLength:(int)legLength;
@end
@implementation BodyData
// 身高的getter和setter
- (int)height
{
return _height;
}
- (void)setHeight:(int)height
{
_height = height;
}
// 体重的getter和setter
- (int)weight
{
return _weight;
}
- (void)setWeight:(int)weight
{
_weight = weight;
}
// 手长的getter和setter
- (int)handLength
{
return _handLength;
}
- (void)setHandLength:(int)handLength
{
_handLength = handLength;
}
// 腿长的getter和setter
- (int)legLength
{
return _legLength;
}
- (void)setLegLength:(int)legLength
{
_legLength = legLength;
}
@end
// 人
@interface Person : NSObject
{
int _age; // 年龄
BodyData *_bodyData; // 身材数据
}
// _age的setter和getter
- (void)setAge:(int)age;
- (int)age;
// _bodyData的setter和getter
- (void)setBodyData:(BodyData *)bodyData;
- (BodyData *)bodyData;
@end
//实现
@implementation Person
// _age的setter和getter
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
// _bodyData的setter和getter
- (void)setBodyData:(BodyData *)bodyData
{
_bodyData = bodyData;
}
- (BodyData *)bodyData
{
return _bodyData;
}
@end
int main()
{
Person *p = [Person new];
// 设置年龄
[p setAge:20];
// 设置身材数据
BodyData *b = [BodyData new];
[b setWeight:60];
[b setHeight:170];
[p setBodyData:b];
return 0;
}
/*
3.设计3个类,类之间的关系自拟(比如继承、组合)
1> 人
(1)属性
* 姓名
* 年龄
(2)方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置姓名和年龄
2> 书
(1)属性
* 书名
* 出版社名称
* 作者(包含姓名和年龄)
(2)方法
* 属性相应的set和get方法
3> 学生
* 姓名
* 年龄
* 学号
* 书(随身带着一本书)
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法-study:输出书名
*/
// 书 拥有一个 作者 --> 组合
// 学生 是一个 人 --> 继承
// 学生 拥有一本 书 --> 组合
#import <Foundation/Foundation.h>
//人
@interface Person : NSObject
{
NSString *_name; // 姓名
int _age; // 年龄
}
// 姓名的getter和setter
- (void)setName:(NSString *)name;
- (NSString *)name;
// 年龄的getter和setter
- (void)setAge:(int)age;
- (int)age;
// 同时设置姓名和年龄
- (void)setName:(NSString *)name andAge:(int)age;
@end
@implementation Person
// 姓名的getter和setter
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
// 年龄的getter和setter
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
// 同时设置姓名和年龄
- (void)setName:(NSString *)name andAge:(int)age
{
_name = name;
_age = age;
/*
[self setName:name];
[self setAge:age];
*/
}
@end
// 书
@interface Book : NSObject
{
NSString *_name; // 书名
NSString *_publisher; // 出版社名称
Person *_author; // 作者
}
// 书名的getter和setter
- (void)setName:(NSString *)name;
- (NSString *)name;
// 出版社名称的getter和setter
- (void)setPublisher:(NSString *)publisher;
- (NSString *)publisher;
// 作者的getter和setter
- (void)setAuthor:(Person *)author;
- (Person *)author;
@end
@implementation Book
// 书名的getter和setter
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
// 出版社名称的getter和setter
- (void)setPublisher:(NSString *)publisher
{
_publisher = publisher;
}
- (NSString *)publisher
{
return _publisher;
}
// 作者的getter和setter
- (void)setAuthor:(Person *)author
{
_author = author;
}
- (Person *)author
{
return _author;
}
@end
// 学生
@interface Student : Person
{
int _no; //学号
Book *_book; //书
}
// 学号的getter和setter
- (void)setNo:(int)no;
- (int)no;
// 书的getter和setter
- (void)setBook:(Book *)book;
- (Book *)book;
// 学习
- (void)study;
@end
@implementation Student
// 学号的getter和setter
- (void)setNo:(int)no
{
_no = no;
}
- (int)no
{
return _no;
}
// 书的getter和setter
- (void)setBook:(Book *)book
{
_book = book;
}
- (Book *)book
{
return _book;
}
// 学习
- (void)study
{
NSLog(@"现在学习的书是:%@", [_book name]);
}
@end
/**
4.设计Car类
1> 属性
* 速度
2> 方法
* 属性相应的set和get方法
* 一个对象方法跟其他车子比较车速,返回速度差
* 一个类方法比较两辆车的车速,返回速度差
*/
#import <Foundation/Foundation.h>
// 车
@interface Car : NSObject
{
int _speed; // 速度
}
// 速度的getter和setter
- (void)setSpeed:(int)speed;
- (int)speed;
// 跟其他车子比较车速,返回速度差
- (int)compareSpeedWithOther:(Car *)car;
// 比较两辆车的车速,返回速度差
+ (int)compareSpeedBetweenCar1:(Car *)car1 andCar2:(Car *)car2;
@end
@implementation Car
// 速度的getter和setter
- (void)setSpeed:(int)speed
{
_speed = speed;
}
- (int)speed
{
return _speed;
}
// 跟其他车子比较车速,返回速度差
- (int)compareSpeedWithOther:(Car *)car
{
// 第1种思路
// return _speed - [car speed];
// 第2种思路
return [Car compareSpeedBetweenCar1:self andCar2:car];
}
// 比较两辆车的车速,返回速度差
+ (int)compareSpeedBetweenCar1:(Car *)car1 andCar2:(Car *)car2
{
return [car1 speed] - [car2 speed];
}
@end
/**
5.设计一个类Point2D,用来表示二维平面中某个点
1> 属性
* double x
* double y
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置x和y
* 设计一个对象方法计算跟其他点的距离
* 设计一个类方法计算两个点之间的距离
3> 提示
* C语言的math.h中有个函数:double pow(double n, double m); 计算n的m次方
* C语言的math.h中有个函数:double sqrt(double n); 计算根号n的值(对n进行开根)
*/
#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种思路
[self setX:x];
[self setY: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
int main()
{
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:10];
Point2D *p2 = [Point2D new];
[p2 setX:13 andY:14];
double d1 = [p1 distanceWithOther:p2];
double d2 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
NSLog(@"d1=%f, d2=%f", d1, d2);
return 0;
}
/**
6.设计一个类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种思路
[self setX:x];
[self setY: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;
}
标签:
原文地址:http://www.cnblogs.com/huimotuo/p/4459820.html