Objective-C 中public、protected、private 的使用
fmms
13年前
<p>Objective-C中,类的实例化变量的范围有@private、@protected、@public。他们代表的意思和C++中相同,只是前面添加了一个@符号。下面介绍一下他们代表的范围:</p> <table class="ke-zeroborder" border="0" cellspacing="0" cellpadding="2"> <tbody> <tr> <td>指令</td> <td>意思</td> </tr> <tr> <td> <p>@private</p> </td> <td>作用范围只能在自身类</td> </tr> <tr> <td>@protected</td> <td>作用范围在自身类和继承自己的子类,什么都不写,默认是此属性。</td> </tr> <tr> <td>@public</td> <td>作用范围最大,在任何地方</td> </tr> </tbody> </table> <p>下面是官方的图:</p> <p><a href="/misc/goto?guid=4959500540007304927" rel="nofollow"><img title="image" alt="Objective-C 中public、protected、private 的使用" src="https://simg.open-open.com/show/1b513b1d317d48127087eb44b084f2a8.png" width="284" height="242" /></a></p> <p>具体的可以参考:<a href="/misc/goto?guid=4959500540096662858" rel="nofollow">http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-SW1</a></p> <p></p> <p>下面是一个例子,来说明他们之间的关系:</p> <p>实现一个Boss类:</p> <pre class="brush:cpp; toolbar: true; auto-links: false;"> #import <foundation foundation.h=""> <span class="referer">@interface</span> Boss : NSObject { <span class="referer">@private</span> int age; @protected NSString *job; } @end </foundation></pre>实现一个Worker类继承Boss类: <pre class="brush:cpp; toolbar: true; auto-links: false;">#import <foundation foundation.h=""> #import "Boss.h" <span class="referer">@interface</span> Worker : Boss { NSString *name; <span class="referer">@private</span> NSString *evaluation; @protected float wage; <span class="referer">@public</span> NSString *boss; } - promoteTo:newPosition; @end </foundation></pre>在Worker的.m文件中添加: <pre class="brush:cpp; toolbar: true; auto-links: false;"> #import "Worker.h" @implementation Worker - promoteTo:newPosition { id old = job; job = newPosition; return old; } -(NSString *)description { return [NSString stringWithFormat:@"Worker name:%@,evaluation:%@,job:%@,wage:%f,boss:%@",name,evaluation,job,wage,boss]; } @end</pre> <p>如果我们在Worker中调用Boss中的私有变量age,会报错:</p> <p><img title="image" alt="Objective-C 中public、protected、private 的使用" src="https://simg.open-open.com/show/f3a1a896912009f13769be6a6608fa5c.png" width="391" height="65" /></p> <p>但是调用job是没有问题的。</p> <p>下面在其他类中调用Worker类:</p> <pre class="brush:cpp; toolbar: true; auto-links: false;">Worker *ceo = [[Worker alloc] init]; ceo->boss = @"XX"; [ceo promoteTo:@"YY"]; NSLog(@">>>=%@",[ceo description]); </pre> <p>运行结果:</p> <p><img title="image" alt="Objective-C 中public、protected、private 的使用" src="https://simg.open-open.com/show/abcdf7dce6737cb4f3cb2f28cb52ea0f.png" width="422" height="27" /></p> <p>但是调用protected的属性会报错:</p> <p><img title="image" alt="Objective-C 中public、protected、private 的使用" src="https://simg.open-open.com/show/0e3fd8a00c285e82014b3955d1bf22b0.png" width="413" height="25" /></p> <p>所以当一些比较重要的属性,不能随便更改的时候,要用private,这样避免其他继承类修改此属性。</p> <p>文章出处:<a href="/misc/goto?guid=4959500540176191654" rel="nofollow">http://wangjun.easymorse.com/?p=1615</a></p>