zoukankan      html  css  js  c++  java
  • 黑马程序员——oc语言学习心得—— 属性声明和赋值

    黑马程序员——oc语言学习心得—— 属性声明和赋值

     

    1,在oc中所有类继承与终极父类Object
    2,声明字符变量采用N是string  *_xxx 实例变量一般以下划线开头
    3,在oc中方法以+ -号区分 -号开头是实例方法或对象方法  +号开头是类方法  前置用对象调用 后者用类名调用
    4,在xcode4以后声明@property 不用在写@snysize  自动生成get、set方法
    5,属性赋值可用.的方式或者指针赋值

    1. @interface Person : NSObject
    2. {
    3.     int _pid;
    4.     NSString *_name;
    5.     int _age;
    6.     NSString *_email;
    7.     NSString *_address;
    8. }
    9. //get set方法
    10. -(void)setpid:(int)pid;
    11. -(int)returnpid;
    12. //简便方法  实现属性声明方法自动实现setget方法
    13. @property (nonatomic)int pid;//nonatomic表示非原子,非线程安全atomic表示线程安全
    14. @property NSString *name;
    15. @property int age;
    16. @property NSString *email;
    17. @property NSString *address;
    18. @end
    复制代码


    1. @implementation Person
    2. //赋值
    3. -(void)setpid:(int)pid{
    4.     //通过指针访问
    5.     self->_pid=pid;
    6. }
    7. //取值
    8. -(int)returnpid{
    9.     //错误写法  self->return pid
    10.     return self->_pid;
    11. }
    12. //属性的实现 然后直接用.调用
    13. @synthesize name=_name;
    14. @synthesize age=_age;
    15. @synthesize email=_email;
    16. @synthesize address=_address;
    复制代码


    1. #import <Foundation/Foundation.h>
    2. #import "Person.h"
    3. #import "Person+Ext.h"
    4. int main(int argc, const char * argv[]) {
    5.     @autoreleasepool {
    6.         // insert code here...
    7.         NSLog(@"第一个函数!!");
    8.         //创建实例
    9.         Person *per=[[Person alloc]init];
    10.         //调用自己写的setpid方法赋值
    11.         [per setpid:1234567];
    12.         //调用取值方法returnpid返回新的值
    13.         int pid=[per returnpid];
    14.         NSLog(@"pid=%d",pid);
    15.         
    16.         
    17.         //用.调用实现赋值
    18.         
    19.         per.name=@"任子杰";
    20.         per.age=20;
    21.         per.email=@"xxxxxxx";
    22.         per.address=@"成都";
    23.         
    24.         //用.调用实现取值'
    25.         NSString *name=per.name;
    26.         int age=per.age;
    27.         NSString *email=per.email;
    28.         NSString *address=per.address;
    29.         
    30.         NSLog(@"%@\n%d\n%@\n%@",name,age,email,address);
    31.         
    32.         [per Test];
    33.         
    34.     }
    35.     return 0;
    36. }
    复制代码

     

     

     

     

     

     

     

     

  • 相关阅读:
    如何去重一个Oracle表
    配置Eclipse来开发Java 程序
    在windows上使用opera mini
    Oracle OLAP 介绍
    一个Batch作业调度系统构思
    how to Use Subversion with TortoiseSVN
    java official Design Pattern
    how to install ubuntu OS combined with Windows
    确保DWBI项目成功的几个关键点
    spinner 读取sqlite
  • 原文地址:https://www.cnblogs.com/zijie/p/4925940.html
Copyright © 2011-2022 走看看