iOS OC04_属性

//

//  Student.h

//  OC04_属性

//

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

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Person.h"

@interface Student : NSObject

// 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





//

//  Person.h

//  OC04_属性

//

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

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Person : NSObject

// 属性

@property NSString *name;

- (void)sayHi;

// 属性的属性

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

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

// settergetter

// settergetter的作用是给设置器和访问器的方法重新起一个名字,注意,设置器在名的设置的时候别忘了:(冒号)

@property(setter=nihao:,getter=buhao)NSString *hobby;

// 2.原子性控制

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

@property(atomic)NSString *sex;

-(void)sayHi;

// 3. 语义设置

// copy assign rutain

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

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

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



@property(nonatomic,copy)NSString *colour;


@property(nonatomic,assign)NSInteger *age;






@end










//

//  Person.m

//  OC04_属性

//

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

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import "Person.h"


@implementation Person

// 属性的实现

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


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

//@synthesize name = _name;


- (void)sayHi{

    NSLog(@"%@",_name);

}


// 属性一共做了三件事

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

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

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

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







@end








//

//  Animal.h

//  OC04_属性

//

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

//  Copyright (c) 2015 zhozhicheng. 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 zhozhicheng. All rights reserved.

//


#import "Animal.h"


@implementation Animal



- (id)initWithColor:(NSString *)color

               size:(CGFloat)size

                age:(NSInteger)age

               type:(NSString *)type{

    

    self =[super init];

    if (self) {

        _age=age;

        _color=color;

        _size=size;

        _type=type;

        

    }return self;

    

}


+ (id)animalWithColor:(NSString *)color

                 size:(CGFloat)size

                  age:(NSInteger)age

                 type:(NSString *)type{

    Animal *animal =[[Animal alloc] initWithColor:color size:size age:age type:type];

    return animal;

}


@end












//

//  main.m

//  OC04_属性

//

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

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Person.h"

#import "Student.h"

#import "Animal.h"

int main(int argc, const char * argv[]) {

 

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

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

    [per setName:@"你好"];

    //

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

//    

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

//    [per nihao:@"1"];

//    [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"]);

    

    

    

    // alloc init

    

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

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

    animalOne.color=@"粉色";

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

    

    

    // 便利构造器

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

    animalTwo.type=@"商帅";

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

    

    

    //kvc

    [animalOne setValue:@"白色" forKey:@"color"];

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

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值