oc _属性

//  main.m

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Person.h"

#import"Student.h"

#import "Animal.h"

 

 

int main(int argc,constchar * argv[]) {

   

//    Person *per =[[Person alloc] init];

    //对象通过设置器对成员变量内容进行修该

//    [per  setName:@"你好"];

    //对象通过访问器来进行取值

//    NSLog(@"%@",[per name ]);

    //属性一共做了三件事

    // 1.声明了设置器,setter和访问器getter

    // 2.实现了设置器和访问器

    // 3.声明了一个成员变量,成员变量命名会在属性名前面加一个下划线

    //具体的数据储存,还是成员变量来完成的,属性只不过帮助程序员完成一些繁琐的工作,简化代码

    

//    Person *per =[[Person alloc] init];

//    [per hobby];

////    [per buhao];

//    Student *stu=[[Student alloc] init];

//    [stu setStuName:@"鹰王"];

//    NSLog(@"%@",[stu stuName]);

    

    //针对对象的属性,我们可以使用点语法,来获取对象的内容,也可以进行设置

//    stu.stuName=@"崔某";

//    NSLog(@"%@",stu.stuName);

    //通过点语法,可以对属性进行操作,大量节省了代码

    // 离=号近的是setter方法,其余的都是getter方法

    

    // KVC

    // key- value -coding

    //把属性名看成kvc中得key建,把要修改的值看成value,然后通过kvc的方法,把值赋值给指定的key

//    Student *stu=[[Student alloc] init];

//    [stu setValue:@"崔某" forKey:@"stuName"];

//    NSLog(@"%@",stu.stuName);

//    NSLog(@"%@",[stu valueForKey:@"stuName"]);

//    

//    [stu setValue:@"5756" forKey:@"Name"];

//    

//    NSLog(@"Name = %@",stu.Name);

//    NSLog(@"name = %@",stu.name);

//    

    // alloc init

   Animal *animalOne =[[Animalalloc]initWithColor:@"红色"size:20age:20 type:@"狗"];

    //通过点语法对对象的值进行修改和设置

    animalOne.color=@"黄";

    

   id animalTest = [AnimalAnimalWithColor:@"grey"size:3age:5 type:@"dg"];

   NSLog(@"idTest = %@",[animalTestcolor]);

     

    // 便利构造器

   Animal *animalTwo=[AnimalAnimalWithColor:@"橙色"size:10age:10 type:@"猫"];

    animalTwo.type=@"鸟";

   NSLog(@"%@",animalTwo.color);

    

    // KVC

    [animalOnesetValue:@"白色"forKey:@"Color"];

   NSLog(@"%@",[animalOnevalueForKey:@"color"]);

 

    

    

   return0;

}

 

//  Person.h

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Person :NSObject

// 属性

 

@property NSString *name;

//@property NSString *sex;

//@property NSInteger age;

 

// 属性的属性

// 1.读写控制 :readonly,readwrite

// readonly设置之后,属性就没有setter,默认是readwrite

// setter和getter的作用是给设置器和访问器的方法重新起一个名字,注意:设置器载明的设置时候不要忘记加:

 

@property()NSString *hobby;

 

// 第二部分 原子性控制

// 通过原子性atomic来监控实物在整个过程中又没有完成,但是一般来讲我们就是对数据的简单赋值,一般我们这部分用nanatomic

@property (atomic)NSString *sex;

 

// 第三部分语义 设置

// copy, assign, retain

// retain一般是对象类型会用到,比如我们自己写的类,还有NSArray会用

// assign一般是NSInteger,CGFloat会使用,因为他们在栈区,不需要内存管理,所以用asign

// copy 一般只有字符串会用copy

@property(nonatomic ,copy)NSString *color;

@property(nonatomic ,assign)NSInteger age;

@property(nonatomic ,assign)CGFloat score;

@property(nonatomic ,retain)NSArray *arr;

 

-(void)sayHi;

 

 

@end

//  Person.m

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "Person.h"

 

@implementation Person

 

// 属性的实现

// 关键词@sythesize,前面放属性名,后面放对应的成员变量

 

 

// zaiXCode4.5之后才开始不写的,所以之前的老的程序里,还有大量的@synthesize

//@synthesize name = _name;

 

 

-(void)sayHi{

   NSLog(@"%@",_name);

}

 

 

@end

//  Animal.h

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Animal :NSObject

 

// 四条属性

@property(nonatomic ,copy)NSString *color;

@property(nonatomic ,assign)CGFloat size;

@property(nonatomic ,assign)NSInteger age;

@property(nonatomic,copy)NSString *type;

 

// 自定义初始化

-(id)initWithColor:(NSString *)color

              size:(CGFloat)size

               age:(NSInteger)age

              type:(NSString *)type;

 

 

// 便利构造器

+ (id)AnimalWithColor:(NSString *)color

                 size:(CGFloat)size

                  age:(NSInteger)age

                 type:(NSString *)type;

 

 

@end

//  Animal.m

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "Animal.h"

 

@implementation Animal

-(id)initWithColor:(NSString *)color

              size:(CGFloat)size

               age:(NSInteger)age

              type:(NSString *)type{

   self=[superinit];

   if (self) {

       _color=color;

       _size=size;

       _age=age;

       _type=type;

        

    }

    return self;

}

 

 

+ (id)AnimalWithColor:(NSString *)color

                 size:(CGFloat)size

                  age:(NSInteger)age

                 type:(NSString *)type

{

   Animal *animal =[[Animalalloc]initWithColor:colorsize:sizeage:agetype:type];

   return animal;

    

}

 

 

@end

//  Student.h

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Person.h"

@interface Student :NSObject

 

 

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *Name;

 

// 1.学生的姓名

 

@property(nonatomic ,copy)NSString *stuName;

 

 

// 2.学生的年龄

@property(nonatomic ,assign)NSInteger stuAge;

 

 

// 3.学生的成绩

 

@property(nonatomic ,assign)CGFloat stuScore;

 

// 4.学生有一个人的属性

@property(nonatomic ,retain)Person *per;

 

// 5.学生有一个数组的属性

@property(nonatomic ,retain)NSArray *arr;

 

// 6.

@property(readonly,getter=isSelected,nonatomic,assign)BOOL selected;

 

 

@end

 

 

//  Student.m

//  OC04_属性

//

//  Created by dllo on 15/7/17.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "Student.h"

 

@implementation Student

 

@end

 

posted @ 2015-07-18 17:50  天涯ml  阅读(88)  评论(0编辑  收藏  举报