设计2个类,类之间的关系自拟(比如继承、组合)
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
int _wheels;
int _speed;
}
- (int)speed;
- (void)setSpeed:(int)speed;
- (int)wheels;
- (void)setWheels:(int)wheels;
@end
@implementation Car
- (int)speed
{
return _speed;
}
- (void)setSpeed:(int)speed
{
_speed = speed;
}
- (int)wheels
{
return _wheels;
}
- (void)setWheels:(int)wheels
{
_wheels = wheels;
}
@end
@interface Bus : Car
{
int _seats;
}
- (int)seats;
- (void)setSeats:(int)seats;
@end
@implementation Bus
- (int)seats
{
return _seats;
}
- (void)setSeats:(int)seats
{
_seats = seats;
}
@end
@interface BodyData : NSObject
{
int _height;
int _weight;
int _handLength;
int _legLength;
}
- (int)height;
- (void)setHeight:(int)height;
- (int)weight;
- (void)setWeight:(int)weight;
- (int)handLength;
- (void)setHandLength:(int)handLength;
- (int)legLength;
- (void)setLegLength:(int)legLength;
@end
@implementation BodyData
- (int)height
{
return _height;
}
- (void)setHeight:(int)height
{
_height = height;
}
- (int)weight
{
return _weight;
}
- (void)setWeight:(int)weight
{
_weight = weight;
}
- (int)handLength
{
return _handLength;
}
- (void)setHandLength:(int)handLength
{
_handLength = handLength;
}
- (int)legLength
{
return _legLength;
}
- (void)setLegLength:(int)legLength
{
_legLength = legLength;
}
@end
@interface Person : NSObject
{
int _age;
BodyData *_bodyData;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setBodyData:(BodyData *)bodyData;
- (BodyData *)bodyData;
@end
@implementation Person
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
- (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个类,类之间的关系自拟(比如继承、组合)
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
int _age;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setAge:(int)age;
- (int)age;
- (void)setName:(NSString *)name andAge:(int)age;
@end
@implementation Person
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
- (void)setName:(NSString *)name andAge:(int)age
{
_name = name;
_age = age;
}
@end
@interface Book : NSObject
{
NSString *_name;
NSString *_publisher;
Person *_author;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setPublisher:(NSString *)publisher;
- (NSString *)publisher;
- (void)setAuthor:(Person *)author;
- (Person *)author;
@end
@implementation Book
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setPublisher:(NSString *)publisher
{
_publisher = publisher;
}
- (NSString *)publisher
{
return _publisher;
}
- (void)setAuthor:(Person *)author
{
_author = author;
}
- (Person *)author
{
return _author;
}
@end
@interface Student : Person
{
int _no;
Book *_book;
}
- (void)setNo:(int)no;
- (int)no;
- (void)setBook:(Book *)book;
- (Book *)book;
- (void)study;
@end
@implementation Student
- (void)setNo:(int)no
{
_no = no;
}
- (int)no
{
return _no;
}
- (void)setBook:(Book *)book
{
_book = book;
}
- (Book *)book
{
return _book;
}
- (void)study
{
NSLog(@"现在学习的书是:%@", [_book name]);
}
@end
设计Car类
/**
4.设计Car类
1> 属性
* 速度
2> 方法
* 属性相应的set和get方法
* 一个对象方法跟其他车子比较车速,返回速度差
* 一个类方法比较两辆车的车速,返回速度差
*/
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
int _speed;
}
- (void)setSpeed:(int)speed;
- (int)speed;
- (int)compareSpeedWithOther:(Car *)car;
+ (int)compareSpeedBetweenCar1:(Car *)car1 andCar2:(Car *)car2;
@end
@implementation Car
- (void)setSpeed:(int)speed
{
_speed = speed;
}
- (int)speed
{
return _speed;
}
- (int)compareSpeedWithOther:(Car *)car
{
return [Car compareSpeedBetweenCar1:self andCar2:car];
}
+ (int)compareSpeedBetweenCar1:(Car *)car1 andCar2:(Car *)car2
{
return [car1 speed] - [car2 speed];
}
@end
设计一个类Point2D,用来表示二维平面中某个点
/**
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;
double _y;
}
- (void)setX:(double)x;
- (double)x;
- (void)setY:(double)y;
- (double)y;
- (void)setX:(double)x andY:(double)y;
- (double)distanceWithOther:(Point2D *)other;
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end
@implementation Point2D
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
- (void)setX:(double)x andY:(double)y
{
[self setX:x];
[self setY:y];
}
- (double)distanceWithOther:(Point2D *)other
{
return [Point2D distanceBetweenPoint1:self andPoint2:other];
}
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
double xDelta = [p1 x] - [p2 x];
double xDeltaPingFang = pow(xDelta, 2);
double yDelta = [p1 y] - [p2 y];
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;
}
设计一个类Circle,用来表示二维平面中的圆
#import <Foundation/Foundation.h>
#import <math.h>
@interface Point2D : NSObject
{
double _x;
double _y;
}
- (void)setX:(double)x;
- (double)x;
- (void)setY:(double)y;
- (double)y;
- (void)setX:(double)x andY:(double)y;
- (double)distanceWithOther:(Point2D *)other;
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end
@implementation Point2D
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
- (void)setX:(double)x andY:(double)y
{
[self setX:x];
[self setY:y];
}
- (double)distanceWithOther:(Point2D *)other
{
return [Point2D distanceBetweenPoint1:self andPoint2:other];
}
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
double xDelta = [p1 x] - [p2 x];
double xDeltaPingFang = pow(xDelta, 2);
double yDelta = [p1 y] - [p2 y];
double yDeltaPingFang = pow(yDelta, 2);
return sqrt(xDeltaPingFang + yDeltaPingFang);
}
@end
@interface Circle : NSObject
{
double _radius;
Point2D *_point;
}
- (void)setRadius:(double)radius;
- (double)radius;
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
- (BOOL)isInteractWithOther:(Circle *)other;
+ (BOOL)isInteractBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2;
@end
@implementation Circle
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
- (void)setPoint:(Point2D *)point
{
_point = point;
}
- (Point2D *)point
{
return _point;
}
- (BOOL)isInteractWithOther:(Circle *)other
{
return [Circle isInteractBetweenCircle1:self andCircle2:other];
}
+ (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];
BOOL b1 = [c1 isInteractWithOther:c2];
BOOL b2 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];
NSLog(@"%d %d", b1, b2);
return 0;
}