——黑马程序员——OC中的私有变量、私有方法、descriptiion方法

来源:互联网 发布:单桂敏艾条淘宝旗舰店 编辑:程序博客网 时间:2024/06/05 21:02

-----------android培训java培训、java学习型技术博客、期待与您交流!-----------

OC中的私有变量、私有方法、descriptiion方法

一、实例变量的作用域

访问修饰有三个

1、@public:在有对象的前提下,实例变量可以在任意程序访问,作用域从@public开始直到大括号结束为止,中间没有遇到其他的修饰符,

例如:

@interface Person:NSObject

{

@public

int _age;

int Phonenum;

}     //从@public 开始到这里结束,中间没有任何其他的变量修饰符


@end


@interface Person:NSObject

{

@public

int _age;

@private

int  _Phonenum;

}     //从@public 开始到@private结束,int _age 是公开的,而int _Phonenum 是相对私有的


@end

2、@protect:实例变量只能在当前类和派生类(子类)中访问,成员变量默认的是@protect类型的

3、@private:实例变量只能在当前类中使被直接访问;能被子类继承(子类能继承父类所有的方法和属性)但不能被访问,是相对私有的

4、@package:它是框架级别的作用域介于私有和公开之间,只要处于同一个框架中就可以直接通过变量访问

二、私有变量

私有变量:在 .m文件中声明并且定义的实例变量,是相对私有的

特点:

1、该变量只能在当前类中使用,是相对私有的

2、不能被子类继承,也不能被其他类访问

三、私有方法

私有方法 .文件中没有声明只在  .m  文件中实现的方法,称为私有方法

#import <Foundation/Foundation.h>//定义Person类@interface Person: NSObject-(void)run;@end//定义Student类继承Person类@interface Student: Person@end//Person类的实现@implementation Person-(void)run{NSLog(@"人在走");}//为声明只实现test方法-(void)test{NSLog(@"这是私有方法");}@end//实现Student类@implementation Student@endint main (int argc, const char *argv[]) {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                                //    //创建对象Person *p = [Person new];Student *stu = [Student new];//用Person调用test方法[p test];//用Student 调用test方法[stu test];       [pool drain];    return 0;}


此处可以成功调用是因为都在一个文件中,没有使用多文件开发

如果是使用了多文件开发后想要调用 私有方法可以在.m文件中使用self,如以下代码就可以调用成功

@implementation Person-(void)run{
//使用self调用test方法[self test];NSLog(@"人在走");}//为声明只实现test方法-(void)test{NSLog(@"这是私有方法");}@end
</pre><p><strong><span style="font-size:18px;color:#ff0000;">结论:私有方法是相对私有的,不能被子类继承和访问</span></strong></p><h2><span style="font-size:14px">四、description 方法重写</span></h2><p><span style="font-size:14px;">description方法1、是NSObject中的方法,返回值是 NSString *  类型的,</span></p><p><span style="font-size:14px;">2、当以%@的方式打印出对象时,默认返回的是类名和对象在内存中的地址</span></p><p><span style="font-size:14px;">3、在使用NSLog打印时输出的结构意思不是很大,所以在子类中可以对他进行重写</span></p><p><span style="font-size:14px;">对象的description方法</span></p><p><span style="font-size:14px;">-(NSString *)description{</span></p><p><span style="font-size:14px;">return @“XXX”;</span></p><p><span style="font-size:14px;">}</span></p><div><span style="font-size: 14px;"></span><pre name="code" class="html">#import <Foundation/Foundation.h>//定义Person类@interface Person: NSObject-(void)run;@end//定义Student类继承Person类@interface Student: Person@end//Person类的实现@implementation Person-(void)run{NSLog(@"人在走");}//为声明只实现test方法-(void)test{NSLog(@"这是私有方法");}@end//实现Student类@implementation Student@endint main (int argc, const char *argv[]) {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                                //    //创建对象Person *p = [Person new];Student *stu = [Student new];//用Person调用test方法//[p test];//用Student 调用test方法//[stu test];      //description方的使用   NSLog(@"p = %@ ",p);       [pool drain];    return 0;}

重写description后
#import <Foundation/Foundation.h>//定义Person类@interface Person: NSObject-(void)run;@end//定义Student类继承Person类@interface Student: Person@end//Person类的实现@implementation Person-(void)run{NSLog(@"人在走");}//为声明只实现test方法-(void)test{NSLog(@"这是私有方法");}//重写description方法-(NSString *)description{return @"这是description方法重写后 这是Person类!";}@end//实现Student类@implementation Student@endint main (int argc, const char *argv[]) {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                                //    //创建对象Person *p = [Person new];Student *stu = [Student new];//用Person调用test方法//[p test];//用Student 调用test方法//[stu test];      //description方的使用   NSLog(@"p = %@ ",p);       [pool drain];    return 0;}


类方法中description重写
+(NSString *)description{

return @“XXCCV”;
}

#import <Foundation/Foundation.h>//定义Person类@interface Person: NSObject-(void)run;@end//定义Student类继承Person类@interface Student: Person@end//Person类的实现@implementation Person-(void)run{NSLog(@"人在走");}//为声明只实现test方法-(void)test{NSLog(@"这是私有方法");}//重写description对象方法-(NSString *)description{return @"这是description方法重写后 这是Person类!";}//重写description类方法+(NSString *)description{return @"这是description类方法重写后 这是类方法!";}@end//实现Student类@implementation Student@endint main (int argc, const char *argv[]) {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                                //    //创建对象Person *p = [Person new];Student *stu = [Student new];//用Person调用test方法//[p test];//用Student 调用test方法//[stu test];      //description方的使用   NSLog(@"p = %@ ",p);   NSLog(@"%@", [p class]);       [pool drain];    return 0;}


五、description陷阱

description方法中不能同时使用%@和self ,自己调用自己 会造成死循环

-(NSString *)description {

return [NSString stringWithFormat:@"%@",self];
}


1 0
原创粉丝点击