[41→100] object-c语法精要

看完征战Objective-CObjective-C面向对象初体验
发现Object-C的语法和C语言是一致的,用官方表述来说,就是OC是C的超集,完全支持C的语法。

在磁盘存储的物理形式上,OC头文件后缀名为.h,实现文件后缀名为.m,与c++混写的实现文件后缀名为.mm

准备工作:用xCode做oc开发必备技巧

/**
 *  必备插件:
 *  1. Xcode plug-in:快速生成格式化的注释 [VVDocumenter-Xcode](https://github.com/onevcat/VVDocumenter-Xcode)
 *  必须记住的快捷键:
 *  * 格式化代码(重排缩进):ctrl+i
 *  * 生成格式化注释: ///
 *  * 折叠、展开代码段 option+cmd+ ← | →
 *  * 在h和m文件间切换:cmd+ctrl+向上的方向键 
 */

直接写代码感受下吧。下面这个Main方法中展示的OC的基本语法结构,包括

  • 变量与数据类型
  • 条件控制语句
  • 循环控制语句
  • 函数
  • 面向对象的封装、继承、多态

参考代码:

Main中引入了两个类:一个父类People、一个子类Man

//
//  main.m
//  HelloObjC
//
//  Created by shitianci on 16/5/25.
//  Copyright © 2016年 Panda. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "People.h"
#import "Man.h"


/**
 *  求面积的函数
 *
 *  @param width  宽
 *  @param height 高
 *
 *  @return <#return value description#>
 */
double getArea(double width, double height){
    return width*height;
}


/**
 *  打印主函数的信息
 *
 *  @param argc 参数个数
 *  @param argv 参数列表
 *
 */
void showInfo(int argc, const char * argv[]) {
    NSLog(@"传入参数%d个", argc);
    for (int i = 0; i < argc; i++) {
        NSLog(@"argv[%d] = %s", i, argv[i]);
    }
    return;
}

/**
 *  入口函数
 *
 *  @param argc 参数个数
 *  @param argv 参数列表
 *
 *  @return 执行结果
 */
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /**
         *  必备插件:
         *  1. Xcode plug-in:快速生成格式化的注释 [VVDocumenter-Xcode](https://github.com/onevcat/VVDocumenter-Xcode)
         *  必须记住的快捷键:
         *  * 格式化代码(重排缩进):ctrl+i
         *  * 生成格式化注释: ///
         *  * 折叠、展开代码段 option+cmd+ ← | →
         *  * 在h和m文件间切换:cmd+ctrl+向上的方向键
         */
        @autoreleasepool {
            NSLog(@"Hello, %s, Welcome Object C World!" , "Panda");

            showInfo(argc, argv);

#pragma mark 变量与数据类型
            /**
             * 变量
             */
            {
                int i = 1;   //32bits, 4bytes
                short s = 1; //16bits, 2bytes
                long l = 1L;  //32bits, 4bytes
                long long ll = 1LL;  //64bits, 8bytes
                unsigned int ui = 1;
                signed int si = -1;

                float f = 0.1;
                double d = 100.0;

                char c = 'A';
                char *string = "string";
                NSLog(@"基础数据类型:\n  \t int: %d,\n  \t float: %f,\n \t double: %lf,\n \t char: %c,\n \t string: %s\n" , i,f,d,c,string);
            }

            /**
             *  表达式:跟C语言一致
             */
            {
                int a = 1;
                NSLog(@"\n\ta 等于 %d, a++ 等于 %d", a, a++);
                NSLog(@"\n\ta 等于 %d, ++a 等于 %d", a, ++a);
                NSLog(@"\n\ta 等于 %d, a++ 等于 %d", a, a++);

                int b = 2;
                int c = 5;
                int d = 31;
                float f = 100.0f;
                NSLog(@"\n\t  (((a+b)*c)%%d)/f 等于 %f", (((a+b)*c)%d)/f);
            }

#pragma mark 条件控制语句

            /**
             *  分支语句if
             */
            {
                int a = 1;
                int b = 2;
                if(a < b && 0 && YES){
                    NSLog(@"这句话是真的");
                }
                else if(!NO){
                    NSLog(@"这句话是假的");
                }
            }

            /**
             *  高级跳转语句goto
             */

            {
                int i = 0;
            a:

                i++;
                NSLog(@"i = %d", i);
                if(i < 5){
                    goto a;
                }
                else{
                    goto b;
                    NSLog(@"不会被执行到哦~");
                }
            b:
                NSLog(@"跳到b了");
            }

            /**
             *  switch分支语句
             */
            {
                int i = 10;
                switch (i) {
                    default:
                        NSLog(@"switch: i的值超出范围");
                        //                    break;
                    case 1:
                        NSLog(@"switch: i的值等于1");
                        break;
                    case 2:
                        NSLog(@"switch: i的值等于2");
                        break;


                }
            }

#pragma mark 循环控制语句
            {
                int i = 0;
                while (i < 5) {
                    i++;
                    NSLog(@"while: i = %d", i);
                }

                do {
                    i++;
                    NSLog(@" do …… while: i = %d", i);
                } while (i < 5);

                int j;
                for (j = 0; j < 100 ;j++) {
                    if(j > 10){
                        break;
                    }
                    else if(j > 5){
                        NSLog(@"continue: j = %d", j);
                        continue;
                    }
                    NSLog(@"for: j = %d", j);
                }
                NSLog(@"break: j = %d", j);
            }
        }

#pragma mark 函数
        {
            double width = 10.0;
            double height = 6.0;
            NSLog(@"width = %f, height = %f, area = %f", width, height, getArea(width, height));

        }

#pragma mark 面向对象

        {
            /**
             初始化
             */
            People *p1 = [[People alloc]init];
            People *p2 = [People new];
            NSLog(@"p1 - %p", p1);
            NSLog(@"p2 - %p", p2);

            /**
             *  成员变量和属性
             */
            NSLog(@"p1.peopleName - %@", p1.peopleName);
//            NSLog(@"p1.peopleName - %s", [p1 peopleName]);
            p2.peopleName = @"栗色";
            NSLog(@"p2.peopleName - %@", p2.peopleName);

            /**
             *  方法
             */
            NSLog(p2->_peopleOut);
            [p2 testSubtractMethod];
            [People testAddMethod];
            [p2 showWithA:1];
            [p2 showWithA:1 andB:2];
            [p2 :1 :2];

            People *p3 = [[People alloc]initWithPeopleName:@"王五" andPeopleAge:18];
            [p3 showBaseInfo];

            /**
             *  封装、继承、多态(oc只支持方法重写、不支持方法重载)
             */
            Man *man = [[Man alloc]init];
            [man showBaseInfo];
        }

    }

    return 0;
}
//
//  People.h
//  HelloObjC
//
//  Created by shitianci on 16/6/6.
//  Copyright © 2016年 Panda. All rights reserved.
//

#import <Foundation/Foundation.h>

/**
 *  人(oc不支持多继承)
 */
@interface People : NSObject

/**
 *  成员变量(默认为类内使用)
 */
{
@public //可以在类外用 ->方式 进行调用,(不推荐这样使用)
    NSString *_peopleOut;
    //    NSString *_peopleName;

@protected //在类内可以使用,在类外不行,子类可以使用(默认值)
    int _peopleAge;

@private //在类内可以使用,在类外不行,子类不可以使用(默认值)
    int _peopleSex;

@package //框架权限,在框架内相当于@protected,在框架外相当于@private

}


/**
 *  属性是成员变量的外部接口,替代相应的get、set方法
 */
@property(nonatomic, strong) NSString *peopleName;

- (void) testSubtractMethod;
+ (void) testAddMethod;

//方法名为 "showWithA:"
- (int) showWithA:(int) a;
//方法名为 "showWithA: andB:"
- (int) showWithA:(int) a andB:(int)b;

//方法名为 ": :"
- (int) :(int) a :(int)b;

- (void)showBaseInfo;
- (instancetype)initWithPeopleName:(NSString * )peopleName andPeopleAge:(int)peopleAge;
@end
//
//  People.m
//  HelloObjC
//
//  Created by shitianci on 16/6/6.
//  Copyright © 2016年 Panda. All rights reserved.
//

#import "People.h"

@implementation People

//成员
//@synthesize peopleName = _peopleName;

/**
 *  重写初始化方法
 *
 *  @return 对象本身
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        _peopleName = @"张三"; //系统会根据属性默认生成 _peopleName 这个成员变量
        _peopleOut = @"直接调用对象成员变量(不推荐)";
    }
    return self;
}

- (void)showBaseInfo
{

    NSLog(@"_peopleName: %@", _peopleName);
    NSLog(@"_peopleAge: %d", _peopleAge);
    NSLog(@"_peopleOut: %@", _peopleOut);
}

- (instancetype)initWithPeopleName:(NSString * )peopleName andPeopleAge:(int)peopleAge
{
    self = [self init];
    if (self) {
        _peopleName = peopleName; //系统会根据属性默认生成 _peopleName 这个成员变量
        _peopleAge = peopleAge;
    }
    return self;
}

- (void)testSubtractMethod
{
    NSLog(@"这是一个减号方法");
}

+ (void)testAddMethod
{
    NSLog(@"这是一个加号方法");
}

- (int)showWithA:(int)a
{
    NSLog(@"方法 showWithA 携带参数 a = %d", a);
    return a;
}

- (int)showWithA:(int)a andB:(int)b
{
    NSLog(@"方法 showWithA andB 携带参数 a = %d,b = %d", a, b);
    return a+b;
}

- (int) :(int) a :(int)b
{
    NSLog(@"方法 : : 携带参数 a = %d,b = %d", a, b);
    return a+b;
}

@end
//
//  Man.h
//  HelloObjC
//
//  Created by shitianci on 16/6/6.
//  Copyright © 2016年 Panda. All rights reserved.
//

#import "People.h"

@interface Man : People

@end
//
//  Man.m
//  HelloObjC
//
//  Created by shitianci on 16/6/6.
//  Copyright © 2016年 Panda. All rights reserved.
//

#import "Man.h"

@implementation Man

- (void)showBaseInfo
{
    NSLog(@"This is a Man。");
    [super showBaseInfo];
}


@end

Panda
2016-06-06

    原文作者:沉思的Panda
    原文地址: https://www.jianshu.com/p/6b84013d4d33
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞