Objective-C中public、protected、private的使用
来源:互联网 发布:电脑三维绘图软件 编辑:程序博客网 时间:2024/05/16 06:04
1.@public,@protected,@private
个人觉得@public和@protected并没有很明确的区别,在他们作用范围内的对象,如果不将对象设置成@property,则该对象相当于 protected对象,只有子类和本身可以访问该对象;如果将对象设置成@property,则可以使使用者访问该对象。@private,顾名思义, 这里声明的就是私有对象
注:可以通过指针的方式访问。
2.static对象(类内)
我们将对象声明在类中"{"和"}"之外,也就是与方法和@property写在一起,就可以声明一个类内部的static对象。
3.static方法
当方法前是使用"+"来修饰并且声明在头文件中,则说明该方法相当于c++中的static方法,通过类直接调用。但是需要注意的是,虽然这样的方法可以通过类直接调用,但是不可以通过对象调用。
4.public方法
当方法前是使用"-"来修饰并且声明在头文件中,则该方法可以通过类的对象进行调用。
5.private方法
Objective-C中的private方法是通过category实现的,在实现文件中我们声明一个类的category,在这里面的方法就是private方法。类的对象是不可以进行调用的,同样由于该方法的声名是在类的实现文件中,所以子类也是不能重写该方法的。
下面的代码是对于public、protected、private的具体实现
头文件
#import <Foundation/Foundation.h>
@interface Grammar : NSObject {
@public
NSString* publicString; //公有成员
@protected
NSString* protectedString; //受保护成员
@private
NSString* privateString; //私有成员
}
NSString* staticString; //静态成员 很重要!!!!!
@property (nonatomic, retain) NSString* publicString; //公有成员
+ (void)staticMethod; //静态方法(类方法)
- (void)publicMethod; //公有方法(对象实例方法)
@end//****************************************************
实现文件
#import "Grammar.h"
#pragma mark -
#pragma mark Grammar(private)
@interface Grammar(private) //(在@implementation上面)- (void)privateMethod; //私有方法(对象实例方法)
@end#pragma mark -
#pragma mark Grammar
@implementation Grammar
@synthesize publicString; //公有成员 (在@implementation下面)
- (id)init
{
if((self = [super init]))
{
if(publicString == nil)
{
publicString = [[NSString alloc] init];
}
if(protectedString == nil)
{
protectedString = [[NSString alloc] init];
}
if(privateString == nil)
{
privateString = [[NSString alloc] init];
}
if(staticString == nil)
{
staticString = [[NSString alloc] init];
}
}
return self;
}
- (void)dealloc
{
[publicString release];
[protectedString release];
[privateString release];
[super dealloc];
}
#pragma mark -
#pragma mark Public Method
+ (void)staticMethod
{
}
- (void)publicMethod
{
}
#pragma mark -
#pragma mark Private Method
- (void)privateMethod
{
}
@end
- Objective-C中public、protected、private的使用
- Objective-C中public、protected、private的使用
- Objective-C中public、protected、private的使用
- Objective-C 中public、protected、private 的使用
- Objective-C 中public、protected、private 的使用
- Objective-C中public、protected、private的使用
- 继承 Objective-C中public、protected、private的使用
- Objective-C中@private、@protected、@public 解析
- Objective-C中@private、@protected、@public 解析
- Objective-C中@private、@protected、@public 解析
- Objective-C中@private、@protected、@public 解析
- Objective-C中也存在public、protected、private的使用
- objective-c @private @protected @public
- Objective-C中@private、@protected、@public、@package 解析
- objective-c 与 protected public private关键字
- Objective-C @protected @private @public @package
- public,private,protected的使用
- Object - c 中 @public @protected @private的理解
- java路径
- 理解多线程设计模式
- tomcat配置httponly属性
- Mysql字段操作—添加字段、删除字段、修改字段名、修改字段类型(约束条件) 、重命名表名
- LeetCode Two Sum
- Objective-C中public、protected、private的使用
- ubuntu terminal选中即复制
- DundasDashboard与微软PerformancePoint 2010差异比较
- 6 ways to download free intraday and tick data for the U.S. stock market
- 的付款后快递费客户开发的很快的非官方几架飞机
- Django模型(model)详细介绍1
- weblogic out of space in CodeCache for adapters导致宕机
- Scala 函数特性编程实践(二)
- 那客户的付货款的进口铝合金的老客户了的房间绿化带