源码:http://files.cnblogs.com/ios8/TestPropertyDemo.zip

1 前言

最近有个疑惑 @interface中的属性和@property声明的属性有什么区别呢,经过上网搜索和实践验证,个人总结了几点,共同学习一下。

2 详述

例子

PropertyModel.h

  1. #import <Foundation/Foundation.h>
  2. @interface PropertyModel : NSObject{
  3. NSString* sex;
  4. @public NSString* name;
  5. @private NSString* classes;
  6. }
  7. @property(nonatomic,copy)NSString* sexName;
  8. @property(nonatomic,copy)NSString* classesName;
  9. @end

SubProModel.m

  1. #import "SubProModel.h"
  2. @implementation SubProModel
  3. -(void)testSub{
  4. //    self.newClasses =  self->classes;//Wrong:private property
  5. self.newSexName = self->sex;
  6. self.newName = self->name;
  7. }
  8. @end

main.m

  1. #import <Foundation/Foundation.h>
  2. #import"PropertyModel.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. NSLog(@"Hello, World!");
  7. PropertyModel* pro = [[PropertyModel alloc] init];
  8. //        NSString* temp = pro->sex;//Wrong:pro is protected
  9. NSString* temp = pro->name;
  10. }
  11. return 0;
  12. }

2.1 本类中

对于PropertyModel的@interface中的属性:

NSString* sex;

@public NSString* name;

@private NSString* classes;

sex为默认的protect属性只允许本类和子类访问。

name为共有的所有类都能访问。

classes为私有的只有子类访问。

2.2 本类访问

  1. #import "PropertyModel.h"
  2. @implementation PropertyModel
  3. -(void) testPro{
  4. self.sexName = sex;
  5. self.classesName = classes;
  6. }
  7. @end

可以直接调用属性名称访问。

2.3 外部类访问

  1. #import "SubProModel.h"
  2. @implementation SubProModel
  3. -(void)testSub{
  4. //    self.newClasses =  self->classes;//Wrong:private property
  5. self.newSexName = self->sex;
  6. self.newName = self->name;
  7. }
  8. @end

需要使用“->”访问指针形式访问。

3 结语

以上是所有内容,希望对大家有所帮助。多的就不说了,可以下载Demo详细观看他们的区别。

Demo下载地址:http://download.csdn.net/detail/u010013695/5539117

Objective的头文件@interface属性的更多相关文章

  1. UIButton 头文件常见属性和方法

    UIButton头文件常见属性 1.属性 contentEdgeInsets: default is UIEdgeInsetsZero.设置内容四边距,默认边距为0 @property(nonatom ...

  2. UILabel头文件常见属性

    text : default is nil 文本属性,默认值是 nil @property(nullable, nonatomic,copy) NSString *text; font : defau ...

  3. Viewing the interface of your Swift code,查看Swift代码的头文件的三种方法

      Technical Q&A QA1914 Viewing the interface of your Swift code Q:  How do I view the interface ...

  4. Objective-C声明在头文件和实现文件中的区别

    Objective-C声明在头文件和实现文件中的区别 转自codecloud(有整理) 调试程序的时候,突然想到这个问题,百度一下发现有不少这方面的问答,粗略总结一下: 属性写在.h文件中和在.m文件 ...

  5. jni.h头文件详解二

    作者:左少华 博客:http://blog.csdn.net/shaohuazuo/article/details/42932813 转载请注明出处:http://blog.csdn.net/shao ...

  6. jni.h头文件详解一

    1.jni.h头文件路径: /usr/lib/jvm/jdk_1.6.0_43/include/jni.h 2.jni.h头文件组成分析图: 3.下面通过上图进行分析讲解jni.h头文件. 一. jn ...

  7. 【转】 jni.h头文件详解(二)

    原文网址:http://blog.csdn.net/shaohuazuo/article/details/42932813 作者:左少华 博客:http://blog.csdn.net/shaohua ...

  8. NSObject头文件解析 / 消息机制 / Runtime解读 (一)

    NSObject头文件解析 当我们需要自定义类都会创建一个NSObject子类, 比如: #import <Foundation/Foundation.h> @interface Clas ...

  9. #include、#import与@class的使用与头文件循环引用问题

    #include #include <>:一般是对系统库文件的引用,编译器会去系统文件文件夹下查找. #include "xxx.h":一般是对自己定义文件的引用,编译 ...

随机推荐

  1. 修改windows默认的远程连接端口

    打开注册表,找到以下路径项,并修改为你想要的端口,重启服务器即可. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Serve ...

  2. RHEL7 添加网卡并设置多个IP地址

    首先在虚拟机管理界面中给该虚拟机添加一块虚拟网卡,VirtualBox虚拟机中要shutdown才能添加. [root@rhel7 ~]# nmcli device show | grep -i de ...

  3. RHEL7 在不同的环境中使用不同的网络配置文件

    比如,我们可以设置RHEL7 系统在公司时使用一个网卡配置文件:在家时则使用另外一个配置文件(可以根据不同的环境设置多个网卡配置文件). 网卡配置信息如下: [root@rhel7 ~]# nmcli ...

  4. Vacuum tube 真空管/电子管

    真空管/电子管的发明 1904, John Ambrose Fleming invented the two-electrode vacuum-tube rectifier, which he cal ...

  5. $(document).ready() 和 window.onload 方法比较

    说明 页面加载文档完毕后,浏览器会通过 Javascript 为 DOM 元素添加事件. Javascript 使用 window.onload 方法,而 jQuery 使用 $(document). ...

  6. Ubuntu12.04安装并配置Sublime Text 2

    Ubuntu是个好系统,Sublime Text 是个好编辑器. 下载&安装 个人习惯喜欢到官网下载软件,http://www.sublimetext.com/2 选择合适的包下载回来的格式是 ...

  7. Xcode之断点调试

    断点类型: 1.异常断点 异常断点是代码出现问题导致编译器抛出异常时触发的断点.它在断点导航器中设置.点击+号,选择Exception Breakpoint选项.如下图3-1所示 Exception选 ...

  8. 【MATLAB】matlabR2010a与vs2010联合编译设置

    在matlab中编译C++程序,首先要配置编译器>> mex -setupPlease choose your compiler for building external interfa ...

  9. 安装samba脚本

    Ubuntu_samba.sh #!/bin/sh echo "Ubuntu 14.04 文件服务器--samba的安装和配置" echo "更新源列表" su ...

  10. React(0.13) 定义一个动态的组件(属性)

    <!DOCTYPE html> <html> <head> <title>React JS</title> <script src=& ...