继承
来源:互联网 发布:知名网络言情小说作家 编辑:程序博客网 时间:2024/06/05 06:27
//
// main.m
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Zombie.h"
#import "LuzhangZombie.h"
#import "TietongZombie.h"
int main(int argc,constchar * argv[]) {
////普通僵尸自定义初始化***************************************************
Zombie *zombie=[[Zombie alloc] initWithBlood:200 attackNum:20 speed:10];
[zombie sayHi];
[zombie walk];
////设置器访问器修改////////////////////////////////////////////////////////
[zombie setSpeed:14.5];
NSLog(@"设置器访问器修改后速度:%g",[zombie speed]);
//点方法修改
zombie.attackNum=23;
NSLog(@"点方法修改后攻击力:%ld",zombie.attackNum);
//普通僵尸的便利构造器
Zombie *zombie1=[Zombie zombieWithBlood:250 attackNum:25 speed:15];
[zombie1 sayHi];
//kvc方法
[zombie1 setValue:@"255" forKey:@"blood"];
NSLog(@"kvc方法修改后血量:%@",[zombie1 valueForKey:@"blood"]);
////路障僵尸自定义初始化***************************************************
LuzhangZombie *luzombie=[[LuzhangZombie alloc] initWithBlood:300 attackNum:30 speed:20 protection:@"路障"];
[luzombie sayHi];
[luzombie walk];
////路障僵尸的设置器//普通僵尸设置器已有的内容可以实现继承.路障僵尸的设置器只需写普通僵尸没有的内容/////
[luzombie setProtection:@"路障帽子"];
NSLog(@"设置器修改后路障僵尸装备:%@",[luzombie protection]);
//路障僵尸便利构造器
LuzhangZombie *luzombie1=[LuzhangZombie luzombieWithBlood:350 attackNum:35 speed:25 protection:@"路障"];
[luzombie1 sayHi];
//kvc
[luzombie1 setValue:@"36" forKey:@"attackNum"];
NSLog(@"kvc方法修改后攻击力:%@",[luzombie1 valueForKey:@"attackNum"]);
////铁桶僵尸自定义初始化**************************
TietongZombie *tiezombie=[[TietongZombie alloc] initWithBlood:400 attackNum:40 speed:30 protection:@"铁桶" weakness:@"铁桶被吸走"];
[tiezombie sayHi];
[tiezombie attack];
/////铁桶僵尸设置器访问器修改,与父类相同的设置器会继承....///////////////////////////////////.
[tiezombie setBlood:440];
NSLog(@"设置器访问器修改铁桶僵尸血量:%ld",tiezombie.blood);
[tiezombie setProtection:@"铁桶帽子"];
NSLog(@"铁桶僵尸设置器修改后装备:%@",[tiezombie protection]);
//铁桶僵尸便利构造器
TietongZombie *tiezombie1=[TietongZombie tieZombieWithBlood:450 attackNum:45 speed:35 protection:@"铁桶" weakness:@"铁桶被吸走了"];
[tiezombie1 sayHi];
//kvc
[tiezombie1 setValue:@"铁桶碎了" forKey:@"weakness"];
NSLog(@"kvc方法修改后弱点%@",[tiezombie1 valueForKey:@"weakness"]);
////此工程没用@property创建类特征,所以不能使用点方法和[对象 set特征:@" "]的方式对类特征进行修改,可以使用kvc方法进行修改.
////要想用点方法和[对象 set特征:@" "]修改,需要设置器和访问器.
////@property创建类可以避免设置器和访问器的设置....但没能继承
////设置器和访问器也可以继承,,,子类与父类相同的内容将会继承
return0;
}
///////////////////////Zombie///////////////////////
(1)
// Zombie.h
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Zombie : NSObject
{
//普通僵尸特征
NSInteger _blood;
NSInteger _attackNum;
CGFloat _speed;
}
//普通僵尸的行为
-(void)sayHi;
-(void)walk;
-(void)attack;
-(void)dead;
//普通僵尸的设置器
-(void)setBlood:(NSInteger)blood;
-(void)setAttackNum:(NSInteger)attackNum;
-(void)setSpeed:(CGFloat)speed;
//普通僵尸的访问器
-(NSInteger)blood;
-(NSInteger)attackNum;
-(CGFloat)speed;
//普通僵尸的自定义初始化
-(id)initWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed;
//普通僵尸的便利构造器
+(id)zombieWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed;
@end
(2)//
// Zombie.m
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "Zombie.h"
@implementation Zombie
-(void)sayHi{
NSLog(@"普通僵尸血量:%ld,攻击力:%ld,速度:%g",_blood,_attackNum,_speed);
}
//普通僵尸的行为
-(void)walk{
NSLog(@"普通僵尸正在走来");
}
-(void)attack{
NSLog(@"普通僵尸正在攻击");
}
-(void)dead{
NSLog(@"普通僵尸正在死亡");
}
//普通僵尸的设置器
-(void)setBlood:(NSInteger)blood{
_blood=blood;
}
-(void)setAttackNum:(NSInteger)attackNum{
_attackNum=attackNum;
}
-(void)setSpeed:(CGFloat)speed{
_speed=speed;
}
//普通僵尸的访问器
-(NSInteger)blood{
return _blood;
}
-(NSInteger)attackNum{
return _attackNum;
}
-(CGFloat)speed{
return _speed;
}
//普通僵尸的自定义初始化
-(id)initWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed{
self=[super init];
if (self) {
_blood=blood;
_attackNum=attackNum;
_speed=speed;
}
return self;
}
//普通僵尸的便利构造器
+(id)zombieWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed{
Zombie *zombie=[[Zombie alloc ]initWithBlood:blood attackNum:attackNum speed:speed];
return zombie;
}
@end
/////////////////LuzhangZombie//////////////////////
(1)
// LuzhangZombie.h
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Zombie.h"
@interface LuzhangZombie : Zombie
{
//路障僵尸的特征,继承普通僵尸特征,重复的不用再写.
NSString *_protection;
}
//路障僵尸的行为
-(void)sayHi;
-(void)walk;
-(void)attack;
-(void)dead;
-(void)lostprotection;
//路障僵尸的设置器
-(void)setProtection:(NSString *)protection;
//路障僵尸的访问器
-(NSString *)protection;
//路障僵尸的自定义初始化
-(id)initWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection;
//路障僵尸便利构造器
+(id)luzombieWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection;
@end
(2)//
// LuzhangZombie.m
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "LuzhangZombie.h"
@implementation LuzhangZombie
-(void)sayHi{
NSLog(@"路障僵尸血量:%ld,攻击力:%ld,速度:%g,装备:%@",_blood,_attackNum,_speed,_protection);
}
//路障僵尸的行为的重写,优先执行重写子类.如果未重写,则执行父类
-(void)walk{
NSLog(@"路障僵尸正在走来");
}
-(void)attack{
NSLog(@"路障僵尸正在攻击");
}
-(void)dead{
NSLog(@"路障僵尸正在死亡");
}
-(void)lostprotection{
NSLog(@"路障僵尸失去了装备");
}
//路障僵尸的设置器
-(void)setProtection:(NSString *)protection{
_protection=protection;
}
//路障僵尸的访问器
-(NSString *)protection{
return _protection;
}
//路障僵尸自定义初始化
-(id)initWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection{
////继承父类初始化
self=[super initWithBlood:blood attackNum:attackNum speed:speed];
if (self) {
_protection=protection;
}
return self;
}
//路障僵尸便利构造器
+(id)luzombieWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection{
LuzhangZombie *luzombie=[[LuzhangZombie alloc] initWithBlood:blood attackNum:attackNum speed:speed protection:protection];
return luzombie;
}
@end
////////////////TietongZombie//////////////////////
(1)
// TietongZombie.h
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "LuzhangZombie.h"
@interface TietongZombie : LuzhangZombie
{
//铁桶僵尸的特征,继承普通僵尸和路障僵尸的特征,此处只写铁桶僵尸的特征
NSString *_weakness;
}
//铁桶僵尸的行为
-(void)sayHi;
-(void)walk;
-(void)attack;
-(void)dead;
-(void)lostprotection;
//铁桶僵尸的设置器
-(void)setWeakNess:(NSString *)weakness;
//铁桶僵尸的访问器
-(NSString *)weakness;
//铁桶僵尸的自定义初始化
-(id)initWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection
weakness:(NSString *)weakness;
//铁桶僵尸的便利构造器
+(id)tieZombieWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection
weakness:(NSString *)weakness;
@end
(2)//
// TietongZombie.m
// 继承练习
//
// Created by dllo on 15/7/18.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "TietongZombie.h"
@implementation TietongZombie
-(void)sayHi{
NSLog(@"铁桶僵尸血量:%ld,攻击力:%ld,速度:%g,装备:%@,弱点:%@",_blood,_attackNum,_speed,_protection,_weakness);
}
//铁桶僵尸的行为
-(void)walk{
NSLog(@"铁桶僵尸正在走来");
}
-(void)attack{
NSLog(@"铁桶僵尸正在攻击");
}
-(void)dead{
NSLog(@"铁桶僵尸正在死亡");
}
-(void)lostprotection{
NSLog(@"铁桶僵尸失去了装备");
}
//铁桶僵尸的设置器//路障与普通僵尸已有的内容可以继承
-(void)setWeakNess:(NSString *)weakness{
_weakness=weakness;
}
//铁桶僵尸的访问器
-(NSString *)weakness{
return _weakness;
}
//铁桶僵尸的自定义初始化
-(id)initWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection
weakness:(NSString *)weakness{
////继承父类
self=[super initWithBlood:blood attackNum:attackNum speed:speed protection:protection];
if (self) {
_weakness=weakness;
}
return self;
}
//铁桶僵尸的便利构造器
+(id)tieZombieWithBlood:(NSInteger)blood
attackNum:(NSInteger)attackNum
speed:(CGFloat)speed
protection:(NSString *)protection
weakness:(NSString *)weakness{
TietongZombie *tiezombie=[[TietongZombie alloc] initWithBlood:blood attackNum:attackNum speed:speed protection:protection weakness:weakness];
return tiezombie;
}
@end
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 视频压缩中IPB帧概念
- java.util.ConcurrentModificationException 卧槽
- hdu1576 mod 运算的逆元
- [转载]实用而有效的美剧英文学习法
- android 环境搭建中adk 位置找不到问题解决方案
- 继承
- 百度地图定位失效,armeabi和armeabi-v7a
- 【基础】java 条件语句 switch
- Makefile经典教程(掌握这些足够)
- Android学习资源(持续更新中)
- Servlet中Web.xml的配置详解
- 贝叶斯分类
- javaSendEmail 的使用
- 孟得明:告诉你月薪3000与月薪30000的文案区别!