您的位置:首页 > 其它

OCday3 初始化方法, 便利构造器, 继承

2015-05-08 19:59 246 查看
//************************************

// Person.h 文件

#import <Foundation/Foundation.h>

@interface Person : NSObject

// name, sex, age
{
NSString *_name;
NSString *_sex;
NSInteger _age;
}

// setter
- (void)setName:(NSString *)name;

- (void)setSex:(NSString *)sex;

- (void)setAge:(NSInteger)age;

// getter
- (NSString *)name;

- (NSString *)sex;

- (NSInteger)age;

// 自定义初始化方法完整版
- (id)initWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age;

// 便利构造器
+ (id)PersonWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age;

// 初始化两个成员变量的便利构造器
+ (id)PersonWithName:(NSString *)name
sex:(NSString *)sex;

// 对两个成员变量节进行赋值
- (id)initWithName:(NSString *)name
sex:(NSString *)sex;

@end

//***************************************

// Person.m 文件

#import "Person.h"

@implementation Person

// setter
- (void)setName:(NSString *)name {
_name = name;
}

- (void)setSex:(NSString *)sex {
_sex = sex;
}

- (void)setAge:(NSInteger)age {
_age = age;
}

// getter
- (NSString *)name {
return _name;
}

- (NSString *)sex {
return _sex;
}

- (NSInteger)age {
return _age;
}

// 自定义初始化方法完整版
- (id)initWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age {

//初始化父类继承过来的成员变量
self = [super init];
if (self) {
_name = name;
_age = age;
_sex = sex;
}

return
self;
}

// 对两个成员变量节进行赋值
- (id)initWithName:(NSString *)name
sex:(NSString *)sex {
self = [super init];
if (self) {
_name = name;
_sex = sex;
}

return
self;
}

// 便利构造器
+ (id)PersonWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age {
Person *per = [[Person alloc] initWithName:name sex:sex age:age];
return per;
}

// 初始化两个成员变量的便利构造器
+ (id)PersonWithName:(NSString *)name
sex:(NSString *)sex {
Person *per = [Person alloc];
[per setName:name];
[per setSex:sex];
return per;
}

@end

//*******************************************

// Student.h 文件

#import "Person.h"

@interface Student : Person

// school, number
{
NSString *_school;
NSInteger _number;
}

// 初始化成员变量完整版
- (id)initWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age
school:(NSString *)school
number:(NSInteger)number;

// setter
- (void)setSchool:(NSString *)school;

- (void)setNumber:(NSInteger)number;

// getter
- (NSString *)school;

- (NSInteger)number;

@end

//**********************************************

// Student.m 文件

#import "Student.h"

@implementation Student

// setter
- (void)setSchool:(NSString *)school {
_school = school;
}

- (void)setNumber:(NSInteger)number {
_number = number;
}

// getter
- (NSString *)school {

return _school;
}

- (NSInteger)number {

return _number;
}

// 初始化成员变量完整版
- (id)initWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age
school:(NSString *)school
number:(NSInteger)number {
self = [super initWithName:name sex:sex age:age];

//如果父类成员没有初始化成功,子类没必要初始化自己的特有成员变量
if (self) {
_school = school;
_number = number;
}

return
self;
}

@end

//*****************************************

// Zombie.h 文件

#import <Foundation/Foundation.h>

@interface Zombie : NSObject
{
NSInteger _blood; //血量
NSInteger _attack; //攻击力
CGFloat _speed; //移动速度
}

// 给僵尸类写一个自定义初始化方法完整版
- (id)initWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed;

// 僵尸的行为

//攻击
- (void)attack;

// 行走
- (void)walk;

// 死亡
- (void)dead;

// 便利构造器
+ (id)ZombieWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed;

@end

//****************************************

// Zombie.m 文件

#import "Zombie.h"

@implementation Zombie

// 僵尸的行为

//攻击
- (void)attack {
NSLog(@"%p", self);

NSLog(@"普通僵尸会攻击");
}

// 行走
- (void)walk {

NSLog(@"普通僵尸会行走");
}

// 死亡
- (void)dead {

NSLog(@"普通僵尸会死亡");
}

// 给僵尸类写一个自定义初始化方法完整版
- (id)initWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed {
self = [super init];
if (self) {
_blood = blood;
_attack = attack;
_speed = speed;
}

return
self;
}

// 便利构造器
+ (id)ZombieWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed {
Zombie *zom = [[Zombie alloc] initWithBlood:blood attack:attack speed:speed];
return zom;
}

@end

//***********************************************

// LuZhangZombie.h 文件

#import <Foundation/Foundation.h>

#import "Zombie.h"
@interface LuZhangZombie : Zombie
{

// NSInteger _blood; // 血量

// NSInteger _attack; // 攻击力

// CGFloat _speed; // 移动速度
NSString *_equipment; //装备
}

// 僵尸的行为

//攻击

//- (void)attack;

// 行走

//方法重写的前提条件:子类和父类的方法名要完全一样

//- (void)walk;

// 死亡

//- (void)dead;

// 失去装备
- (void)LoseEquip;

// 对路障僵尸的所有成员变量进行初始化方法完整版
- (id)initWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment;

// 便利构造器
+ (id)LuZhangZombieWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment;

@end

//***********************************************

// LuZhangZombie.h 文件

#import "LuZhangZombie.h"

@implementation LuZhangZombie

// 僵尸的行为

//攻击
- (void)attack {

NSLog(@"路障僵尸会攻击%ld", _blood);
}

// 行走
- (void)walk {

NSLog(@"路障僵尸会行走");
}

// 死亡
- (void)dead {

NSLog(@"路障僵尸会死亡");
}

// 失去装备
- (void)LoseEquip {

NSLog(@"路障僵尸失去装备");
}

// 对路障僵尸的所有成员变量进行初始化方法完整版
- (id)initWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment {
self = [super initWithBlood:blood attack:attack speed:speed];
if (self) {
_equipment = equipment;
}

return
self;
}

// 便利构造器
+ (id)LuZhangZombieWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment {
LuZhangZombie *luz =[[LuZhangZombie alloc] initWithBlood:blood attack:attack speed:speed equipment:equipment];
return luz;
}

@end

//******************************************

// TieTongZombie.h 文件

#import "LuZhangZombie.h"

@interface TieTongZombie : LuZhangZombie
{
NSString *_weakness;
}

// 对铁桶僵尸所有成员变量进行初始化设置完整版
- (id)initWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment
weakness:(NSString *)weakness;

// 便利构造器
+ (id)TieTongZombieWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment
weakness:(NSString *)weakness;

@end

//***********************************************

// TieTongZombie.m 文件

#import "TieTongZombie.h"

@implementation TieTongZombie

// 僵尸的行为

//攻击
- (void)attack {

NSLog(@"铁桶僵尸会攻击%ld", _blood);
}

// 行走
- (void)walk {

[super walk];// super属于本类

NSLog(@"铁桶僵尸会行走");
}

// 死亡
- (void)dead {

NSLog(@"铁桶僵尸会死亡");
}

// 失去装备
- (void)LoseEquip {

NSLog(@"铁桶僵尸失去装备");
}

// 对铁桶僵尸所有成员变量进行初始化设置完整版
- (id)initWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment
weakness:(NSString *)weakness {
self = [super initWithBlood:blood attack:attack speed:speed equipment:equipment];
if (self) {
_weakness = weakness;
}

return
self;
}

// 便利构造器
+ (id)TieTongZombieWithBlood:(NSInteger)blood
attack:(NSInteger)attack
speed:(CGFloat)speed
equipment:(NSString *)equipment
weakness:(NSString *)weakness {
TieTongZombie *tie = [[TieTongZombie alloc] initWithBlood:blood attack:attack speed:speed equipment:equipment weakness:weakness];
return tie;
}

@end

//********************************************

// main.m 文件

#import <Foundation/Foundation.h>

#import "Zombie.h"

#import "LuZhangZombie.h"

#import "TieTongZombie.h"

#import "Person.h"

#import "Student.h"
int main(int argc,constchar * argv[])
{

//

// Zombie *zom = [[Zombie alloc] init];

// LuZhangZombie *luZom = [[LuZhangZombie alloc] init];

//继承的目的就是为了节省代码,提高代码的重复利用率

//子类继承父类,会继承父类所有的特征和行为

//父类的成员变量无论什么可见度都可以被继承,但是私有的成员变量能被继承,却不能在子类里面进行使用

// [zom walk];

// [luZom walk];

//当方法调用的时候,优先使用自己类里面的方法,如果没有就使用父类的对应的方法

// TieTongZombie *tie = [[TieTongZombie alloc] init];

// [tie walk];

//练习一

// Student *stu = [[Student alloc] init];

// [stu setSex:@"女"];

// [stu setName:@"如来佛祖"];

// NSLog(@"%@", [stu sex]);

// NSLog(@"%@", [stu name]);

//
练习二

// int *p = NULL;

// *p = 10; // 对空指针操作,会崩溃

// printf("%p\n", *p);

// NSString *str= nil;

// NSLog(@"%@", str);

// OC把空指针视为空代码

//
练习三

// TieTongZombie *tie = [[TieTongZombie alloc] initWithBlood:100 attack:30 speed:50 equipment:@"铁桶" weakness:@"无"];

//
练习四

// Zombie *zom1 = [[Zombie alloc] init];

// NSLog(@"%p", zom1);

// [zom1 attack];

//

// Zombie *zom2 = [[Zombie alloc] init];

// NSLog(@"%p", zom2);

// [zom2 attack];

//哪个对象调用方法,对应的类里的self就是谁

/////////

//遍历构造器创建一个对象

// Person *per = [Person PersonWithName:@"玉皇大帝" sex:@"女" age:90];

// NSLog(@"%@", [per name]);
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: