黑马程序员——Objective-C 第三天课程学习总结

<span style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);">Objective-C 第三天课程学习总结</span>

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

1. 点语法

1.1   利用点语法替换set方法和get方法

1.1.1 方法调用

Student *stu = [Studentnew];

[stu setAge:100];

int age = [stu age];

1.1.2点语法

stu.age = 100;

int age = stu.age;

1.2 点语法的本质

其实点语法的本质还是方法调用

当使用点语法时,编译器会自动展开成相应的方法


1.3   死循环注意

- (void) setAge:(int)age {

     // 下面的代码会引发死循环

     self.age = age;

}

- (int)age {

    // 下面的代码会引发死循环

     return self.age;

}

 

2. 成员变量的作用域

2.1 基本概念

局部变量、全局变量都有自己的作用域,成员变量也不例外

2.2 类型

@private:只能在当前类的对象方法中直接访问

@protected:可以在当前类以及子类的对象方法中直接访问

 @public:任何地方都可以直接访问

 @package:同一个“体系内”(框架)可以访问,介于@private和@public之间

2.3 继承补充

  专业术语

父类\超类 superclass

  子类subclass\subclasses

单继承

2.4   @implementation补充

没有@interface,只有@implementation,也可以开发一个类

 

3. @property和@synthesize

3.1 @property

用在@inteface中

用来自动生成setter和getter的声明

用@property int age;就可以代替下面的两行

- (int)age;   // getter

- (void)setAge:(int)age;  // setter


3.2 @synthesize

用在@implementation中

用来自动生成setter和getter的实现

 用@synthesize age = _age;就可以代替

- (int)age{

        return_age;

}

- (void)setAge:(int)age{

       _age= age;

}


 3.3 @synthesize的细节

 1> @synthesize age = _age;

setter和getter实现中会访问成员变量_age

 如果成员变量_age不存在,就会自动生成一个@private的成员变量_age

  2> @synthesize age;

 setter和getter实现中会访问成员变量age

如果成员变量age不存在,就会自动生成一个@private的成员变量age

3> 手动实现

若手动实现了setter方法,编译器就只会自动生成getter方法

若手动实现了getter方法,编译器就只会自动生成setter方法

 若同时手动实现了setter和getter方法,编译器就不会自动生成不存在的成员变量

3.4 @property新特性

   自从Xcode 4.x后,@property就独揽了@synthesize的功能。也就是说,@property可以同时生成setter和getter的声明和实现

   默认情况下,setter和getter方法中的实现,会去访问下划线 _ 开头的成员变量

4.  id

4.1 简介

   万能指针,能指向任何OC对象,相当于NSObject*

  id类型的定义

typedef struct objc_object {

    Class isa;

} *id;

4.2 使用

// 注意:id后面不要加上*

id p = [Personnew];

4.3 局限性

调用一个不存在的方法,编译器会马上报错

5. 构造方法

5.1 对象创建原理

 new的拆分两部曲

 分配内存(+alloc)

 初始化(-init)

Person *p1 = [Personalloc];

Person *p1 = [p1 init];

合成一句后:

Person *p = [[Personalloc] init];

5.2 init方法的重写

想在对象创建完毕后,成员变量马上就有一些默认的值

init方法的重写过程

- (id)init

{

    if (self = [superinit])

    {

       _age = 10;

    }

    return self;

}

5.3 自定义构造方法

   构造方法的一些规范

   返回值是id类型

   方法名都以init开头

- (id)initWithAge:(int)age {

    if (self = [super init]) {

        _age = age;

    }

    return self;

}

 传递多个参数进行初始化

- (id)initWithAge:(int)age andNo:(int)no;

 

6.  .h和.m文件的抽取

   每个类分布在不同文件中

   类的声明放在.h文件,类的实现放在.m文件

   若想使用某个类,就包含某个类的.h声明文件

7 代码示例

//
//  Point2D.h
//  10-编程题6
//
//  Created by jacy on 15/4/21.
//  Copyright (c) 2015年 jacy. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Point2D : NSObject

// x坐标
@property double x;
// y坐标
@property double y;
// 计算和另一点的距离
- (double) distanceOfPoint: (Point2D *) point;
// 计算两点之间的距离
+ (double) distanceOfPoint1: (Point2D *) point1 andPoint2: (Point2D *) point2;

@end


//
//  Circle.h
//  10-编程题6
//
//  Created by jacy on 15/4/21.
//  Copyright (c) 2015年 jacy. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Point2D.h"

@interface Circle : NSObject

// 圆心
@property Point2D * point;
// 半径
@property double radius;

// 一个圆和另一个圆是否重叠
- (BOOL) isOverlapWithOthers: (Circle *) other;

// 比较一个圆和另一个圆是否重叠
+ (BOOL) isOverlapOfCircle1: (Circle *) circle1 andCircle2: (Circle *) circle2;

@end


//
//  Circle.m
//  10-编程题6
//
//  Created by jacy on 15/4/21.
//  Copyright (c) 2015年 jacy. All rights reserved.
//

#import "Circle.h"

@implementation Circle

// 比较一个圆和另一个圆是否重叠
- (BOOL) isOverlapWithOthers: (Circle *) other
{

    return [Circle isOverlapOfCircle1: self andCircle2: other];
}

// 比较一个圆和另一个圆是否重叠
+ (BOOL) isOverlapOfCircle1: (Circle *) circle1 andCircle2: (Circle *) circle2
{
    // 两圆的半径之和
    double radiusSum = circle1.radius + circle2.radius;
    // 两圆心之间的距离
    double distance = [circle1.point distanceOfPoint:circle2.point];
    // 判断是否重叠
    if(distance < radiusSum)
    {
        return YES;
    }
    else
        return NO;
}
@end


//
//  Point2D.m
//  10-编程题6
//
//  Created by jacy on 15/4/21.
//  Copyright (c) 2015年 jacy. All rights reserved.
//

#import "Point2D.h"

@implementation Point2D

// 计算和另一点的距离
- (double) distanceOfPoint: (Point2D *) point
{
    // (x1- x2)平方
    double x = pow((self.x - point.x),2);
    // (y1-y2)平方
    double y = pow((self.y - point.y),2);
    // 距离
    double distance = sqrt(x + y);
    
    return distance;

}
// 计算两点之间的距离
+ (double) distanceOfPoint1: (Point2D *) point1 andPoint2: (Point2D *) point2
{

    return [point1 distanceOfPoint: point2];
}

@end



//
//  main.m
//  10-编程题6
//
//  Created by jacy on 15/4/21.
//  Copyright (c) 2015年 jacy. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Circle.h"
#import "Point2D.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
   
        // 创建Point2D对象
        // (10,10)
        Point2D *p = [[Point2D alloc] init];
        p.x = 10;
        p.y = 10;
        // 圆心(10,10),半径2的圆
        Circle *c = [[Circle alloc] init];
        c.point = p;
        c.radius = 2;
        
        // (14,13)
        Point2D *p2 = [[Point2D alloc] init];
        p2.x = 14;
        p2.y = 13;
        // 圆心(14,13),半径4
        Circle *c2 = [[Circle alloc] init];
        c2.point = p2;
        c2.radius = 4;
        
        // 比较两圆是否重叠
        //int a = [c isOverlapWithOthers: c2];
        int a = [Circle isOverlapOfCircle1: c andCircle2:c2];
        // 输出结果
        NSLog(@"a = %d",a);
        
        
    }
    return 0;
}


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值