zoukankan      html  css  js  c++  java
  • 【学习笔记】【OC语言】创建对象

    一、定义OC的类和创建OC的对象
    要描述OC中的类分2大步骤:类的声明、类的实现(定义)。跟函数类似,函数有分声明和定义
    1.类的声明
    1》代码编写
    *定义一个Car类,拥有2个属性:轮子数、时速,1个行为:跑
    *类名属性的命名规则:标示符的规则
    *类名的命名规范:有意义、驼峰标识、首字母大写

    #import <Foundation/Foundation.h>
    // 类的声明
    @interface Car : NSObject
    {
        @public
        int wheels; // 多少个轮子
        int speed; // 时速
    }
    - (void)run; // 跑的行为
    @end

    2》成员变量
    @interface的大括号{}中声明的变量:wheels、speed
    @interface的大括号和函数的大括号是不一样的
    默认会初始化为0

    3》@public
    @public可以让Car对象的wheels和speed属性被外界访问

    4》NSObject
    加上:NSObject的目的是让Car类具备创建对象的能力

    2.类的实现

    // 类的实现
    @implementation Car
    - (void) run
    {
        NSLog(@"%i个轮子,%i时速的车子跑起来了", wheels, speed);
    }
    @end

    3.创建对象
    1》代码编写

    // 主函数
    int main()
    {
        // 创建车子对象
        Car *c = [Car new];
        c->wheels = 3;
        c->speed = 300;
        
        [c run];
        return 0;
    }

    2》main函数的代码分析、内存分析(对象在内存中有成员)
    *[Car new]每次都会创建出新的对象,并且返回对象的地址,那么就应该用一个指针变量保存对象的地址
    Car *c = [Car new];
    用一个指针变量c指向内存中的Car对象    
    *设置车子对象的属性
    跟用指向结构体的指针访问结构体属性一样,用->
    c->wheels = 3;
    c->speed = 300;

    4.创建多个Car对象
    *分别只设置wheels、speed属性
    Car *c1 = [Car new];
    c1->wheels = 4;
    Car *c2 = [Car new];
    c2->speed = 250;
    [c1 run];


    *1个赋值给另一个,然后修改属性
    Car *c1 = [Car new];
    c1->wheels = 4;
    c1->speed = 250;
    Car *c2 = c1;
    c2->wheels = 3;
    [c1 run];

    5.面向对象封装的好处
    更加接近人类的思考方式
    只需要关注对象,不需要关注步骤

    6.对象与函数参数
    对象成员变量作为函数参数
    指向对象的指针作为函数参数
    修改指向指向对象的成员
    修改指针的指向

    二、类的声明和实现
    1.@interface和@implementation的分工

    @interface就好像暴露在外面的时钟表面
    @implementation就好像隐藏在时钟内部的构造实现

    2.声明和定义多个类

    3.常见错误
    只有类的声明,没有类的实现
    漏了@end
    @interface和@implementation嵌套
    两个类的声明嵌套
    成员变量没有写在括号里面
    方法的声明写在了大括号里面

    4.语法细节
    成员变量不能在{}中进行初始化、不能被直接拿出去访问
    方法不能当做函数一样调用
    成员变量方法不能用static等关键字修饰,别跟C语言混在一起(暂时忽略)
    类的实现可用写在main函数的后面,主要在声明后面就行了

    5.OC方法和函数的区别
    OC方法只能声明在@interface和@end之间,只能实现在@implementation和@end之间。也就是说OC方法不能独立于类存在
    C函数不属于类,跟类没有联系,C函数只归定义函数的文件所有
    C函数不能访问OC对象的成员
    低级错误:方法有声明,但是实现的时候写成了函数

    6.OC的方法注意
    方法只有声明,没有实现(经典错误)
    方法没有声明,只有实现(编译器警告,但是能调用,OC的弱语法)
    编译的时候:访问没有的成员变量直接报错,访问没有的方法,只是警告

    7.@implementation
    没有@interface,只有@implementation,也是能成功定义一个类的

    @implementation Car : NSObject
    {
        @public
        int wheels; // 多少个轮子
        int speed; // 时速
    }
    - (void) run
    {
        NSLog(@"%i个轮子,%i时速的车子跑起来了", wheels, speed);
    }
    @end

    @implementation中不能声明和@interface一样的成员变量

    三、方法
    设计一个Caculator计算器类,它拥有计算的功能(行为)
    1.不带参数的方法
    设计一个返回PI的方法

    // 方法声明
    - (double)pi;
    // 方法实现
    - (double)pi
    {
        return 3.14;
    }

    方法调用

    2.带一个参数的方法
    设计一个计算平方的方法

    // 方法声明
    - (double)square:(double)number;
    // 方法实现
    - (double)square:(double)number
    {
        return number * number;
    }

    方法调用


    3.带多个参数的方法
    设计一个计算和的方法

    // 方法声明
    - (double)sumOfNum1:(double)num1 andNum2:(double)num2;
    // 方法实现
    - (double)sumOfNum1:(double)num1 andNum2:(double)num2
    {
        return num1 + num2;
    }

    方法调用

    4.方法名注意
    冒号也是方法名的一部分
    同一个类中不允许两个对象方法同名

    四、匿名对象
    属性访问
    [Car  new]->speed = 200;
    方法调用
    [ [Car  new]  run];

    五、代码

    1.第一个OC类

     1 /*
     2  类名:Car
     3  属性:轮胎个数、时速(速度)
     4  行为:跑
     5  */
     6 
     7 // 因为使用了NSObject
     8 #import <Foundation/Foundation.h>
     9 
    10 // 完整地写一个函数:函数的声明和定义(实现)
    11 // 完整地写一个类:类的声明和实现
    12 
    13 // 1.类的声明
    14 // 声明对象的属性、行为
    15 // : NSObject 目的是:让Car这个类具备创建对象的能力
    16 @interface Car : NSObject
    17 {// 用来声明对象属性(实例变量成员变量,默认会初始化为0)
    18     // @public可以让外部的指针间接访问对象内部的成员变量
    19     @public
    20     int wheels; // 轮胎个数
    21     int speed; // 时速(xxkm/h)
    22 }
    23 
    24 // 方法(行为):方法名、参数、返回值(声明、实现)
    25 // 只要是OC对象的方法,必须以减号 - 开头
    26 // OC方法中任何数据类型都必须用小括号()扩住
    27 // OC方法中的小括号():括住数据类型
    28 - (void)run;
    29 
    30 @end
    31 
    32 // 2.类的实现
    33 // 用来实现@inteface中声明的方法
    34 @implementation Car
    35 // 方法的实现(说清楚方法里面有什么代码)
    36 
    37 - (void)run
    38 {
    39     NSLog(@"车子跑起来了");
    40 }
    41 
    42 @end
    43 
    44 int main()
    45 {
    46     // 在OC中,想执行一些行为,就写上一个中括号[行为执行者 行为名称]
    47     // 利用类来创建对象
    48     // 执行了Car这个类的new行为来创建新对象
    49     
    50     // 定义了一个指针变量p,p将来指向的是Car类型的对象
    51     // [Car new]每次都会创建出一个新对象,并且会返回新对象本身(新对象的地址)
    52     Car *p = [Car new];
    53     
    54     
    55     Car *p2 = [Car new];
    56     p2->wheels = 5;
    57     p2->speed = 300;
    58     [p2 run];
    59     
    60     // 给p所指向对象的wheels属性赋值
    61     p->wheels = 4;
    62     p->speed = 250;
    63     
    64     // 给p所指向对象发送一条run消息
    65     [p run];
    66     
    67     NSLog(@"车子有%d个轮子,时速位:%dkm/h", p->wheels, p2->speed);
    68     
    69     return 0;
    70 }

    2.第二个OC类

     1 /*
     2  3  类名:Person
     4  属性(成员变量实例变量):体重、年龄
     5  行为(方法):走路、吃
     6  */
     7 #import <Foundation/Foundation.h>
     8 /*
     9  1.类的声明
    10     * 成员变量
    11     * 方法的声明
    12  */
    13 @interface Person : NSObject
    14 {
    15     @public
    16     int age;
    17     double weight;
    18 }
    19 
    20 - (void)walk;
    21 - (void)eat;
    22 
    23 @end
    24 
    25 
    26 // 2.类的实现
    27 @implementation Person
    28 
    29 // 实现@interface中声明的方法
    30 - (void)walk
    31 {
    32     NSLog(@"%d岁、%f公斤的人走了一段路", age, weight);
    33 }
    34 
    35 - (void)eat
    36 {
    37     NSLog(@"%d岁、%f公斤的人在吃东西", age, weight);
    38 }
    39 
    40 @end
    41 
    42 int main()
    43 {
    44     // 在使用类创建对象之前,会将类加载进内存
    45     Person *p = [Person new];
    46     p->age = 20;
    47     p->weight = 40;
    48     
    49     [p eat];
    50     [p walk];
    51     
    52     Person *p2 = [Person new];
    53     p2->age = 30;
    54     p2->weight = 60;
    55     [p2 eat];
    56     [p2 walk];
    57     
    58     /*
    59     Person *p2 = [Person new];
    60     p2->age = 30;
    61     p2->weight = 50;
    62     
    63     p = p2;
    64     
    65     p->age = 40;
    66     
    67     [p2 walk];
    68     */
    69     
    70     /*
    71     Person *p = [Person new];
    72     p->age = 20;
    73     
    74     Person *p2 = [Person new];
    75     p2->weight = 50.0;
    76     
    77     [p walk];
    78      */
    79      
    80     /*
    81     Person *p = [Person new];
    82     p->age = 20;
    83     p->weight = 50.0;
    84     [p walk];
    85     
    86     Person *p2 = [Person new];
    87     p2->age = 30;
    88     p2->weight = 60.0;
    89     [p2 walk];
    90     */
    91     return 0;
    92 }

     3.常见错误

     1 /*
     2  方法
     3  1.对象方法都是以减号 - 
     4  2.对象方法的声明必须写在@interface和@end之间
     5    对象方法的实现必须写在@implementation和@end之间
     6  3.对象方法只能由对象来调用
     7  4.对象方法归类对象所有
     8  
     9  函数
    10  1.函数能写在文件中的任意位置(@interface和@end之间除外),函数归文件所有
    11  2.函数调用不依赖于对象
    12  3.函数内部不能直接通过成员变量名访问某个对象的成员变量
    13  
    14  */
    15 
    16 #import <Foundation/Foundation.h>
    17 
    18 @interface Person : NSObject
    19 @end
    20 
    21 @implementation Person
    22 @end
    23 
    24 @interface Car : NSObject
    25 {// 成员变量实例变量
    26     //int wheels = 4; 不允许在这里初始化
    27     //static int wheels; 不能随便将成员变量当做C语言中的变量来使用
    28     @public
    29     int wheels;
    30 }
    31 
    32 - (void)run;
    33 - (void)fly;
    34 @end
    35 
    36 int main()
    37 {
    38     // wheels = 10;
    39     /*
    40     Car *c = [Car new];
    41     c->wheels = 4;
    42     //run();
    43     
    44     [c run];
    45     */
    46     
    47     void test2();
    48     
    49     test2();
    50     
    51     return 0;
    52 }
    53 
    54 @implementation Car
    55 
    56 - (void) fly
    57 {
    58     
    59 }
    60 
    61 /*
    62 void test2()
    63 {
    64     NSLog(@"调用了test2函数-%d", wheels);
    65 }*/
    66 
    67 void test()
    68 {
    69     NSLog(@"调用了test函数");
    70 }
    71 
    72 - (void)run
    73 {
    74     test();
    75     NSLog(@"%d个轮子的车跑起来了", wheels);
    76 }
    77 @end
     
     
     
  • 相关阅读:
    Redis源代码分析(十三)--- redis-benchmark性能測试
    kvm中运行kvm
    umount.nfs device busy day virsh extend diskSpace, attachDisk
    ultravnc
    openNebula dubug
    maintenance ShellScripts
    virsh VMI deploy data serial xml
    cloud computing platform,virtual authentication encryption
    基于C 的libvirt 接口调用
    storage theory
  • 原文地址:https://www.cnblogs.com/dssf/p/4639412.html
Copyright © 2011-2022 走看看