黑马程序员-OC类开发技巧

------ Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1.NSString 使用

@"12345"也是一个对象

oc .h与.m的分工
.h是声明
.m是实现


bogon:05oc whome$ touch 01oc.m
bogon:05oc whome$ open 01oc.m

#import<Foundation/Foundation.h>
//关于NSString
int main()
{
    //最简单的创建字符串的方式
    NSString *str = @"itcast";
    char *nameC = "itcast";
    
    NSLog(@"%s",nameC);
    NSLog(@"2我在%@上课",str);
    
    int age=15;
    int no = 5;
    NSString *name=@"jack";
    //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]);
    return 0;
}


bogon:05oc whome$ cc 01oc.m -framework Foundation
bogon:05oc whome$ ./a.out
2014-10-09 09:04:38.749 a.out[553:507] itcast
2014-10-09 09:04:38.753 a.out[553:507] 2我在itcast上课
2014-10-09 09:04:38.754 a.out[553:507] ---- 4
2014-10-09 09:04:38.755 a.out[553:507] ---- My age is 15 and no is 5 and name is jack
2014-10-09 09:04:38.764 a.out[553:507] ---- 41
2.练习.实现点类和圆类,然后判断两个圆是否重叠

WhomedeMac:05oc whome$ touch 02oc.m
WhomedeMac:05oc whome$ open 02oc.m


/*
 设计一个类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; // x值
    double _y; // y值
}
// x值的getter和setter方法
- (void)setX:(double)x;
- (double)x;

// y值的getter和setter方法
- (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值的getter和setter方法
- (void)setX:(double)x
{
    _x = x;
}
- (double)x
{
    return _x;
}

// y值的getter和setter方法
- (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];
    [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];
    //(x1-x2)的平方
    double xDeltaPF = pow(xDelta,2);
    
    
    //y1-y2
    double yDelta=self->_y - [other y];
    //(y1-y2)的平方
    double yDeltaPF = pow(yDelta,2);
    
    //返回距离
    return sqrt(xDeltaPF + yDeltaPF);
    
}
//计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *) p1 andPoint2:(Point2D *) p2
{
    return [p2 distanceWithOther:p1];
    
    /*
    //x1-x2
    double xDelta= [p1 x] - [p2 x];
    //(x1-x2)的平方
    double xDeltaPF = pow(xDelta,2);
    
    
    //y1-y2
    double yDelta=[p1 y] - [p2 y];
    //(y1-y2)的平方
    double yDeltaPF = pow(yDelta,2);
    
    //返回距离
    return sqrt(xDeltaPF + yDeltaPF);
     */
}
@end



/*
 1.设计一个类Circle,用来表示二维平面中的圆
 属性
    double _radius(半径)
    Point2D *_point (圆心)
 方法
    属性相应的set和get方法
    设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
    设计一个对象判断两个圆是否重叠(重叠返回YES,否则返回NO)
 */
//圆
@interface Circle : NSObject
{
    // 半径
    double _radius;
    // 圆心
    Point2D *_point;
}
// 半径的getter和setter
- (void)setRadius:(double)radius;
- (double)radius;

//圆心的getter和setter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;

// 判断跟其他圆是否重叠 (重叠返回YES,否则返回NO)
//返回值是BOOL类型的,方法名一般都以is开头
-(BOOL)isInteractWithOther:(Circle *)other;
// 判断两个圆是否重叠 (重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;

@end
@implementation Circle
// 半径的getter和setter
- (void)setRadius:(double)radius
{
    _radius = radius;
}
- (double)radius
{
    return _radius;
}

//圆心的getter和setter
- (void)setPoint:(Point2D *)point
{
    _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];
    
    if(distance < radiusSum)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    return distance < radiusSum;
}

// 判断两个圆是否重叠 (重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2
{
    return [c1 isInteractWithOther:c2];
}
@end



/*
总结
1.只有利用类名调用类方法的时候,不需要在类名后面写*。其他情况下,类名后面统一加上一个*
Circle *c1 = [Circle new];
- (BOOL)isInteractWithOther:(Circle *)other;

2.返回值是BOOL类型的方法,方法名一般都以is开头
- (BOOL)isInteractWithOther:(Circle *)other;

3.想要拥有某个对象,就先创建对象,然后调用set方法将对象传递给内部的成员变量
*/
int main()
{
    Circle *c1 = [Circle new];
    //设置半径
    [c1 setRadius:5];
    //创建圆心对象
    Point2D *p = [Point2D new];
    [p setX:10 andY:15];
    
    //选设置圆心
    [c1 setPoint:p];
    
    [[c1 point] setX:15];
    
    //圆对象
    Circle *c2 = [Circle new];
    //设置圆的半径
    [c2 setRadius:2];
    
    //创建圆心对象
    Point2D *pp = [Point2D new];
    [pp setX:12 andY:19];
    //设置圆心
    [c2 setPoint:pp];
    
    BOOL b1 = [c1 isInteractWithOther:c2];
     NSLog(@"测圆是否重叠%d",b1);
    
    //测点距离
    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];
    NSLog(@"测点距离%f",d1);
    
    double d2 =[Point2D distanceBetweenPoint1:p1 andPoint2:p2];
    NSLog(@"测点距离%f",d2);
    
    return 0;
    
}


WhomedeMac:05oc whome$ cc 02oc.m -framework Foundation
WhomedeMac:05oc whome$ ./a.out
2014-10-14 00:35:28.527 a.out[603:507] 测圆是否重叠1
2014-10-14 00:35:28.534 a.out[603:507] 测点距离5.000000
2014-10-14 00:35:28.541 a.out[603:507] 测点距离5.000000

多文件开发
1.Point2dM:Pointd这个类的声明和实现-编译成功
2.Circle.m上类的声明和实现
3.main函数。

oc .h与.m的分工
.h是声明
.m是实现

使用xcode
点击xcode - Create a new Xcode project - OS X- Application - Command Line Tool - create(程序运行在终端)


command+r 运行

bogon:03oc分文件开发 whome$ touch Point2D.h
bogon:03oc分文件开发 whome$ touch Point2D.m
bogon:03oc分文件开发 whome$ touch Circle.h
bogon:03oc分文件开发 whome$ touch Circle.m
bogon:03oc分文件开发 whome$ open Point2D.h
bogon:03oc分文件开发 whome$ open Point2D.m
bogon:03oc分文件开发 whome$ open Circle.h
bogon:03oc分文件开发 whome$ open Circle.m
bogon:03oc分文件开发 whome$ open main.m

Point2D.h->
/*
 设计一个类 ,用来表示二维平面中某个点
 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>

//点
@interface Point2D : NSObject
{
    double _x; // x值
    double _y; // y值
}
// x值的getter和setter方法
- (void)setX:(double)x;
- (double)x;

// y值的getter和setter方法
- (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



Point2D.m->
/*
 设计一个类 ,用来表示二维平面中某个点
 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>
#import"Point2D.h"

@implementation Point2D

// x值的getter和setter方法
- (void)setX:(double)x
{
    _x = x;
}
- (double)x
{
    return _x;
}

// y值的getter和setter方法
- (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];
    [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];
    //(x1-x2)的平方
    double xDeltaPF = pow(xDelta,2);
    
    
    //y1-y2
    double yDelta=self->_y - [other y];
    //(y1-y2)的平方
    double yDeltaPF = pow(yDelta,2);
    
    //返回距离
    return sqrt(xDeltaPF + yDeltaPF);
    
}
//计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *) p1 andPoint2:(Point2D *) p2
{
    return [p2 distanceWithOther:p1];
    
    /*
     //x1-x2
     double xDelta= [p1 x] - [p2 x];
     //(x1-x2)的平方
     double xDeltaPF = pow(xDelta,2);
     
     
     //y1-y2
     double yDelta=[p1 y] - [p2 y];
     //(y1-y2)的平方
     double yDeltaPF = pow(yDelta,2);
     
     //返回距离
     return sqrt(xDeltaPF + yDeltaPF);
     */
}
@end




Circle.h->
/*
 1.设计一个类Circle,用来表示二维平面中的圆
 属性
 double _radius(半径)
 Point2D *_point (圆心)
 方法
 属性相应的set和get方法
 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
 设计一个对象判断两个圆是否重叠(重叠返回YES,否则返回NO)
 */
//圆
#import<Foundation/Foundation.h>
#import"Point2D.h"

@interface Circle : NSObject
{
    // 半径
    double _radius;
    // 圆心
    Point2D *_point;
}
// 半径的getter和setter
- (void)setRadius:(double)radius;
- (double)radius;

//圆心的getter和setter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;

// 判断跟其他圆是否重叠 (重叠返回YES,否则返回NO)
//返回值是BOOL类型的,方法名一般都以is开头
-(BOOL)isInteractWithOther:(Circle *)other;
// 判断两个圆是否重叠 (重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;

@end



Circle.m->

/*
 1.设计一个类Circle,用来表示二维平面中的圆
 属性
 double _radius(半径)
 Point2D *_point (圆心)
 方法
 属性相应的set和get方法
 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
 设计一个对象判断两个圆是否重叠(重叠返回YES,否则返回NO)
 */
//圆
#import<Foundation/Foundation.h>
#import"Point2D.h"
#import"Circle.h"

@implementation Circle


// 半径的getter和setter
- (void)setRadius:(double)radius
{
    _radius = radius;
}
- (double)radius
{
    return _radius;
}

//圆心的getter和setter
- (void)setPoint:(Point2D *)point
{
    _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];
    
    if(distance < radiusSum)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    return distance < radiusSum;
}

// 判断两个圆是否重叠 (重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2
{
    return [c1 isInteractWithOther:c2];
}
@end


/*
 总结
 1.只有利用类名调用类方法的时候,不需要在类名后面写*。其他情况下,类
 名后面统一加上一个*
 Circle *c1 = [Circle new];
 - (BOOL)isInteractWithOther:(Circle *)other;
 
 2.返回值是BOOL类型的方法,方法名一般都以is开头
 - (BOOL)isInteractWithOther:(Circle *)other;
 
 3.想要拥有某个对象,就先创建对象,然后调用set方法将对象传递给内部的
 成员变量
 */


Main.m->
#import<Foundation/Foundation.h>
#import"Point2D.h"
#import"Circle.h"

int main()
{
    Circle *c1 = [Circle new];
    //设置半径
    [c1 setRadius:5];
    //创建圆心对象
    Point2D *p = [Point2D new];
    [p setX:10 andY:15];
    
    //选设置圆心
    [c1 setPoint:p];
    
    [[c1 point] setX:15];
    
    //圆对象
    Circle *c2 = [Circle new];
    //设置圆的半径
    [c2 setRadius:2];
    
    //创建圆心对象
    Point2D *pp = [Point2D new];
    [pp setX:12 andY:19];
    //设置圆心
    [c2 setPoint:pp];
    
    BOOL b1 = [c1 isInteractWithOther:c2];
    NSLog(@"测圆是否重叠%d",b1);
    
    //测点距离
    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];
    NSLog(@"测点距离%f",d1);
    
    double d2 =[Point2D distanceBetweenPoint1:p1 andPoint2:p2];
    NSLog(@"测点距离%f",d2);
    
    return 0;
    
}




缺少定义.没有实现,只有声明,编译通过,链接不行。
bogon:03oc分文件开发 whome$ cc main.m -framework Foundation
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_Circle", referenced from:
      objc-class-ref in main-69b0a8.o
  "_OBJC_CLASS_$_Point2D", referenced from:
      objc-class-ref in main-69b0a8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
bogon:03oc分文件开发 whome$ cc main.m -framework Foundation

正确
bogon:03oc分文件开发 whome$ cc -c Point2D.m
bogon:03oc分文件开发 whome$ cc -c Circle.m
bogon:03oc分文件开发 whome$ cc -c Main.m
bogon:03oc分文件开发 whome$ cc Point2D.o Circle.o Main.o -framework Foundation
bogon:03oc分文件开发 whome$ ./a.out
2014-10-27 07:17:17.402 a.out[694:507] 测圆是否重叠1
2014-10-27 07:17:17.406 a.out[694:507] 测点距离5.000000
2014-10-27 07:17:17.407 a.out[694:507] 测点距离5.000000


------ Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值