黑马程序员-多文件开发以及Xcode功能演示

来源:互联网 发布:汽车报价软件哪个好 编辑:程序博客网 时间:2024/06/05 13:22

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

 

所有程序都放在一起显然是繁冗的,所以我们要多文件开发。

Point2D.m文件放Point2D

 

touch一个新文件Circle.m保存Circle

#import <Foundation/Foundation.h>

/*如果仅仅有这个的话,是不能识别下面的Point2D *类型的。所以要包含一个#import “Point2D.m”*/

 

 

1. Point2D.m :Point2D这个类的声明和实现  cc - c Point2D.m 编译成功->Point2D.o文件(只有Point2D的声明个实现,缺少main函数)

2. Cirle.m : Circle这个类的声明和实现  记得#import “Point2D.m”,Point2D类,Cicle类的声明和实现,缺少main函数

3. main函数 #import "Point2D.m" #import "Circle.m ".Point2D类,Cicle类的声明和实现,main函数

 

cc .m Point2D.m Circle.m -framework Foundation 一起编译

然后Point2D.o Circle.o main.o里面有重复的Point2D声明和实现内容,会报链接错。

跟C语言是一样的道理。one.c里面包含two.c文件里的test函数声明实现,会建立一个two.h文件只放进去test函数的声明。这样就不会报错了。

如下建立.h头文件

Point2D.h头文件

#import <Foundation/Foundation.h>

#import <math.h>

@interface Point2D : NSObject

{

double _x; // x值

double _y; // y值

}

 

// x值得set和get方法

- (void)setX:(double)x;

- (double)x;

 

// y值得set和get方法

- (void)setY:(double)y;

- (double)y;

 

// 设计一个对象方法同时设置x和y

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

 

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

- (double)DistanceWithOtherPoint:(Point2D *);

 

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

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

@end

Point2D.m文件

#import “Point2D.h”//为了识别Point2D类声明的属性方法

@implementation Point2D

// x值得set和get方法

- (void)setX:(double)x

{

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

   _x = x;

}

- (double)x

{

   return _x;

}

 

// y值得set和get方法

- (void)setY:(double)y

{

   _y = y;

}

- (double)y

{

   return _y;

}

 

// 设计一个对象方法同时设置x和y

- (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

 

下面是Circle.h

#import “Point2D.h”

#import <Foundation/Foundation.h>

@interface Circle : NSObject

{

   double _radius;

   Point2D *_point;//默认是NULL,nil

}

- (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

 

Circle.m

#import “Cirle.h”

@implementation Circle

 

//Radius的setter和getter

- (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

 

最后来看main.m

#import “Point2D.h”

#import “Circle.h”/*只要有声明通过编译就行,有方法,能使用,具体怎么实现不用管,这也就是封装。*/

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;

}

Xcode功能演示

1.    新建类:注意重复实现

2.    断点调试:精确知道代码的每步实现过程变量变化。查找无语法错误的错误,断点失效。

3.    代码段保存: 右下方用户自定义User。左键在代码段长按变为箭头,拖动放在User里。在summary里简介。重点是shortcut起一个简单的代号比如说_age-name。下次在代码里打这个整体我们COPY的代码部分就全部出现在我们现在的代码中。

//通过一定的格式创建字符串

4.    [NSString stringWithFormat:@"a is %d",age];

//从文件中读取字符串

5.    [NSString stringWithContentsofFile]

 

6.    文档的注释标记

1,基本使用: #pragma mark 今天就写到这儿

2.#pragma mark - 其他方法,有-就会有一条分割线,代表我们每一类

 

7.    练习的时候将suggest completions while typing 这个提示取消。便于自己记忆。

 

 

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