ios开发动物园管理 继承多态的实现
//
// main.m
// 继承
// // #import <Foundation/Foundation.h>
#import "Animal.h"
#import "Cat.h"
#import "Dog.h"
#import "FeedMan.h" int main(int argc, const char * argv[])
{ // Animal * animal = [Animal new];
//
// [animal eat];
//
// [animal sleep]; // //忘记引入头文件
// Animal * cat =[[Cat alloc]init];
//
// [cat eat]; // Cat * cat = [[Cat alloc]init];
//
// [cat catchMouse];
//
// Dog * dog = [[Dog alloc]init];
//
// [dog bark];
// //父类指针保存子类对象,如何调用子类对象的方法? // Animal * animal_cat = [[Cat alloc]init];
//
// FeedMan * man = [[FeedMan alloc]init];
//
// [man showName:animal_cat]; //[animal_cat eat]; // [animal_cat setName:@"Hello Cat"]; Animal * animal_dog = [[Dog alloc]init]; FeedMan * man = [[FeedMan alloc]init]; [man showName:animal_dog]; [man FeedAnimal:animal_dog]; //子类调用父类的方法。如何实现方法的不同性? return 0;
}
//
// FeedMan.h
// 继承
// #import "Animal.h" @interface FeedMan : NSObject -(void)showName:(Animal *)animal; -(void)FeedAnimal:(Animal *)animal;
@end
//
// FeedMan.m
// 继承 #import "FeedMan.h"
#import "Dog.h"
#import "Cat.h" @implementation FeedMan -(void)FeedAnimal:(Animal *)animal
{
if ([animal isKindOfClass:[Dog class]] ) { Dog * dog = (Dog *)animal;
[dog eat];
}
} -(void)showName:(Animal *)animal
{
//能够动态的检測动物的类型用到的一个类?
if([animal isKindOfClass:[Dog class]])
{
//须要强制类型转换
Dog * dog = (Dog *)animal;
[dog bark];
}
else if ([animal isKindOfClass:[Cat class]])
{
Cat * cat = (Cat *)animal;
[cat catchMouse];
} } @end
//
// Animal.h
// 继承 #import <Foundation/Foundation.h> @interface Animal : NSObject
{
NSString * _name; int _age;
} @property NSString * name;
@property int age; -(void)eat; -(void)sleep; -(void)showAge; @end
//
// Animal.m
// 继承
// #import "Animal.h" @implementation Animal -(void)eat
{
NSLog(@"动物吃东西");
} -(void)sleep
{
NSLog(@"动物睡觉了");
} -(void)showAge
{
NSLog(@"小动物的年龄");
}
@end
//
// Dog.h
// 继承
// #import "Animal.h" @interface Dog : Animal
{ } -(void)bark;
-(void)eat; @end
// Dog.m
// 继承
// #import "Dog.h" @implementation Dog -(void)bark
{
NSLog(@"小狗汪汪叫");
}
-(void)eat
{
NSLog(@"小狗吃东西");
} @end
//
// Cat.h
// 继承 #import "Animal.h" @interface Cat : Animal
{ }
-(void)catchMouse; -(void)eat;
@end
// Cat.m
// 继承
// #import "Cat.h" @implementation Cat
{ } -(void)catchMouse
{
NSLog(@"猫咪会捉老鼠! ");
}
-(void)eat
{
NSLog(@"小猫吃小鱼");
}
@end
ios开发动物园管理 继承多态的实现的更多相关文章
- IOS开发依赖管理工具CocoaPods
CocoaPods IOS开发依赖管理工具 CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It ...
- iOS开发——源代码管理——git(分布式版本控制和集中式版本控制对比,git和SVN对比,git常用指令,搭建GitHub远程仓库,搭建oschina远程仓库 )
一.git简介 什么是git? git是一款开源的分布式版本控制工具 在世界上所有的分布式版本控制工具中,git是最快.最简单.最流行的 git的起源 作者是Linux之父:Linus Bened ...
- iOS开发——源代码管理——SVN
一.源代码管理(svn)简介 01. 源代码管理工具概述 ======================================================================= ...
- iOS开发-内存管理
内存管理 对于这篇呢,其实现在都是ARC模式,正常状态下基本不用我们去手动释放内存,所以如果不是要面试呀.装逼或者扎实功底的,就先别看了或者了解下即可,因为像面试时,有些面试官想看你的基础时,就有些人 ...
- iOS开发-应用管理
// // ViewController.m // 21-应用管理-1 // // Created by hongqiangli on 2017/8/2. // Copyright © 201 ...
- iOS开发Objective-C基础之──多态
Objective-C语言是面向对象的高级编程语言,因此,它具有面向对象编程所具有的一些特性,即:封装性.继承性和多态性. 今天介绍一下Objective-C中的多态性. 一.什么是多态 多态:不同对 ...
- iOS开发——源代码管理——svn 命令行下常用的几个命令
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domai ...
- iOS开发ARC内存管理技术要点
本文来源于我个人的ARC学习笔记,旨在通过简明扼要的方式总结出iOS开发中ARC(Automatic Reference Counting,自动引用计数)内存管理技术的要点,所以不会涉及全部细节.这篇 ...
- iOS开发-Alcatraz插件管理
CocoaPod负责iOS开发中的引用类库的管理,Alcatraz中文翻译阿尔卡特拉斯岛,也有人称之为恶魔岛,主要是负责管理第三方Xcode 插件.模版以及颜色配置的工具,直接集成到 Xcode 的图 ...
随机推荐
- NOJ——聊天止于呵呵(string流重定向+map,水题)
[1645] 聊天止于呵呵 时间限制: 5000 ms 内存限制: 65535 K 问题描述 (现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵” ...
- 一个简单的django user.is_authenticated问题
Q1:这是我一个view函数: def user_info(request): response=HttpResponse() user=request.user user_id=user.id if ...
- 40深入理解C指针之---指针与单链表
一.指针与单链表 1.定义:通过使用指针将节点(结点)链接起来成为链表 2.节点(结点): 1).数据域:主要用来存储数据,可以基本数据类型,也可以是构造数据类型: 2).指针域:主要用来当前节点(结 ...
- Delphi GDI对象之绘制位图
http://www.cnblogs.com/pchmonster/archive/2012/07/06/2579334.html 绘制位图(Drawing Bitmaps) 绘制位图听起来似乎很难, ...
- [Python Cookbook] IPython: An Interactive Computing Environment
You can launch IPython on the command line just like launching the regular Python interpreter except ...
- pycharm上传代码到码云(详细)
如要转载 麻烦请您备注好原文出处!!!!(谢谢合作!) >>首先要去码云注册个账号 提示(尽量使用英文名)创建用户名 使用邮箱登录 >>然后创建库 >填写项目的基础信息 ...
- bzoj 1552: [Cerc2007]robotic sort
1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1198 Solved: 457[Submit] ...
- jar word 模板操作比较好用的工具
个人觉得比较好用的java word 模板 http://deepoove.com/poi-tl/
- 结构体和类中属性定义需要static地方
private function Readxxx:Integer;static; public class property XXX:Integer read ReadXXx; Txxx =recor ...
- Objective-C 协议(protocol)二
我们前面提到了OOP的继承,我们说Objective-C不像C++可以有多重继承,Objective-C是单一继承的,如果想要做到一个类别同时拥有多种型别的能力,我们就可以通过协议来实现.Object ...