我们先建立一下工程,其中Son和SonA、SonB继承于Father类,Cat和Dog继承于Animal类,其余继承于NSObject
将所有头文件都导入main.m中
建立完工程之后,我们想来了解一下相关的概念
我们都知道,面向对象的三大特征是:封装、继承、多态
✪封装:将属性及方法相结合、共同体现对象的特性。可以
隐藏内部实现、稳定外部接口
♢好处:
使用起来更加简单
变量更加安全
可以隐藏内部实现
开发速度更加快捷
♢作用:
类来封装了属性和方法
方法封装了实现的代码
属性来封装了成员变量
✪继承:继承是避免继承冗余,提高代码的可重用性和维护的有效手段
◎继承的传递性: 直接父类 间接父类
◎继承需要符合的关系 : is-a子类和父类都需要满足 is-a 关系,才存在继承,继承概念下的 is-a 关系 是个 单向 的关系
◎子类具有父类的属性和行为,以及自身特殊的属性和行为。
继承的特点:书本57页
1. 使用继承可以实现代码的复用,减少代码冗余。
2.Objective-C 中一个类可以继承另一个类,被继承的类称为父类或超类(基类),继承的类称为子类或派生类(孩子类)。
3.子类可直接“拥有”父类中所有允许子类继承的属性和方法。
4.子类可以改变父类中已有的方法,执行不同的代码实现。
5.Objective-C中只允许单一继承,因为多重继承会有称为“致命方块”的问题。
✪多态:多态就是对于不同对象响应同一个方法做出的不同反应,它是建立在继承的基础上面。
1.继承于同一父类的子类,他们本身具有自己的特征
2.继承于同一父类的子类,他们在执行同一命令的时候,可以具有不同效果
多态的好处:
1.可以简化编程接口
▶ 允许多个类中定义同一消息接口;
▶ 可以定义一个通用的调用方法,以简化调用。
2.把不同的子类对象都当作父类来看
◀可以屏蔽不同子类对象之间的差异,写出通用的代码。
◀做出通用的编程,以适应需求的不断变化。
两大原则:开闭原则 和 里氏替换原则
◎开闭原则: 对扩展开放,对修改关闭
◎里氏替换原则:任何基类可以出现的地方,子类一定可以出现
下面我们来举个大栗子~
首先先看到Student.h,举个小栗子说明一下上面的封装概念
//使用类来 封装 成员变量
// @public
// NSString *_name;
// NSInteger _age;
// NSString *_homeAddress;
//使用@property 封装成员变量,实现变量安全
@property (nonatomic,strong)NSString *name;
@property(nonatomic ,assign)NSInteger age;
@property(nonatomic,strong)NSString *homeAddress;
//使用类来封装了功能代码
-(void)helloWorld;
到Student.m中实现一下
//重写init方法
-(id)init{
if (self = [super init]) {
_name = @"Bob";
_age = 19;
_homeAddress = @"GD";
}
return self;
}
-(void)helloWorld{
NSLog(@"Hello World!");
//打印哪个类里面的哪个方法
// NSLog(@"%s",__FUNCTION__);
NSLog(@"%s",__func__);//一样
[self hiGuys];
}
//私有方法:@interface中没有相关声明的方法,可以把它们看做私有方法,仅在类的实现文件中使用
-(void)hiGuys{
NSLog(@"我是Mr.Lu私有方法");
NSLog(@"%s",__func__);
}
接着,我们去到Father类,给接口文件定义相关属性和方法
@property (nonatomic ,strong)NSString *name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,strong)NSString *hobby;
-(void)helloMyson;
-(void)charge;
去Father.m实现一下
-(void)helloMyson{
NSLog(@"hello,I am your father, my name is %@ ",self.name);
}
-(void)charge{
NSLog(@"Your father is rich ,this card 随便刷")
好,接着,我们去到NotInheritedSon,其接口和实现文件和father一致,command c和command v 就好了。其实我觉得没必要建立这个工程,只是为了区分继承而已。
然后我们再写一下Son.h文件:
@interface Son : Father
@property(nonatomic,strong)NSString *homeAddress;
//@property(nonatomic,)
-(void)race;
给他一个属性和方法,注意,他是继承于Father,所以拥有Father的方法
Son.m
//重写父类中charge的方法
-(void)charge{
NSLog(@"爸,拿你的卡刷一下!");
[super charge];
}
-(void)race{
NSLog(@"我爸真有钱!");
}
-(void)helloMyson{
NSLog(@"homeAddress is %@",self.homeAddress);
}
再来看到SonA跟SonB,这两个工程其实也并无太大必要,为的也是更好的解释继承。
SonA和SonB的接口文件都为空即可。
SonA.m实现文件:
-(void)helloMyson{
[super helloMyson];
NSLog(@" I am the First son!");
}
SonB.m实现文件:
-(void)helloMyson{
[super helloMyson];
NSLog(@"I am the Second son");
}
接着,我们利用动物类和人类来为大家解释一下多态。
首先写一下Animal类
接口文件给它一个吃的方法-(void)eat;
实现一下
-(void)eat{
NSLog(@"动物吃饭啦!");
}
点到它的两个子类,猫和狗,我们给它们调用一下父类 吃 的方法(接口文件为空)
Cat.m:-(void)eat{
[super eat];
NSLog(@"猫吃鲨鱼");
}
Dog.m:-(void)eat{
[super eat];
NSLog(@"骨头吃狗");
}
然后,我们来实现一下人喂动物的功能
敲一下Person.h的代码:
#import "Cat.h"
#import "Dog.h"
#import "Animal.h"
@interface Person : NSObject
-(void)feedCat:(Cat *)cat;
-(void)feedDog:(Dog *)dog;
-(void)feedAnimal:(Animal *)animal;
其中,当出现编译错误时,注意看看头文件是否导入
Person.m的代码:
-(void)feedCat:(Cat *)cat{
NSLog(@"人钓鲨鱼喂猫猫");
[cat eat];
}
-(void)feedDog:(Dog *)dog{
NSLog(@"人放狗来喂骨头");
[dog eat];
}
-(void)feedAnimal:(Animal *)animal{
if ([animal isMemberOfClass:[Cat class]]) {
NSLog(@"人钓鲨鱼喂猫!");
[animal eat];
}
if ([animal isMemberOfClass:[Dog class]]) {
NSLog(@"人买狗喂骨头!");
[animal eat];
}
}
不要忘了,还有main.m函数
Student *stu = [[Student alloc]init];
[stu helloWorld];
Father *father = [[Father alloc]init];
father.name = @"David";
[father helloMyson];
[father charge];
NotInheritedSon *aSon = [[NotInheritedSon alloc]init];
aSon.name = @"Barry";
[aSon helloMyson];
[aSon charge];
Son *son = [[Son alloc]init];
[son charge];
son.homeAddress = @"GD";
[son helloMyson];
[son race];
NSString *_name = @"Bob";
NSInteger _age = 19;
NSString *_homeAddress = @"GD";
NSLog(@"学生名:%@,He is %ld years old,地址:%@",_name,_age,_homeAddress);
SonA *sonA = [[SonA alloc]init];
sonA.name = @"Mr.Zhang";
[sonA helloMyson];
SonB *sonB = [[SonB alloc]init];
sonB.name = @"Jason";
//1.可以简化接口
id animal = nil; //由于id类型的通用性质,我们可以将创建任意对象赋值给animal
animal = [Cat new];
[animal eat];
animal = [Dog new];
[animal eat];
//2.把不同的子类当作父类来看
Animal *animalB = nil;
animalB = [Cat new];
[animalB eat];
animalB = [Dog new];
[animalB eat];
Cat *cat = [Cat new];
Dog *dog = [Dog new];
Person *p = [[Person alloc]init];
// [p feedCat:cat];
// [p feedDog:dog];
[p feedAnimal:cat];
[p feedAnimal:dog];
}
return 0;
}
多态就是对于不同对象响应同一个方法做出的不同反应,它是建立在继承的基础上面。
Cat和Dog都是动物,继承于动物,所以可以直接用动物来替代猫和狗;
最后,我们来看一下运行结果: