黑马程序员- NSString和点圆的设计作业

来源:互联网 发布:java soa架构是什么 编辑:程序博客网 时间:2024/05/29 11:00
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

NSString的基本使用

@"456"也是个对象,它属于NSString

 

#import <Foundation/Foundation.h>

@interface Person : NSObject

{

       //char *_name;

       NSString *_name;

}

@end

 

@implementation

@end

 

int main()

{

       //最简单的创建字符串的方式

       NSString *str = @"itcast";

       char *name = "itcast";

       NSLog(@"我在%@上课"str);

       NSLog(@"%s",name);

      

       int age = 10;

       int no = 5;

 

       //OC字符串的好处,面向对象的思想,用哪个字符串调用什么对象。

 

       NSString *name = @"jack";

       //length方法算的是字数,中文一个字跟C语言占据的3字符不同,它就是一个字。

       int size = [name length];

      

       //创建OC字符串的另一种方式

       NSString *newStr = [NSString stringWithFormat:@"My age is %d and no is %d and name is

%@",age,no,name];

       NSLog(@"- - -%@",newStr);

 

       NSLog(@"--%d",[newStr length]);

/*有个警告要求改成%ld,上面的int size已经将length转化成int类型,而下面的[newStr length]直接放在下面了。所以有个警告。*/

       return 0;

}

 

1.在分号后面写注释,要有一个空格,然后在//后面再加一个空格然后写注释

2. 在等号左右两边分别有个空格,这是要求必须的。

3.OC里面包含头文件都要用import

 

作业:点和圆的设计

 

/*

5.设计一个类Point2D,用来表示二维平面中某个点

1> 属性

* double x

* double y

 

2> 方法

* 属性相应的setget方法

* 设计一个对象方法同时设置xy

* 设计一个对象方法计算跟其他点的距离

* 设计一个类方法计算两个点之间的距离

 

3> 提示

* C语言的math.h中有个函数:double pow(double n, double m);计算nm次方

* 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值得setget方法

- (void)setX:(double)x;

- (double)x;

 

// y值得setget方法

- (void)setY:(double)y;

- (double)y;

 

// 设计一个对象方法同时设置xy

- (void)setX:(double)x andY:(double)y;

 

//设计一个对象方法计算跟其他点的距离

- (double)DistanceWithOtherPoint:(Point2D *);

 

//设计一个类方法计算两个点之间的距离

+ (double)DistanceBetweenPoint1(Point2D *)p1 andPoint2:(Point2D *)p2;

 

@end

 

@implementation Point2D

 

// x值得setget方法

- (void)setX:(double)x

{

       //if(x>0) x = 1;

       _x = x;

}

- (double)x

{

       return _x;

}

 

// y值得setget方法

- (void)setY:(double)y

{

       _y = y;

}

- (double)y

{

       return _y;

}

 

// 设计一个对象方法同时设置xy

- (void)setX:(double)x andY:(double)y

{

       _x = x;

       _y = y;

      

       self->_x = x;

       self->_y = y;

 

       [self setX:x];//有时候需要过滤,直接调用set方法,set方法里的注释部分

       [self setY:y];

}

 

//设计一个对象方法计算跟其他点的距离

- (double)DistanceWithOtherPoint:(Point2D *)

{

       //(x1-x2)平方+(y1-y2)平方然后开根

       return [Point2D DistanceBetweenPoint1:self andPoint2:other];

/*

       //x1-x2

       double xDelta = _x - [other x];

//内部成员变量直接写_x,other访问,要用get方法

       //(x1-x2)的平方

       double xDeltaPF = double pow(xDelta,2);

      

       //y1-y2

       double yDelta = _y - [other y];//_y其实就是[self y]

       //(y1-y2)的平方

       double yDeltaPF = double pow(yDelta,2);

      

       //返回距离

       return sqrt(xDeltaPF + yDeltaPF);

*/

}

 

 

//设计一个类方法计算两个点之间的距离

+ (double)DistanceBetweenPoint1(Point2D *)p1 andPoint2:(Point2D *)p2

{

       //x1-x2

       double xDelta = [p1 x] - [p2 x];

       //(x1-x2)的平方

       double xDeltaPF = double pow(xDelta,2);

      

       //y1-y2

       double yDelta = [p1 y] - [p2 y];

       //(y1-y2)的平方

       double yDeltaPF = double pow(yDelta,2);

      

       //返回距离

       return sqrt(xDeltaPF + yDeltaPF);

 

       //return [p1 DistanceWithOther:p2];

}

@end

/*

6.设计一个类Circle,用来表示二维平面中的圆

1> 属性

* double _radius (半径)

* Point2D *_point (圆心)

 

2> 方法

* 属性相应的setget方法

* 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO

* 设计一个类方法判断两个圆是否重叠(重叠返回YES,否则返回NO

*/

 

 

@interface Circle : NSObject

{

       double _radius;

       Point2D *_point;//默认是NULLnil

}

- (void)setRadius;

- (double)radius;

 

- (void)setPoint:(Point2D *)point;//(Point2D *);

- (Point2D *)point;

 

// 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO

// 返回值是BOOL类型,方法名一般都以is开头

-  (BOOL)isInteractWithOther:(Cicle *)other;

+  (BOOL)isInteractBetweenCicle1:(Cicle *)c1 andCicle2:(Cicle *)c2;

@end

 

@implementation Circle

 

//Radiussettergetter

- (void)setRadius:(double)radius

{

       _radius = radius;

}

- (double)radius

{

       return _radius;

}

 

- (void)setPoint:(Point2D *)point;//(Point2D *)类型

{

       //[_point setX:[point x]];

       //[_point setY:[point y]];这样写不行,因为_point是空值null

 

       _point = point;

}

- (Point2D *)point

{

       return point;

}

- (BOOL)isInteractWithOther:(Cicle *)other

{

       Point2D *p1 = [self point];

       Point2D *p2 = [self point];

       //圆心之间的距离

       double distance = [p1 DistanceWithOther:p2];

       double radiusSum = [self radius]+ [other radius];

/*          

       if (distance < radiusSum)

       {

              return YES;

       }

       else

       {

              reurn NO;

       }

 

       if (distance < radiusSum)

       {

              return 1;

       }

       else

       {

              reurn 0;

       }

*/

       return distance < radiusSum;

}

+ (BOOL)isInteractBetweenCicle1:(Cicle *)c1 andCicle2:(Cicle *)c2

{

 

}

@end

 

//只有利用类名调用类方法的时候不需要再类名后面*

//其它情况类名后面统一加一个*

 

int main()

{

       Point2D *p1 = [Point2D new];      

       [p1 setX: 10andY:15];

       Point2D *p2 = [Point2D new];

       [p2 setX13andY:19];

      

       double d1 = [p1 DistanceWithOther:p2];

      

       double d2 = [Point2D DistanceBetweenPoint1:p1 andPoint2:p2];

 

       NSLog(@"%f", d1);//d1前有个空格

 

       Circle *c1 = [Circle new];

       //[[c1 point] setx:10]; [c1 point]为空,没有任何意义。所以要先设置圆心

       //创建圆心对象

       Point2D *p = [Point new];

       [p1 setX:10 andY:15];

       //设置圆心

       [c1 setPoint:p1];//p1地址交给 _point

      

       [[c1 point] setx:10]; //现在的point对象是存在的可以进行赋值操作。

 

       // 圆对象

       Circle *c2 = [Circle new];

       // 设置圆半径

       [c2 setRadius];

       //设置圆心

      //首先设置圆心对象

       Point2D *p2 = [Point2D new];

       [p2 setX:12 andY:19];

       [c2 setPoint:p2];

      

       return 0;

}

 

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------详细请查看:www.itheima.com
0 0
原创粉丝点击