码迷,mamicode.com
首页 > 其他好文 > 详细

OC1_点语法

时间:2015-06-16 18:19:40      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:

//
//  Dog.h
//  OC1_点语法
//
//  Created by zhangxueming on 15/6/16.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject
//{
//    NSString *_name;
//    NSInteger _age;
//}

- (id)init;

//方法一
//- (NSString *)name;
//- (NSInteger)age;
//
//- (void)setName:(NSString *)name;
//- (void)setAge:(NSInteger)age;

//方法二:
//@property 关键字展开setter getter方法的声明

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

//- (NSInteger)age;
//- (void)setAge:(NSInteger)age;
//@property NSInteger age;

//方法三
//展开setter getter方法的声明及synthesize语句
//展开@synthesize name = _name;
//@synthesize age = _age;

@property NSString *name;
@property NSInteger age;


- (void)printDog;

@end
//
//  Dog.m
//  OC1_点语法
//
//  Created by zhangxueming on 15/6/16.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "Dog.h"

@implementation Dog

- (id)init
{
    if (self=[super init]) {
        _name = @"小黑";
        _age  = 12;
    }
    return self;
}
//方法一:
//- (NSString *)name
//{
//    return _name;
//}
//
//- (NSInteger)age
//{
//    return _age;
//}
//
//- (void)setName:(NSString *)name
//{
//    _name = name;
//}
//
//- (void)setAge:(NSInteger)age
//{
//    _age = age;
//}

//方法二
//@synthesize 关键字展开 setter getter方法的实现
//展开了 _name _age
//@synthesize name=_name;
//@synthesize age = _age;

//方法三: 省约@synthesize



- (void)printDog
{
    NSLog(@"name = %@ , age = %li", [self name], [self age]);
}


@end
//
//  main.m
//  OC1_点语法
//
//  Created by zhangxueming on 15/6/16.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"

//点语法: 方便其他程序员转移到ios开发

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Dog *dog = [[Dog alloc] init];
        [dog printDog];
        
        [dog setName:@"小白"];
        dog.name = @"小白";//<==> [dog setName:@"小白"]
        dog.age = 20;     //<==> [dog setAge:20];
        [dog printDog];
        
        //<==> [dog name] [dog age];
        NSLog(@"name = %@ age = %li", dog.name, dog.age);
    }
    return 0;
}

 

OC1_点语法

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4581196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!