iOS前期OC训练OC_03:属性

来源:互联网 发布:else if在java中意思 编辑:程序博客网 时间:2024/05/18 12:35

//

//  main.m

//  OC04_属性

//

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

//  Copyright (c) 2015 Clare. 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.实现了设置器和fangwenq

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

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


//    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 = [[Studentalloc]init];

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

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

   NSLog(@"%@", [stuvalueForKey:@"stuName"]);

    

    

    // alloc init

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

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

    

    animalOne.color =@"粉色";

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

    // 便利构造器

   Animal *animalTow = [AnimalanimalWithColor:@"灰色"size:10age:10type:@""];

    

    animalTow.size =15;

   NSLog(@"%lg", animalTow.size);

    

    // KVC

    [animalOnesetValue:@"绿色"forKey:@"color"];

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

   return0;

}



//

//  Person.h

//  OC04_属性

//

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

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

//


#import <Foundation/Foundation.h>


@interface Person :NSObject

// 属性

@property NSString *name;

// NSString * 属性的类型,name属性名

// @property 相当于写了一个设置器和访问器以及其实现方法


// 属性的属性

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

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

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

//@property(readonly)NSString *hobby;

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


// 第二部分 原子性控制

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

@property(atomic)NSString *sex;


// 第三部分语义设置

// copy, assign, retain

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

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

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

@property(atomic, 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 Clare. All rights reserved.

//


#import "Person.h"


@implementation Person

// 属性的实现

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

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

//@synthesize name = _name;这个也包含在@property


- (void)sayHi{

   NSLog(@"%@",_name);

}

@end


//

//  Student.h

//  OC04_属性

//

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

//  Copyright (c) 2015 Clare. 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


//

//  Student.m

//  OC04_属性

//

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

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

//


#import "Student.h"


@implementation Student


@end


//

//  Animal.h

//  OC04_属性

//

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

//  Copyright (c) 2015 Clare. 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 Clare. 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) {

        _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 = [[Animal alloc] initWithColor:color size:size age:age type:type];

   return animal;

    

}

@end





1 0