在Objective_C编程中使用私有方法是本文要介绍的内容,主要是来学习私有方法的使用。面向对象的设计中,有一个特性封装性,就是将某些东西包装和隐藏起来,让外界无法直接使用,只能通过某些特定的方式才能访问。
在Objective-C编程中也可以做到这一点。比如下面这个类:
先是接口:
- // ===========================
- // = File: SomeClass.h
- // = Interface for SomeClass
- // ===========================
- @interface SomeClass : NSObject
- -(void) msg;
- +(void) classMsg;
- @end
- // ===========================
- // = File: SomeClass.h
- // = Interface for SomeClass
- // ===========================
- @interface SomeClass : NSObject
- -(void) msg;
- +(void) classMsg;
- @end
很简单的接口,再是它的实现和类别:
- // ===========================
- // = File: SomeClass.m
- // ===========================
- #import "SomeClass.h"
- // =================================
- // = Interface for hidden methods
- // =================================
- @interface SomeClass (hidden)
- +(void) hiddenClassMethod;
- -(void) hiddenInstanceMethod;
- @end
- // =====================================
- // = Implementation of hidden methods
- // =====================================
- @implementation SomeClass (hidden)
- +(void) hiddenClassMethod
- {
- printf( "Hidden class method.\n" );
- }
- -(void) hiddenInstanceMethod
- {
- printf( "Hidden instance method\n" );
- }
- @end
- // ================================
- // = Implementation for SomeClass
- // ================================
- @implementation SomeClass
- -(void) msg
- {
- printf("Inside msg()...\n");
- [self hiddenInstanceMethod];
- [SomeClass hiddenClassMethod];
- }
- +(void) classMsg
- {
- printf("Inside classMsg()...\n");
- }
- @end
- // ===========================
- // = File: SomeClass.m
- // ===========================
- #import "SomeClass.h"
- // =================================
- // = Interface for hidden methods
- // =================================
- @interface SomeClass (hidden)
- +(void) hiddenClassMethod;
- -(void) hiddenInstanceMethod;
- @end
- // =====================================
- // = Implementation of hidden methods
- // =====================================
- @implementation SomeClass (hidden)
- +(void) hiddenClassMethod
- {
- printf( "Hidden class method.\n" );
- }
- -(void) hiddenInstanceMethod
- {
- printf( "Hidden instance method\n" );
- }
- @end
- // ================================
- // = Implementation for SomeClass
- // ================================
- @implementation SomeClass
- -(void) msg
- {
- printf("Inside msg()...\n");
- [self hiddenInstanceMethod];
- [SomeClass hiddenClassMethod];
- }
- +(void) classMsg
- {
- printf("Inside classMsg()...\n");
- }
- @end
有一个hidden的类别,在SomeClass的实现中调用了hidden的两个方法。
只包含SomeClass.h文件的main:
- //
- // main.m
- // Private Method
- //
- // Created by mac on 11-8-10.
- // Copyright 2011年 __MyCompanyName__. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "SomeClass.h"
- int main (int argc, const char * argv[])
- {
- SomeClass *ptr = [[SomeClass alloc] init];
- // Display message (including messages from hidden methods)
- [ptr msg];
- // Call a class method
- [SomeClass classMsg];
- // Compile warning (can't access hidden instance method)
- // [ptr hiddenInstanceMethod];
- // Compile warning (can't access hidden class method)
- // [SomeClass hiddenClassMethod];
- [ptr release];
- return 0;
- }
- //
- // main.m
- // Private Method
- //
- // Created by mac on 11-8-10.
- // Copyright 2011年 __MyCompanyName__. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "SomeClass.h"
- int main (int argc, const char * argv[])
- {
- SomeClass *ptr = [[SomeClass alloc] init];
- // Display message (including messages from hidden methods)
- [ptr msg];
- // Call a class method
- [SomeClass classMsg];
- // Compile warning (can't access hidden instance method)
- // [ptr hiddenInstanceMethod];
- // Compile warning (can't access hidden class method)
- // [SomeClass hiddenClassMethod];
- [ptr release];
- return 0;
- }
运行成功后应该可看到如下的显示:
- Inside msg()...
- Hidden instance method
- Hidden class method.
- Inside classMsg()...
内部调用hidden的两个方法是没问题的。在main的return之前有四行注释,把其中两行发送消息的注释取消,再编译后可能会遇到两种情况:
1、有警告,但是编译成功,直接运行后可以得到如下结果:
- Inside msg()...
- Hidden instance method
- Hidden class method.
- Inside classMsg()...
- Hidden instance method
- Hidden class method.
方法确实隐藏的(从警告中可以得知),但是可以连接到。
2、编译失败,Xcode4.2版本的编译器好像默认是Apple LLVM compiler 3.0,需要切换成LLVM GCC 4.2才能编译成功,编译成功后运行可以得到上述结果。
小结:详解在Objective_C编程中使用私有方法的内容介绍完了,希望通过讲解在Objective_C编程中如何使用私有方法的学习能对你有所帮助!