利用类目扩充类目方法,以及延展实现

来源:互联网 发布:大数据在电商中的应用 编辑:程序博客网 时间:2024/04/30 22:04

#import <Foundation/Foundation.h>


//创建一个people类,继承于NSObject

@interface People : NSObject


//创建一个减号方法

-(void)playing;


//调用延展得到的方法

-(void)hello;

@end


//扩充类方法

@interface People (telephone)

-(void)playingTelephone;

@end


@interface People (computer)

-(void)playingComputer;

@end


#import "People.h"


//延展写在.m文件上

//延展不提供名称表示方法必须实现,提供名称可以不实现

//延展是定义私有方法

@interface People (text)

-(void)text;

@end


//实现减号方法

@implementation People


-(void)playing

{

    NSLog(@"正在玩");

}


//调用延展定义的私有方法

-(void)hello

{

    [self text];

}

-(void)text

{

    NSLog(@"你好");

}

@end


@implementation People (telephone)

-(void)playingTelephone

{

    NSLog(@"正在玩手机");

}


@end


@implementation People (computer)

-(void)playingComputer

{

    NSLog(@"正在玩电脑");

}

@end


//ViewController中调用

- (void)viewDidLoad

{

    [superviewDidLoad];

    People * p = [[Peoplealloc]init];

    [p playing];

    [p playingTelephone];

    [p playingComputer];

    [p hello];   

}



0 0