OC语法基础(4)

本文介绍了OC语法的基础,重点探讨了面向对象特性中的继承和super的使用,以及多态的概念和价值。通过继承可以减少代码量并为多态提供基础,而多态则通过继承实现,允许一个对象有多种形态,增强了代码的灵活性和解耦性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

面向对象特性

#import

 

int main(int argc, const char * argv[])

{

 

   

    return 0;

}

self和类方法

//  Iphone.h

#import

 

//闪光灯状态

typedef enum

{

    kFlashlightStatusOpen,

    kFlashlightStatusClose

}FlashlightStatus;

 

@interface Iphone : NSObject

{

    int _cpu;

}

 

- (void)setCpu:(int)cpu;

- (int)cpu;

 

//拍照方法

+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

 

//打开闪光灯

+ (void)openFlashlight;

//关闭闪光灯

+ (void)closeFlashlight;

 

@end

//  Iphone.m

#import "Iphone.h"

 

@implementation Iphone

 

+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

    if (kFlashlightStatusOpen == status) {

//        打开闪光灯

//        [Iphone openFlashlight];

//        self 谁调用这个方法slef就代表谁

//        self == Iphone

        [self openFlashlight];

       

    }else

    {

//        关闭闪光灯

//        [Iphone closeFlashlight];

//        在类方法中利用self可以调用其它类方法

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

 

+ (void)openFlashlight

{

    NSLog(@"打开闪光灯");

}

 

+ (void)closeFlashlight

{

    NSLog(@"关闭闪光灯");

}

 

- (void)setCpu:(int)cpu

{

    _cpu = cpu;

   

}

 

- (int)cpu

{

    return _cpu;

}

 

@end

//  main.m

#import

#import "Iphone.h"

 

 

int main(int argc, const char * argv[])

{

 

    [Iphone cameraWithFlashlightStatus:kFlashlightStatusOpen];

   

    return 0;

}

self和对象方法成员变量

//  Iphone.h

#import

 

//闪光灯状态

typedef enum

{

    kFlashlightStatusOpen,

    kFlashlightStatusClose

}FlashlightStatus;

 

@interface Iphone : NSObject

{

    int _cpu;

}

 

- (void)setCpu:(int)cpu;

- (int)cpu;

 

//拍照方法

//+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

 

//打开闪光灯

- (void)openFlashlight;

//关闭闪光灯

- (void)closeFlashlight;

 

打开闪光灯

//+ (void)openFlashlight;

关闭闪光灯

//+ (void)closeFlashlight;

 

@end

//  Iphone.m

#import "Iphone.h"

 

@implementation Iphone

 

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

   

    NSLog(@"self = %p", self);

    

    if (kFlashlightStatusOpen == status) {

        //        打开闪光灯

        //        self 谁调用这个方法slef就代表谁

        //        self == ip

        [self openFlashlight];

       

    }else

    {

        //        关闭闪光灯

        //        在对象方法中self可以调用其它对象方法

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

//  main.m

 

#import

#import "Iphone.h"

 

int main(int argc, const char * argv[])

{

//    [Iphone cameraWithFlashlightStatus:kFlashlightStatusOpen];

   

    Iphone *ip = [Iphone new];

    [ip setCpu:2];

   

//    ip->_cpu = 20;

    NSLog(@"超频前:cpu = %d", [ip cpu]);

    NSLog(@"ip = %p", ip);

    [ip cameraWithFlashlightStatus:kFlashlightStatusOpen];

     NSLog(@"超频后:cpu = %d", [ip cpu]);

   

   

    return 0;

}

 

self注意

//  main.m

#import

#import "Iphone2.h"

 

int main(int argc, const char * argv[])

{

//  同时有对象方法和类方法存在的时候,self不会调错

//   

//    Iphone2 *ip = [Iphone2 new];

//    [ip cameraWithFlashlightStatus:kFlashlightStatusClose];

   

//    [Iphone2 cameraWithFlashlightStatus:kFlashlightStatusClose];

   

   

//    Iphone2 *ip = [Iphone2 new];

//    [ip closeFlashlight];

   

    [Iphone2 openFlashlight];

    return 0;

}

//  Iphone.h

 

#import

 

//闪光灯状态

typedef enum

{

    kFlashlightStatusOpen,

    kFlashlightStatusClose

}FlashlightStatus;

 

@interface Iphone : NSObject

{

    int _cpu;

}

 

- (void)setCpu:(int)cpu;

- (int)cpu;

 

//拍照方法

+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

//打开闪光灯

+ (void)openFlashlight;

//关闭闪光灯

+ (void)closeFlashlight;

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

//打开闪光灯

- (void)openFlashlight;

//关闭闪光灯

- (void)closeFlashlight;

 

 

@end

//  Iphone.m

 

#import "Iphone.h"

 

@implementation Iphone

+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

    if (kFlashlightStatusOpen == status)

    {

        //        打开闪光灯

        [self openFlashlight];

       

    }else

    {

       

        //        关闭闪光灯

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

 

+ (void)openFlashlight

{

   

    NSLog(@"类方法 - 打开闪光灯");

}

 

+ (void)closeFlashlight

{

    NSLog(@"类方法 - 关闭闪光灯");

}

 

#pragma mark - 对象方法

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

   

    NSLog(@"self = %p", self);

   

    if (kFlashlightStatusOpen == status) {

        //        打开闪光灯

        [self openFlashlight];

       

    }else

    {

        //        关闭闪光灯

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

 

 

- (void)openFlashlight

{

 

    NSLog(@"对象方法 - 打开闪光灯");

}

 

- (void)closeFlashlight

{

    NSLog(@"对象方法 - 关闭闪光灯");

    [self closeFlashlight];

}

 

- (void)setCpu:(int)cpu

{

    _cpu = cpu;

   

}

 

- (int)cpu

{

    return _cpu;

}

@end

 

//  Iphone2.h

//  01-面向对象特性

//

//  Created by apple on 14-3-8.

//  Copyright (c) 2014 itcast. All rights reserved.

//

 

#import

 

//闪光灯状态

typedef enum

{

    kFlashlightStatusOpen,

    kFlashlightStatusClose

}FlashlightStatus;

 

@interface Iphone2 : NSObject

{

    int _cpu;

}

 

- (void)setCpu:(int)cpu;

- (int)cpu;

 

//拍照方法

+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

//打开闪光灯

+ (void)openFlashlight;

//关闭闪光灯

+ (void)closeFlashlight;

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

//打开闪光灯

- (void)openFlashlight;

//关闭闪光灯

- (void)closeFlashlight;

 

@end

 

//  Iphone2.m

 

#import "Iphone2.h"

 

@implementation Iphone2

+ (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

    if (kFlashlightStatusOpen == status)

    {

        //        打开闪光灯

        [self openFlashlight];

       

    }else

    {

       

        //        关闭闪光灯

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

 

+ (void)openFlashlight

{

   

    NSLog(@"类方法 - 打开闪光灯");

//    在类方法中不能利用self调用和自己同名的类方法,会造成死循环

    [self openFlashlight];

}

 

+ (void)closeFlashlight

{

    NSLog(@"类方法 - 关闭闪光灯");

}

 

#pragma mark - 对象方法

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

   

    NSLog(@"self = %p", self);

   

    if (kFlashlightStatusOpen == status) {

        //        打开闪光灯

        [self openFlashlight];

       

    }else

    {

        //        关闭闪光灯

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

 

 

- (void)openFlashlight

{

   

    NSLog(@"对象方法 - 打开闪光灯");

}

 

- (void)closeFlashlight

{

    NSLog(@"对象方法 - 关闭闪光灯");

//    在对象方法中不能利用self调用和自己同名的对象方法,会造成死循环

//    [self closeFlashlight];

}

 

- (void)setCpu:(int)cpu

{

    _cpu = cpu;

   

}

 

- (int)cpu

{

    return _cpu;

}

@end

 

继承和super(重要)

关于继承的一些认识:

继承本质:继承是从单一方面描述一个事物,他想表达的是:是一个(is a)的问题;而如果想从多个方面描述一个事物用多态来描述(这是建立在继承上的)

继承价值1>继承的出现减少了代码量

2>为多态的特性垫底基础

继承模型:每一个类都可以看成是由父类和扩展内容组成的,及图中的上部分和下部分组成,多层继承是一个递归过程

 



OC语法基础(4)
 

 

 

 

 

 


如果重写了某个父类方法后想调用原父类被重写的方法用super(这个单词的意思不翻译为它是上一个,他是一个词根,可在《现代英语词源字典》中查到)

//  Iphone.h

#import

#import "Phone.h"

 

@interface Iphone : Phone

 

@end

//  Iphone.m

#import "Iphone.h"

 

@implementation Iphone

 

// 如果在子类中实现了和父类中同名同类型的方法,我们称之为重写

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

    NSLog(@"Iphone 拍照");

}

 

+ (void)test

{

    NSLog(@"Iphone test");

}

@end

//  Phone.h

#import

//闪光灯状态

typedef enum

{

    kFlashlightStatusOpen,

    kFlashlightStatusClose

}FlashlightStatus;

 

@interface Phone : NSObject

{

    @public

    int _cpu;

}

//拍照

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status;

//打开闪光灯

- (void)openFlashlight;

//关闭闪光灯

- (void)closeFlashlight;

 

+ (void)test;

- (void)test;

@end

//  Phone.m

#import "Phone.h"

 

@implementation Phone

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

   

    if (kFlashlightStatusOpen == status) {

        //        打开闪光灯

        [self openFlashlight];

       

    }else

    {

        //        关闭闪光灯

        [self closeFlashlight];

    }

   

    NSLog(@"拍照");

}

 

 

- (void)openFlashlight

{

   

    NSLog(@"Phone  - 打开闪光灯");

}

 

- (void)closeFlashlight

{

    NSLog(@"Phone - 关闭闪光灯");

}

 

- (void)setCpu:(int)cpu

{

    _cpu = cpu;

   

}

 

- (int)cpu

{

    return _cpu;

}

 

 

+ (void)test

{

    NSLog(@"+ Phone test");

}

 

- (void)test

{

    NSLog(@"- Phone test");

}

@end

//  Anycall.h

#import

#import "Phone.h"

 

@interface Anycall : Phone

{

//    OC当中子类继承了父类,就不能定义和父类同名的成员变量

//    int _cpu;

}

 

+ (void)test;

@end

//  Anycall.m

#import "Anycall.h"

 

@implementation Anycall

 

//+ (void)test

//{

//    NSLog(@"Anycall test");

//}

 

- (void)cameraWithFlashlightStatus:(FlashlightStatus)status

{

    NSLog(@"聚焦");

//    if (kFlashlightStatusOpen == status) {

//        //        打开闪光灯

//        [self openFlashlight];

//       

//    }else

//    {

//        //        关闭闪光灯

//        [self closeFlashlight];

//    }

//   

//    NSLog(@"拍照");

   

//    死循环

//    [self cameraWithFlashlightStatus:status];

   

//     super是明确的告诉程序要执行父类中的方法

//    super使用场合:子类重写父类方法的时候想保留父类的一些行为的时候

//    使用super在对象方法中调用父类的方法会调用父类的对象方法

    [super cameraWithFlashlightStatus:status];

 

}

 

+ (void)test

{

    NSLog(@"子类 test");

//    使用super在类方法中调用父类方法,会调用父类的类方法

    [super test];

}

 

- (void)test

{

    NSLog(@"子类 test");

    [super test];

}

@end

//  main.m

#import

#import "Iphone.h"

#import "Anycall.h"

 

int main(int argc, const char * argv[])

{

   

   

//    new方法是父类中继承过来的, Phone之所以能创建对象是因为继承了NSObject

//    [Phone new];

   

   

   

   

//    Anycall *al = [Anycall new];

//    al->_cpu = 50;

//    NSLog(@"_cpu = %d", al->_cpu);

   

//    [Anycall test];

   

   

   

//    Anycall *al = [Anycall new];

//    [al cameraWithFlashlightStatus:kFlashlightStatusOpen];

 

//    [Anycall test];

    Anycall *al = [Anycall new];

    [al test];

    return 0;

}

description

//  Iphone.h

#import

 

@interface Iphone : NSObject

{

    int _cpu;

    int _size;

    int _weight;

    int _color;

}

 

- (void)setCpu:(int)cpu;

- (int)cpu;

 

- (void)setSize:(int)size;

- (int)size;

 

- (void)setWeight:(int)weight;

- (int)weight;

 

- (void)setColor:(int)color;

- (int)color;

@end

//  Iphone.m

#import "Iphone.h"

 

@implementation Iphone

 

- (void)setCpu:(int)cpu

{

    _cpu = cpu;

   

}

- (int)cpu

{

    return _cpu;

}

 

- (void)setSize:(int)size

{

    _size = size;

}

- (int)size

{

    return _size;

}

 

- (void)setWeight:(int)weight

{

    _weight = weight;

}

- (int)weight

{

    return _weight;

}

 

- (void)setColor:(int)color

{

    _color = color;

}

- (int)color

{

    return _color;

}

 

- (NSString *)description

{

    NSString *str = [NSString stringWithFormat:@"size  =%d, weight = %d, color = %d, cpu = %d", _size, _weight, _color, _cpu];

    return str;

}

 

+ (NSString *)description

{

    return @"加号开头的description方法";

}

@end

//  main.m

#import

#import "Iphone.h"

 

int main(int argc, const char * argv[])

{

 

    Iphone *ip = [Iphone new];

    [ip setCpu:20];

    [ip setColor:1];

    [ip setWeight:20];

    [ip setSize:5];

//    NSLog(@"cpu = %d, color = %d, weight= %d, size  =%d", [ip cpu], [ip color], [ip weight], [ip size]);

//     NSLog(@"cpu = %d, color = %d, weight= %d, size  =%d", [ip cpu], [ip color], [ip weight], [ip size]);

//     NSLog(@"cpu = %d, color = %d, weight= %d, size  =%d", [ip cpu], [ip color], [ip weight], [ip size]);

   

//    当使用%@打印一个对象的时候会去调用对象的description方法

    NSLog(@"ip = %@", ip);

    NSLog(@"%@", ip);

   

    Class cs = [Iphone class];

    NSLog(@"%@", cs);

   

    return 0;

}

多态

关于多态的一些认识:

多态本质:继承是从单一方面描述一个事物,他想表达的是:是一个(is a)的问题;而如果想从多个方面描述一个事物用多态来描述(这是建立在继承上的),多态是多种形态它通过继承实现,它想描述一个事物“既是A也是B”

多态价值:赋值兼容(解耦)

多态条件:

1>继承,并重新父类方法

2>父类引用指向子类对象

3>运行时绑定

//  Animal.h

#import

 

//动物类

@interface Animal : NSObject

{

    int  _age;// 年龄

}

 

- (void)eat;//

@end

//  Animal.m

#import "Animal.h"

 

@implementation Animal

 

- (void)eat

{

    NSLog(@"动物吃东西");

}

@end

 

 

//  Cat.h

#import "Animal.h"

//猫类

@interface Cat : Animal

 

@end

//  Cat.m

#import "Cat.h"

 

@implementation Cat

- (void)eat

{

    NSLog(@"吃鱼");

}

@end

//  Dog.h

#import

#import "Animal.h"

 

//狗类

@interface Dog : Animal

 

- (void)jiao;

@end

//  Dog.m

#import "Dog.h"

 

@implementation Dog

- (void)eat

{

    NSLog(@"狗啃你的脚");

}

 

//狗的特有方法

- (void)jiao

{

    NSLog(@"嗷嗷叫");

}

@end

//  Person.h

#import

#import "Dog.h"

#import "Cat.h"

 

@interface Person : NSObject

//饲养员 ,专门喂动物

- (void)feedDog:(Dog *)dog;

 

- (void)feedCat:(Cat *)cat;

 

// 1000种动物

- (void)feedAnimal:(Animal *)animal;

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (void)feedDog:(Dog *)dog

{

    [dog eat];

}

 

- (void)feedCat:(Cat *)cat

{

    [cat eat];

}

 

- (void)feedAnimal:(Animal *)animal// Animal *animal = [Dog new];

{

//    运行时候动态绑定(动态的判断animal的真实类型)

    [animal eat];

}

@end

//  Pig.h

#import "Animal.h"

 

@interface Pig : Animal

 

@end

//  Pig.m

#import "Pig.h"

 

@implementation Pig

 

- (void)eat

{

    NSLog(@"大口大口吃");

}

@end

 

//  main.m

#import

#import "Dog.h"

#import "Cat.h"

#import "Animal.h"

#import "Person.h"

#import "Pig.h"

 

int main(int argc, const char * argv[])

{

   

   

   

   

    

   

//    Dog *d = [Dog new];

//    [d jiao];

   

   

   

    double d = 10.1;

    int i = (double)d;

   

注意:"

     不要把父类类型强制转换为子类类型<</span>这里是在没有父类指针指向子类对象的前提下>

//    Animal *a2 = [Animal new];

//    Dog *d2 = (Dog *)a2;// 写给编译器看的

   运行方法时会动态检查对象的真实类型, (动态绑定)

//    [d2 jiao];

   

 

   

   

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值