---------- CornerFly真诚期待与您交流! ----------
一、NSString基本使用
1、NSString是一个生成OC字符串对象的类
二、length和sizeof的区别
1、length方法算的是字符串的字数。
2、sizeof算的是字节数。
3、strlen算的是字符串的字符数。
三、代码示例
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
//char *name; 用指针来表示C语言字符串
NSString *_name;
}
@end
@implementation Person
@end
int main()
{
int age = 10;
int no = 5;
//NSString *name = @"jack"; // C语言中算是4个字符
NSString *name = @"哈哈jack"; // C语言中算是10个字符,用OC的length算的是6(字数)
// length 方法算的是字数
int size = [name length];
NSLog(@"%d", size);
// 创建OC字符串的另一种方式
NSString *newStr = [NSString stringWithFormat:@"My age is %d and no is %d and name is %@", age, no, name];
//NSLog(@"---%@", newStr);
NSLog(@"--%ld", [newStr length]); // 43个字数:包括空格
return 0;
}
四、作业讲解
1、设计一个类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 <math.h>
#import <Foundation/Foundation.h>
// 点
@interface Point2D : NSObject
{
double _x; // x值
double _y; // y值
}
// x值的setter和getter(x值的set方法和get方法)
- (void)setX:(double)x;
- (double)x;
// y值的setter和getter
- (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值的setter和getter(x值的set方法和get方法)
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
// y值的setter和getter
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
// 同时设置X和y
- (void)setX:(double)x andY:(double)y
{
// 直接调用set方法
[self setX:x];
[self setY:y];
}
// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other
{
// ((x1-x2)的平方 + (y1 -y2)的平方)开根
//return [Point2D distanceBetweenPoint1:self andPoint2:other];调用类方法
// x1-x2
double xDelta = [self x] - [other x]; // double xDelta = _x - [other x];不建议这样写
// (x1-x2)的平方
double xDeltaPF = pow(xDelta, 2.0);
// y1-y2
double yDelta = [self y] - [other y]; // double yDelta = _y - [other y]; 不建议这样写
// (y1-y2)的平方
double yDeltaPF = pow(yDelta, 2.0);
// 返回距离
return sqrt(xDeltaPF + yDeltaPF);
}
// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
return [p1 distanceWithOther:p2]; // 调用对象方法
// return [p2 distanceWithOther:p1];//与上一行同
}
@end
int main()
{
Point2D *p1 = [Point2D new];
// 设置点 (10, 15)
[p1 setX:10 andY:15];
Point2D *p2 = [Point2D new];
// 设置点 (13, 19)
[p2 setX:13 andY:19];
//double d1 = [p1 distanceWithOther:p2];调用类方法求距离
double d1 = [p1 distanceBetweenPoint1:p1 andPoint2:p2];
NSLog(@"%f", d1);
return 0;
}
2、在1的基础上设计一个类Circle,用来表示二维平面中的圆
1>属性
*double _radius (半径)
*Point2D *_point (圆心)
2>方法
*属性相应的set和get方法
*设计一个对象方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
*设计一个类方法判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
#import <Foundation/Foundation.h>
#import "Point2D.h"
// 圆
@interface Circle : NSObject
{
// 半径
double _radius;
// 圆心
// 采用组合
Point2D *_point;
}
// 声明半径的setter和getter
- (void)setRadius:(double)radius;
- (double)radius;
// 声明圆心的setter和getter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
// 判断跟其他圆是否重叠(重叠返回YES, 否则返回NO)
// 返回值是BOOL类型的,方法名一般都是以is开头
- (BOOL)isInteractWithOther:(Circle *)other;
// 判断两个圆是否重叠(重叠返回YES, 否则返回NO)
+ (BOOL)isInteractBeteenCircle1:(Circle *)c1: andCircle2:(Circle *)c2;
@end
#import "Circle.h"
@implementation Circle
// 半径的setter和getter
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
// 圆心的setter和getter
- (void)setPoint:(Point2D *)point
{
// _point是Circle的一个对象,默认情况下为0;所以下面代码无意义
//[_point setX:[point x]];
//[_point setY:[point y]];
_point = point;
}
- (Point2D *)point
{
return _point
}
// 判断跟其他圆是否重叠(重叠返回YES, 否则返回NO)
// 返回值是BOOL类型的,方法名一般都是以is开头
- (BOOL)isInteractWithOther:(Circle *)other
{
// 如果两个圆心的距离 < 两个圆的半径和, 重叠
// 如果两个圆心的距离 >= 两个圆的半径和, 重叠
Point2D *p1 = [self point];
Point2D *p2 = [other point];
// 圆心之间的距离
double distance = [p1 distanceWithOther:p2];
// 半径和
double radiusSum = [self radius] + [other radius];
return distance < radiusSum; // 关系运算符的结果只有两个,1和0
}
// 判断两个圆是否重叠(重叠返回YES, 否则返回NO)
+ (BOOL)isInteractBeteenCircle1:(Circle *)c1: andCircle2:(Circle *)c2
{
return [c1 isInteractWithOther:c2];
}
@end
int main()
{
// 圆对象
Circle *c1 = [Circle new];
//[[c1 point] setX:10];没有意义
// 设置半径
[c1 setRadius:5];
// 创建圆心对象
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:15];
// 先设置圆心
[c1 setPoint:p1];
[[c1 point] setX:15];
// c1 半径:5 圆心:(15,15)
// 圆对象
Circle *c2 = [Circle new];
// 设置圆的半径
[c2 setRadius:2];
// 创建圆心对象
Point2D *p2 = [Point2D new];
[p2 setX:12 andY:19];
// 设置圆心
[c2 setPoint:p2];
// c2 半径: 2 圆心: (12,19)
// 圆心距离: 5 半径和: 7
//BOOL b1 = [c1 isInteractWithOther:c2];
BOOL b1 = [Circle isInteractBeteenCircle:c1 andCircle2:c2];
NSLog(@"%d", b1);
return 0;
}