OC 属性

//
//  main.m
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Classroom.h"
#import "Teacher.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
//        
        Person *xiaoGuiZi = [[Person alloc]init];
        //属性的使用
        //(一)消息语法
        //1.赋值
        [xiaoGuiZi setName:@"小鬼子"];
        //2.取值
        NSLog(@"%@",[xiaoGuiZi name]);
        //(二)点语法
        //1.赋值
        xiaoGuiZi.age = 20;
        //[xiaoGuiZi setAge:20];
        //xiaoGuiZi.sex = @"男";
        //2.取值
        NSLog(@"%d   %@", xiaoGuiZi.age, xiaoGuiZi.sex);
        
        
        Person *yanan = [[Person alloc]init];
        yanan.age = xiaoGuiZi.age;
        //[yanan setAge:[xiaoGuiZi age]];
         //test 方法符合getter书写格式,但不是getter
        //以下方法编译器通过,运行无错,但不符合规范
        //所以不要这么写
        NSLog(@"%d",yanan.test);
    
        Teacher *bobo = [[Teacher alloc] init];
        bobo.name = @"波波";
        Classroom *class19 = [[Classroom alloc]init];
        class19.teacher = bobo;
        
        class19.teacher.name = @"波霸";
        [[class19 teacher]setName:@"波霸"];

        NSString *name = [[NSString alloc]initWithFormat:@"张总"];
        Person *p = [[Person alloc]init];
        [p setName:name];
        [name release];
        NSLog(@"%lu",[p retainCount]);
        NSLog(@"%lu",[name retainCount]);
       // [name release];
        NSLog(@"%lu",[p retainCount]);
     //   NSLog(@"%lu",[name retainCount]);
        NSLog(@"%@", [p name]);
        NSString *newName = [[NSString alloc]initWithFormat:@"woca"];
        
    }
    return 0;
}
<pre name="code" class="objc">//
//  Person.h
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString *_name;
    NSString *_sex;
    int _age;
}
//@property(nonatomic, retain) NSString *name;//属性本质:setter,getter
//@property(nonatomic, assign) int age;
//基本数据类型的属性特征只能用assign
//@property(readwrite, nonatomic, retain) NSString *sex;
- (void)setName:(NSString *)name;
- (void)setSex:(NSString *)sex;
- (void)setAge:(int)age;
- (NSString *)name;
- (NSString *)sex;
- (int)age;
- (int)test;

//- (void)setName:(NSString *)name;
//- (NSString *)name;
@end

//

//  Person.m

//  oc05_属性

//

//  Created by Vision on 14-9-17.

//  Copyright (c) 2014 Vision. All rights reserved.

//


//
//  Person.m
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import "Person.h"

@implementation Person
//@synthesize name = _name;//属性的实现
- (int)test
{
    return _age;
}
- (void)dealloc;
{
    //复合对象销毁前对其对象类型成员变量———release
    [_name release];
    _name = nil;
    [_sex release];
    _sex = nil;
    [super dealloc];
}

- (void)setName:(NSString *)name
{
    if (_name != name) {
        [_name release];
        _name = [name retain];
    }
}
- (void)setSex:(NSString *)sex
{
    if (_sex != sex) {
        [_sex release];
        _sex = [sex retain];
    }
}
- (void)setAge:(int)age
{
    _age = age;
}
- (NSString *)name
{
    return [[_name retain]autorelease];
}
- (NSString *)sex
{
    return [[_sex retain]autorelease];                                                                           
}
- (int)age
{
    return _age;
}

@end

//
//  Classroom.h
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import <Foundation/Foundation.h>
@class Teacher;
@interface Classroom : NSObject
@property(retain) Teacher *teacher;
@end

//
//  Classroom.m
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import "Classroom.h"

@implementation Classroom

@end

//
//  Teacher.h
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import "Person.h"

@interface Teacher : Person


@property(nonatomic, retain) NSString *teach;
@property(nonatomic, retain) NSString *answerQ;

- (void)sayHi;

@end

//
//  Teacher.m
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import "Teacher.h"

@implementation Teacher
- (void)sayHi
{
    NSLog(@"我叫:%@年龄:%d 性别:%@ 技能1:%@ 技能2:%@",
          [self name], [self age], [self sex] ,[self teach] , _answerQ);
    //注意:
    //1.父类通过属性生成的成员变量子类无法直接访问
    //2.当前类定义的属性对应的成员变量可以直接访问
}
@end

//
//  Student.h
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import "Person.h"

@interface Student : Person
{
    int _num;
    NSString *_listen;
}
- (void)setNum:(int)num;
- (void)setListen:(NSString *)listen;
- (int)num;
- (NSString *)listen;

@end

//
//  Student.m
//  oc05_属性
//
//  Created by Vision on 14-9-17.
//  Copyright (c) 2014年 Vision. All rights reserved.
//

#import "Student.h"

@implementation Student
- (void)setNum:(int)num
{
    _num = num;
}
- (void)setListen:(NSString *)listen
{
    if (_listen != listen) {
        [_listen release];
        _listen = [listen retain];
    }
}
- (int)num
{
    return _num;
}
- (NSString *)listen
{
    return [[_listen retain]autorelease];
}
@end
相关文章
相关标签/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
公众号推荐
   一个历史类的公众号,欢迎关注
一两拨千金