继承多态详解

来源:互联网 发布:用电脑怎么看淘宝直播 编辑:程序博客网 时间:2024/04/28 10:48

//
// ViewController.m
// OOP
//
// Created by apple on 15/9/9.
// Copyright (c) 2015年 hell xin. All rights reserved.
//

import “ViewController.h”

@interface Animal : NSObject
- (void)eat;
@end

@implementation Animal
- (void)eat
{

}
@end

@interface Dog : Animal
- (void)eat;
@end

@implementation Dog
- (void)eat
{

}
@end

@interface Cat : Animal
- (void)eat;

@end

@implementation Cat
- (void)eat
{

}
@end

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // //继承
    // Animal *animal = [Animal new];
    // [animal eat];
    // Dog *dog = [Dog new];
    // [dog eat];
    //多态当不同类具备相同的名称的方法时,互换调用该方法的对象的类型,产生不同的行为。
    Animal *animal = nil;
    animal = [Dog new];
    [animal eat];
    animal =[Dog new];
    [animal eat];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

0 0