zoukankan      html  css  js  c++  java
  • Objective-C的第二课

    今天学习了继承, 属性, 多态

    继承:继承就类似与父子之间的关系, 孩子继承了父亲的基因, 有父亲的特征, 但是又有自己的特点, 这种方法就叫做继承, 在OC上就是子类继承与父类的特征, 父类的方法, 在子类可以运用, 但是子类的特点方法在父类不能使用.

    多态:多态其实就是方法名一样, 但是实现的方法不同, 下一个子类的方法会覆盖替换上一个的方法.

    多态的作业:动物父类, 猫科子类, 狮子孙子类;

    #import <Foundation/Foundation.h>
    
    @interface Animal : NSObject
    
    - (void)cry;
    
    @end    //动物子类
    
    
    
    #import "Animal.h"
    
    @implementation Animal
    
    - (void)cry
    {
        NSLog(@"%@",@"Animal Tears");
    }
    
    @end    //动物实现的方法
    
    
    
    #import "Animal.h"
    
    @interface Cats : Animal
    
    - (void)cry;
    
    @end    //猫科子类
    
    
    
    #import "Cats.h"
    
    @implementation Cats
    
    - (void)cry
    {
        NSLog(@"%@",@"Cats Mews");
    }
    
    @end    //猫科实现方法
    
    
    
    
    #import "Cats.h"
    
    @interface Lion : Cats
    
    - (void)cry;
    
    @end    //狮子的子类
    
    
    
    
    #import "Lion.h"
    
    @implementation Lion
    
    - (void)cry
    {
        NSLog(@"%@", @"Lion The lion roar");
    }
    
    @end    //狮子的方法实现
    
    
    
    #import "ViewController.h"
    #import "Lion.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        Animal *animal;
        animal = [[Animal alloc] init];
        [animal cry];    //动物的哭
        
        Cats *cats;
        cats = [[Cats alloc] init];
        [cats cry];    //猫科的哭
        
        Lion *lion;
        lion = [[Lion alloc] init];
        [lion cry];    //狮子的哭
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    博客迁移.
    win10不能睡眠问题
    我多希望没有遇见你
    打印队列 UVA12100
    理科工具——数值分析计算相关软件及下载地址
    用原生js完成鼠标点击显示滑入滑出效果
    javascript:用原生js模拟贪吃蛇游戏练习
    阅读css官方参考手册的关键点
    翻滚吧骰子!——flex布局加css3动画综合练习
    flexible box布局微博客户端发现页面练习
  • 原文地址:https://www.cnblogs.com/iOSCain/p/4007455.html
Copyright © 2011-2022 走看看