1.点语法(找出不合理的地方)
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    int _age;
}
- (void)setAge:(int)age;
- (int)age;
@end

@implementation Person
{
    int _age;
}
- (void)setAge:(int)age
{
    _age = age;
    // 会引发死循环
    // self.age = age;
}
- (int)age
{
    return _age;
    // 会引发死循环
    // return self.age;
}
@end



2.@property
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    // no的set和get方法全部都手动实现了,因此系统就不会再生成_no这个成员变量了,只实现一个可产生
    int _no;
}
@property int age;
@property int no;
- (void)test;
@end

@implementation Person
- (void)test
{
    NSLog(@"年龄是%d, 号码是%d", _age, _no);
}

- (void)setAge:(int)age
{
    _age = age;
}

// _no的set和get方法全部都手动实现了,因此系统就不会再生成_no这个成员变量了
- (void)setNo:(int)no
{
    _no = no;
}

- (int)no
{
    return _no;
}
@end


3.成员变量的作用域
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    int _age;
    
@public
    int _no;
    int _weight;
    
@private
    int _height;
    
@protected
    int _money;
}
@end

@interface Student : Person
- (void)test;
@end

@implementation Student
{
    int _money;
@public
    int _friendsCount;
}
- (void)test
{
    _age = 10;
    _no = 1;
    
    // 私有的不能在子类中访问,即在implementation中声明的变量、或在声明中说明为私有的
    // _name = @"Jack";
    _money = 100;
    
    // 私有的不能在子类中访问
    // _height = 170;
}
@end

int main()
{
    Student *s = [Student new];
    s->_no = 1;
    //@propected类型的变量只能在当前类和子类中访问
    //s->_age = 20;
    // 在main函数后面的成员变量都不能直接访问
    //s->_name = @"Rose";
    // 在main函数后面的成员变量都不能直接访问
    //s->_color = 10;
    s->_friendsCount = 500;
    return 0;
}

@implementation Person
{
    NSString *_name;
@public
    int _color;
}
@end

点语法、property、self注意的更多相关文章

  1. OC self = [super init] , 点语法 , @property

    OC self = [super init] , 点语法 , @property 构造方法为啥这么写? self = [super init]; [super init] 的结果可能有三种: 第一种: ...

  2. 网络编程-Python高级语法-property属性

    知识点: 一.什么是property属性? 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法,Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最 ...

  3. linux设备树笔记__dts基本概念及语法【转】

    转自:http://www.360doc.com/content/15/1113/11/15700426_512794532.shtml 设备树手册(Device Tree Usage)原文地址:ht ...

  4. vb的property 和event

    Event 语句 定义用户自定义的事件. 语法 [Public] Event procedurename [(arglist)] Event 语句包含下面部分: 部分 描述 Public 可选的.指定 ...

  5. WPF样式中TargetType 属性 (Property) 和 x:Key 属性 (Attribute)

    如第一个示例所示,如果将 TargetType 属性设置为 TextBlock 而不为样式分配 x:Key,样式就会应用于所有 TextBlock 元素.这种情况下,x:Key 隐式设置为 {x:Ty ...

  6. Objective-C 关键字:retain, assgin, copy, readonly,atomic,nonatomic

    声明式属性的使用:声明式属性叫编译期语法 @property(retain,nonatomic)Some *s; @property(参数一,参数二)Some *s; 参数1:retain:修饰引用( ...

  7. Objective-C 30分钟入门教程

    Objective-C 30分钟入门教程 我第一次看OC觉得这个语言的语法有些怪异,为什么充满了@符号,[]符号,函数调用没有()这个,但是面向对象的高级语言也不外乎类,接口,多态,封装,继承等概念. ...

  8. Objective-C汇总

    Objective  C(20世纪80年代初) 一.OC语言概述 .1985年,Steve  Jobs成立了NeXT公司 .1996年,12月20日,苹果公司宣布收购了NeXT  softwar ...

  9. 21)pom 中的缺省值(default properties)

    1 引言 项目中build 时用到了maven-jar-plugin ,其中有一个 ${project.build.directory} <plugin> <artifactId&g ...

  10. Objective-C面向对象(一)

    1.类和对象 1.1定义类 面向对象的程序设计中有两个重要概念:类(class)和对象(object),类事某一批对象的抽象,对象是一个具体存在的实体. Objective-C定义类需要氛围2个步骤 ...

随机推荐

  1. Marshal 类的内存操作的一般功能

    Marshal类 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法. 命名空间:System.Runtim ...

  2. sffs

    因为微软收购了Skype,就减少了对linux版软件的支持,官方下载页面已找不到了.只有这一个地址了.要下的尽早.http://download.skype.com/linux/skype-ubunt ...

  3. Magnolia-CMS安装配置

    Magnolia-CMS安装配置 Magnolia-CMS安装配置 介绍:Magnolia 是一个开源基于Java的Web内容管理系统(CMS),构建在Java内容知识库标准(JSR-170).它适合 ...

  4. Android 抓包,监控流量工具之 mitmproxy

    转:http://greenrobot.me/devpost/how-to-debug-android-http-get-started/ mitmproxy实践教程之调试 Android 上 HTT ...

  5. 初学swift笔记运算符(二)

    swift 中的运算符 import Foundation //字符类型 var char: Character = "a" char="b" println( ...

  6. OpenCV2.4.9+VS2012安装与配置

    需要下载并安装Visual Studio 2012 然后在OpenCV官网下载安装OpenCV2.4.9 for Windows,网址为http://opencv.org/downloads.html ...

  7. Linux08--Shell程序设计03 shell script

    第一个Shell脚本——HelloWorld [root@localhost ~]# vi sh01.sh #!/bin/bash #!表明使用哪种shell # this is my first s ...

  8. 在Linux上怎么安装和配置DenyHosts工具

    使用DenyHosts能够进行自动屏ip的功能,掌握DenyHosts在Linux系统中的安装是很有必要的,那么在Linux系统中要如何安装DenyHosts工具呢?安装后又要如何配置呢?这都是用户需 ...

  9. 拓扑排序(TopologicalSort)算法

    拓扑排序算法应用: 有些事情做都需要按照流程的去做,比如你准备约你小女友去影院看速度与激情7大片,首先你想的是我怎么到达影院,然后达到影院,你可以先买票,或者等小女友来了一起买票,然后一起进电影大厅. ...

  10. LFM 隐语义模型

    隐语义模型: 物品       表示为长度为k的向量q(每个分量都表示  物品具有某个特征的程度) 用户兴趣 表示为长度为k的向量p(每个分量都表示  用户对某个特征的喜好程度) 用户u对物品i的兴趣 ...