——黑马程序员——OC中的私有变量、私有方法、descriptiion方法
2015-11-06 06:56
489 查看
-----------android培训、java培训、java学习型技术博客、期待与您交流!-----------
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:它是框架级别的作用域介于私有和公开之间,只要处于同一个框架中就可以直接通过变量访问
特点:
1、该变量只能在当前类中使用,是相对私有的
2、不能被子类继承,也不能被其他类访问
文件中实现的方法,称为私有方法
此处可以成功调用是因为都在一个文件中,没有使用多文件开发
如果是使用了多文件开发后想要调用 私有方法可以在.m文件中使用self,如以下代码就可以调用成功
重写description后
类方法中description重写
+(NSString *)description{
return @“XXCCV”;
}
description方法中不能同时使用%@和self ,自己调用自己 会造成死循环
-(NSString *)description {
return [NSString stringWithFormat:@"%@",self];
}
9d20
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、不能被子类继承,也不能被其他类访问
三、私有方法
私有方法:在 .h 文件中没有声明,只在 .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 @end int 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
@end
int 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
@end
int 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
@end
int 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];
}
9d20
相关文章推荐
- 黑马程序员-Java I/O流(二)-File类、Properties类、打印流、序列流(合并流)
- Java 线程面试问题
- SOA面试题
- 小米面试题 计算朋友圈问题
- Java程序员面试中的多线程问题
- 面试经历---铂涛集团(2015年10月17日下午)
- 一个优秀的Java程序员必须了解的GC机制
- [非凡程序员]观察者模式
- [非凡程序员]观察者模式
- 面试代码
- 2016求职小结
- 一个骨灰级的程序员即将闪亮毕业
- 模拟面试100问
- 【笔记】程序员的思维修炼4
- 【笔记】程序员的思维修炼5
- 黑马程序员——集合类(一)
- 黑马程序员——Java基础---多线程(2)
- 黑马程序员——Java基础---多线程(1)
- 高效能程序员的修炼 --读书笔记
- 如果像招聘程序员那样招聘木匠