Objective C 有没有 共有 私有 受保护的 成员变量概念?

一直以为是没有的,今天看官方文档,无意中竟然发现了,其实是有的。也怪自己,平时编码,在@interface 里面从来没有试过@private 一下,其实是有的,自动补全的提示也有。


Objective C 有跟 C++一样的成员变量的类型,但是还是有所不同的。

默认的不声明的情况下,是@protected的,可以主动声明为 公有私有或者受保护的,有一个表格:

Directive

Meaning

@private

The instance variable is accessible only within the class that declares it.

@protected

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

@public

The instance variable is accessible everywhere.

@package

Using the modern runtime, an @package instance variable has @public scope inside the executable image that implements the class, but acts like @private outside.

The @package scope for Objective-C instance variables is analogous to private_extern for C variables and functions. Any code outside the class implementation’s image that tries to use the instance variable gets a link error.

This scope is most useful for instance variables in framework classes, where @private may be too restrictive but @protected or @public too permissive.


里面解释的很清楚。就不多说啦!一般来讲,在obj C编程里,也不会太特意关注成员变量的 访问权限,默认protected的刚好大多满足类设计的需要,特别的,比如希望派生类不拥有父类的一些成员变量,就可以把父类的成员变量声明为@private。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值