面向对象的特征
1.封装(Encapsulation):封装是将客观事物表示为
一个类,并且对其它类隐藏数据和操作,从而实
现对内部数据和功能的保护
(1)
@interface Animal : NSObject
-(void)stup1;
-(void)stup2;
-(void)stup3;
-(void)work;
(2)
@implementation Animal
-(void)stup1
{
NSLog(@"1. 小明起床");
}
-(void)stup2
{
NSLog(@"2. 小明背着书包");
}
-(void)stup3
{
NSLog(@"3. 小明去炸学校");
}
-(void)work
{
[self stup1];
[self stup2];
[self stup3];
}
(3)
#import "ViewController.h"
#import "Animal.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Animal *animal = [[Animal alloc] init ];
[animal work];
2.继承(inheritance) :派生类具有基类定义的状态和行为
@interface Person : NSObject
-(void)eat;
@implementation Person
-(void)eat{
NSLog(@"吃麻辣烫”);
Person *p1 = [[Person alloc] init];
[p1 eat];
Bigson *p2 = [[Bigson alloc] init];
[p2 eat];
运行结果:吃麻辣烫;
吃麻辣烫;
3.多态(polymorphism):多态是面向对象编程的关键思想。
当不同类具备相同名称的方法时,互换调用该方法的对象
的类型,产生不同的行为。
Person *person = nil;
person = [Bigson new];
[person eat];
person = [Littleson new];
[person eat];
id temp = nil;
temp = [Bigson new];
[temp eat];
temp = [Littleson new];
[temp eat];