zoukankan      html  css  js  c++  java
  • Objective--C随笔2016年8月7日

    一、Objective-C语言的特点

    1、支持C语言语法,是一个高效的编程语言。

    2、作为C语言面向对象的扩展,支持完整的面向对象的编程特性。

    3、简洁而优雅的编程风格使得编写程序与阅读代码都变得格外清晰。

    4、兼容性好,可以在同一个项目中同时使用OC与C++,也可以在项目中导入由C、C++等语言编写的库文件。

    如何学好这门语言?

    OC语言语法预览:

    C语言实现文件.c  C++实现文件.cpp  OC实现文件.m  OC&&C++实现文件.mm  头文件都是.h

    类的定义:

    @interface SimpleClass:NSObject

    @end

    类的属性声明:

    @interface Person:NSObject

    @property NSString *firstName;

    @property NSString *lastName;

    @end;

    @property NSNumber *yearOfBirth;

    @property int yearOfBirth;

    @property (readonly) NSString *firstName;

    减号方法(普通方法又称对象方法或实例方法)申明

    加号方法(类方法,又称静态方法)申明

    类的实现:

    #import "XYZPerson.h"

    @implementtation XYZPerson

    @end

    二、

      OOP(Objective Oriented Programming)--面向对象编程

      OOA(Object-Oriented Analysis)--面向对象分析

      OOD(Object Oriented Design)--面向对象设计

    三、

      C++:1983--贝尔实验室。Simula 67 复杂,静态

      Objective--C: 1988--乔布斯NextStep。Smaltalk 简单,动态

    四、类和对象的基本概念

      1、创建类、得到对象。-->类的实例化。

      

     1       //实例化对象
     2         /*
     3          [类名 方法名]
     4          [对象名 方法名]
     5          alloc - 为对象分配内存空间
     6          init - 进行初始化操作
     7          */
     8         People *p1 = [[People alloc] init];//推荐
     9         People *p2 = [People new];
    10 
    11       NSLog(@"%p", p1);
    12         NSLog(@"%p", p2);

      2、成员变量的声明和使用。属性的声明和使用

     1 #import <Foundation/Foundation.h>
     2 //类内使用成员变量。类外使用成员属性。
     3 @interface People : NSObject
     4 //声明成员变量
     5 {
     6     @public
     7     NSString *_peopleName;
     8     int _peopleAge;
     9     int _peopleSex;
    10 }
    11 //属性为了让类外可以访问成员变量
    12 //属性就是成员变量的外部接口
    13 @property (nonatomic, strong) NSString *peopleName;//声明属性
    14 
    15 @end
     1 #import "People.h"
     2 
     3 @implementation People
     4 
     5 - (instancetype) init
     6 {
     7     if (self = [super init])
     8     {
     9         //类内调用成员变量而不是属性,属性是给类外使用的。
    10         self.peopleName = @"张三";
    11     }
    12     return self;
    13 }
    14 
    15 @end

    后记:练习的源代码:

     1 #import <Foundation/Foundation.h>
     2 #import "People.h"
     3 
     4 int main(int argc, const char * argv[])
     5 {
     6     @autoreleasepool
     7     {
     8         //调用方法使用[]
     9         People *p1 = [[People alloc] init];
    10         int a = [p1 report];
    11         NSLog(@"%d", a);
    12 //        [People report1];
    13         int a1 = [p1 showWithA:10];
    14         NSLog(@"%d", a1);
    15         int a2 = [p1 showWithA:10 andB:20];
    16         NSLog(@"%d", a2);
    17     }
    18     return 0;
    19 }
    20 
    21 #import <Foundation/Foundation.h>
    22 /*
    23  1、 - 代表对象方法就(实例方法):用对象名来调用   +代表类方法:用类名来调用
    24     加号方法和减号方法可以互相调用,当然需要类名和实例化变量,加号方法不能调用成员变量。
    25  2、- (int) 返回值类型
    26  3、:(int)x - :代表有参数,(int)代表参数类型,a代表参数名
    27  4、函数名(方法名) - 去掉方法类型,去掉参数类型、去掉参数名,剩下的就是方法名
    28  */
    29 @interface People : NSObject
    30 
    31 - (int)report;
    32 + (void)report1;
    33 
    34 - (int)showWithA:(int)a;
    35 //showWithA: andB:(函数名/方法名)
    36 - (int)showWithA:(int)a andB:(int)b;
    37 
    38 
    39 @end
    40 
    41 #import "People.h"
    42 
    43 @implementation People
    44 
    45 {
    46     NSString *_peopleName;
    47 }
    48 
    49 static NSString *_peopleName1;
    50 
    51 - (int)report
    52 {
    53     NSLog(@"report");
    54     [People report1];
    55     _peopleName = @"123";
    56     return 20;
    57     
    58 }
    59 
    60 + (void)report1
    61 {
    62     NSLog(@"report1");
    63 //    [[People alloc] report];//死循环
    64     _peopleName1 = @"张三";
    65     
    66 }
    67 
    68 - (int)showWithA:(int)a
    69 {
    70     return a;
    71 }
    72 - (int)showWithA:(int)a andB:(int)b
    73 {
    74     return a + b;
    75 }
    76 
    77 
    78 @end

      

  • 相关阅读:
    P1197 [JSOI2008]星球大战[并查集+图论]
    P1955 [NOI2015]程序自动分析[离散化+并查集]
    取模运算律[简单数学]
    P1462 通往奥格瑞玛的道路[最短路+二分+堆优化]
    P1330 封锁阳光大学[搜索+染色]
    P1168 中位数[堆 优先队列]
    P2661 信息传递[最小环+边带权并查集]
    P1080 【NOIP 2012】 国王游戏[贪心+高精度]
    P2085 最小函数值[优先队列]
    【转】priority_queue的用法
  • 原文地址:https://www.cnblogs.com/songlei0601/p/5745816.html
Copyright © 2011-2022 走看看