• OC1_点语法


    //
    //  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;
    }
  • 相关阅读:
    MySQL统计逗号分隔字段元素的个数
    MySQL学习(十)索引
    MySQL学习(十一)为什么不推荐使用uuid和雪花id
    1.计算机系统概论
    MySQL学习(十二)排序原理
    MySQL学习(九)frm与ibd了解
    MySQL学习(十一)B树与B+树了解
    MySQL 某一列的值加入到另一列
    MySQL学习(十三)InnoDB
    IOSios微信打开网页键盘弹起后页面上滑,导致弹框里的按钮响应区域错位
  • 原文地址:https://www.cnblogs.com/0515offer/p/4581196.html
Copyright © 2020-2023  润新知