黑马程序员技术交流社区

标题: OC 封装和self初 [打印本页]

作者: 八寒地狱    时间: 2014-10-20 01:27
标题: OC 封装和self初
一 面向对象和封装
面向对象有三大特性;封装(成员变量).继承和多态
开发过程中,为了安全性的要求,我们一般不再成员变量名前面使用@public,@protected等关键字修饰,
而是使用set 方法为对象提供成员变量的值。也可以在set方法内部对一些不合理的赋值进行过滤

1 Set
方法的作用;为外界提供一个设置成员变量名的方法
命名规范
1 方法名必须以set开头
2 set 后面跟上成员变量的名称。手字母大写
3 返回值一定是void
4 一定要接受个参数,而且参数类型需要和成员变量保持一致
2 Get 方法
方法的作业,为调用者返回对象内部的成员变量
命名规范
(1)  一定有返回值,返回值类型和成员变量类型一致
(2)  方法名和成员变量名一样
(3) 不需要接受任何参数
3 set 和 get 方面小练习

设计一个成绩类
  C语言成绩
  OC成绩
总分
  平均分

#import <Foundation/Foundation.h>

@interface Score : NSObject
{
    int _cScore; // C语言成绩
    int _ocScore; // OC成绩
   
    int _totalScore;// 总分
    int _averageScoe; // 平均分
}

- (void)setCScore:(int)cScore;
- (int)cScore;
/*
set 方面后跟首字母大写的变量名,变量类型后面不要求首字母大写
*/
- (void)setOcScore:(int)ocScore;
- (int)ocScore;
/*
get 方面声明成员变量首字母不要求大写
*/
- (int)totalScore;
- (int)averageScore;

@end

@implementation Score
- (void)setCScore:(int)cScore
{
    _cScore = cScore;
   
    // 计算总分
    _totalScore = _cScore + _ocScore;
    _averageScoe = _totalScore/2;
}
- (int)cScore
{
    return _cScore;
}

- (void)setOcScore:(int)ocScore
{
    _ocScore = ocScore;
   
    // 计算总分
    _totalScore = _cScore + _ocScore;
    _averageScoe = _totalScore/2;
}
// 监听成员变量的改变

- (int)ocScore
{
    return _ocScore;
}
/*
get 方法返回内部成员变量
*/

- (int)totalScore
{
    return _totalScore;
}
- (int)averageScore
{
    return _averageScoe;
}
@end


int main()
{
    Score *s = [Score new];
   
    [s setCScore:90];
    [s setOcScore:100];
   
    [s setCScore:80];
   
   
    int a = [s totalScore];
   
    NSLog(@"总分:%d", a);
   
    return 0;
}
4 对象方法和类方面
     对象方面 以减号- 开头,只能对象来钓鱼 能访问当前对象的成员变量
   类方法   以+开头 只能由类来调用 不能访问成员变量 类方面调用类方法容易引发死循环

#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    int age;
}
//类方法都是以+开头
+ (void)pName;

- (void)test;
+ (void)test;
@end
@implementation Person

+ (void)pName
{
   
}

- (void)test
{
    NSLog(@"30-%d", age);
   
}

+ (void)test
{
    // 会引发死循环
    //[Person test];
   
    NSLog(@"34");
   
    // 会引发死循环
    //  [Person test];
}

@end

int main()
{

   
    [Person test];
   

    return 0;
}



4  self
谁调用当前方面就代表谁,self出现在对象方面中就代表对象,出现在类方法中就代表类
(1)调用self 时 当成员变量和局部变量同名,采取就近原则,访问的是局部变量
(2) self 不能出现在函数中 使用OC 对象方法和类方法
(3) 使用self->成员变量名 可以访问当前方面调用的成员变量 self 方面名 来调用方法                       
一般错误 self 调用函数   。 类方法中用self调用对象方面。对象方法中 用self调用类方法。
  注意self 的死循环  在声明里面 self不能调用自己的对象方法或者类方面,否则会引发死循环
,可以在当前类方法或者对象方法中 调用其他类的对象或者类方法
以 李明杰老师的视频教程为例

#import <Foundation/Foundation.h>

@interface Person : NSObject
- (void)test;
+ (void)test;

- (void)test1;
+ (void)test2;


- (void)haha1;

+ (void)haha2;


@end

@implementation Person
- (void)test
{
    NSLog(@"调用了-test方法");
   
    // 会引发死循环
        // 这里调用了自己的对象方法
    //[self test];
}

+ (void)test
{
    NSLog(@"调用了+test方法");
   
    // 会引发死循环
     //这里调用了自己的类方法
    //[self test];
}

- (void)test1
{
    [self test];
}

+ (void)test2
{
    [self test];
}

- (void)haha1
{
    NSLog(@"haha1-----");
}


void haha3()
{
   
}

+ (void)haha2
{
    // haha3();
    [self haha3];
    // [self haha1];
}
@end

int main()
{
    [Person haha2];
    //Person *p = [Person new];
   
    //[p test1];
    return 0;
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2