objective-c - 类实例变量的声明


Instance variables can be declared in a class interface or implementation; however, declaring them in the public interface of a class would violate one of the key tenets of OOP—encapsulation. Therefore, the recommended practice is to declare instance variables in the class implementation; specifically, within a statement block placed immediately after the class @implementation directive.


实例变量(可理解为类成员变量)可以在接口(interface)的内部进行声明,也可以在实现部分(implementation)进行声明;


但是把实例变量声明在接口中的方式违反了面向对象的一个关键原则:封装性。


因此,建议把实例变量的声明放到类实现部分。实例变量声明紧随在@implementation 指令后面的花括号里面。如下所示

@implementation ClassName
{
   // 实例变量声明的地方
}
...
@end


如果一定要在interface中声明变量,为了不影响其encapsulation   可以采用属性property的方式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值