@fiy-fish
2015-07-18T19:48:46.000000Z
字数 1278
阅读 1256
Objective-c
// main.m
// 偷吃
//
// Created by Aaron on 15/7/8.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
//2.幼儿园老师问,谁偷吃了苹果?
//孩子类回答说不知道
//女孩类回答 是他偷吃了
//男孩类回答 不是我
#import "Teacher.h"
#import "Boy.h"
#import "Girl.h"
#import "Child.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Teacher *t = [[Teacher alloc] init];
Boy *b = [[Boy alloc] init];
// [t askToBoy:b];
Girl *g = [[Girl alloc] init];
[t askToChild:g];
}
return 0;
}
//继承的作用2: 统一接口(不是最常用的)
//继承的作用3: 当我们觉得官方的类或者第三方的类库不太适用的时候,我们可以继承这些类,然后添加自己的属性和方法
//OC是单继承
//类簇不能继承
#import <Foundation/Foundation.h>
@interface Child : NSObject
-(void)answerToTeacher;
@end
#import "Child.h"
@implementation Child
-(void)answerToTeacher
{
NSLog(@"不知道");
}
@end
#import "Child.h"
@interface Girl : Child
@end
#import "Girl.h"
@implementation Girl
-(void)answerToTeacher
{
NSLog(@"是他吃的");
}
@end
#import <Foundation/Foundation.h>
#import "Child.h"
@interface Teacher : NSObject
//-(void)askToBoy:(Boy *)b;
-(void)askToChild:(Child *)c;
@end
#import "Teacher.h"
@implementation Teacher
//-(void)askToBoy:(Boy *)b
//{
// NSLog(@"谁偷吃了苹果?");
// [b boyAnswer];
//}
-(void)askToChild:(Child *)c
{
NSLog(@"谁偷吃了苹果?");
[c answerToTeacher];
}
@end
#import "Child.h"
@interface Boy : Child
//-(void)boyAnswer;
@end
#import "Boy.h"
@implementation Boy
//-(void)boyAnswer
//{
// NSLog(@"不是我");
//}
-(void)answerToTeacher
{
NSLog(@"不是我");
}
@end