开发技巧
NSString:字符串处理类。存在于Foundation框架里面的。继承了NSObject。
创建字符串对象
#import <Foundation/Foundation.h>
int main()
{
// 最简单的创建字符串的方式
NSString *str = @"itcast"; // @”itcast”是一个对象,这样就直接创建了对象str并且可以操纵itcast
NSLog(@”这个字符串是%@”, str); // 字符串用%@
int size = [str length]; <span style="white-space:pre"> </span>// length方法,计算字数长度(包括空格)。例如“黑马”是两个字长,很实用
int age = 15;
int no = 10;
NSSring *newStr = [NSString stringWithFormat:@”My age is %d ang no is %d”, age, no]; // 输出可变化的age和no。
NSLog(@"--------%@", newStr);
return 0;
}
OC练习
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的值
#import <Foundation/Foundation.h>
#import <math.h>
@interface Point2D : NSObiect
{
double _x;
double _y;
}
- (void)setX:(double)x;
- (double)x;
- (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)setX:(double)x andY:(double)y
{
[self setX:x]; /* 这样设置的好处:如果赋值是对x和y有限制,调用set方法可以准确的 限制。若是 _x = x; 则不能体现在setX:x方法中对_x的限制性。*/
[self setY:y];
}
- (double)distanceWithOther:(Point2D *)other
{
/* 第一种算法:
//x1 – x2 double xCha = _x – [other x]; _x也可以用[self x]
// (x1 – x2)的平方 double xChaPF = pow(xCha, 2);
//y1 – y2 double yCha = _y – [other y];
// (y1 – y2)的平方 double yChaPF = pow(yCha, 2);
// return sqrt(xChaPF + yChaPF); 开根号
*/
// 第二种写法,直接调用类方法
return [Point2D distanceBetweenPoint1:self andPoint2:other];
}
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
// x1 – x2
double xCha = [p1 x] – [p2 x];
// (x1 – x2)的平方
double xChaPF = pow(xCha, 2);
// y1 – y2
double yCha = [p1 y] – [p2 y];
// (y1 – y2)的平方
double yChaPF = pow(yCha, 2);
// 求距离,开根号
return sqrt(xChaPF + yChaPF);
/* 如果对象方法里面计算了距离,类方法调用对象方法则
return [p1 distanceWithOther:p2]; 或者return [p2 distanceWithOther:p1]; */
}
@end
int main()
{
Point2D *p1 = [Point2D new];
Point2D *p2 = [Point2D new];
[p1 setX:10 andY:20];
[p2 setX:13 andY:16];
double d = [p1 distanceWithOther:p2];
double d2 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2]; // 以上两个都输出距离
NSLog(@"%f", d);
return 0;
}
2、练习:(运用了组合和上题中组合)
设计一个Circle,用来表示二维平面中的圆
-1、属性
*double _radius (半径)
*Point2D *_point (圆心)
-2、方法
* 属性相应的set和get方法
*设计一个对象判断跟其他圆是否相交(相交返回YES,否则返回NO)
*设计一个类方法判断两个圆是否相交(相交返回YES,否则返回NO)
#import <Foundation/Foundation.h>
#import <math.h>
@interface Circle : NSObiect
{
// 半径
double _radius;
// 圆心,和上题组合
Point2D *point;
}
// 半径的set和get方法
- (void)setRadius:(double)radius;
- (double)radius;
// 圆心的set和get方法
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
// 判断是否相交
// 返回值是BOOL类型的,方法名一般以is开头
- (BOOL)isInteractWithOther:(Circle *)other;
// 类方法判断是否相交
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;
@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
{
// 圆心距离小于半径和就相交
Point2D *p1 = [self point];
Point2D *p2 = [other point];
// 两个圆心的距离
double distance = [p1 distanceWithOther:p2];
// 两个半径的和
double radiusSum = [self radius] + [other radius];
return distance < radiusSum;
}
// 类方法判断是否相交
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2
{
return [c1 isInteractWithOther:c2];
}
@end
int main()
{
// 圆的对象
Circle *c1 = [Circle new];
[c1 setRadius:1];
// 圆心对象1
Point2D *p1 = [Point2D new]; // 先创建圆心对象
[p1 setX:10 andY:15];
[c1 setPoint:p1]; // 把圆心对象占为己有
// [[c1 point] setX:12]运行这行代码圆的x变为12
// 圆的对象2
Circle *c2 = [Circle new];
[c2 setRadius:5];
// 圆心对象2
Point2D *p2 = [Point2D new];
[p2 setX:3 andY:8];
[c2 setPoint:p2];
BooL b1 = [c1 isInteractWithOther:c2];
NSLog(@”%d”, b1);
return 0;
}
多文件开发:
1、定义一个类分2个文件:.h声明文件、.m实现文件
.h : 成员变量、方法的声明
.m :方法的实现
2、如果想使用某一个类,只需要#import类的.h文件即可