類的擴充--類目--ios

來源:互聯網
上載者:User

標籤:blog   os   io   檔案   div   log   ef   on   

 person+money.h  這是類目類

#import "Person.h"//這是擴充person類的介面類,獨立一個檔案@interface Person (Money)-(void) haveMoney;@end

 person+money.m 這是類目類

#import "Person+Money.h"//這是擴充person類的實作類別,獨立一個檔案@implementation Person (Money)-(void) haveMoney{    NSLog(@"人人有錢");}@end

 person.h

#import <Foundation/Foundation.h>//這是原始類@interface Person : NSObject@property(nonatomic,copy) NSString *name;@property(nonatomic,retain) NSNumber *age;-(void) originTest;@end//這是原始類的擴充類,直接定義與原始類同一檔案中@interface Person (Creat)+(id) personWithName:(NSString *) name;@end//這是原始類的擴充類,直接定義與原始類同一檔案中@interface Person (Test)-(void) test;@end

 person.m

#import "Person.h"//這是person類的實作類別@implementation Person:NSObject-(id) init{    self=[super init];    NSLog(@"調用類自訂的init 方法");    return self;}+(id) personWithName:(NSString *) name{    Person *person=[[Person alloc] init];    person.name=name;    return person;}//這是person類的實作類別,直接定義與原始類同一檔案中-(void) test{    NSLog(@"這是類目定義的方法");}-(void) originTest{  NSLog(@"這是原始類的方法");}-(void) haveMoney{  NSLog(@"人人有錢");}@end

 person_family.h  這是延展的類

#import "Person.h"@interface Person ()-(void) haveFamily;@end

 

main.m

  //類目擴充學習

        Person *person=[Person personWithName:@"caictou"];

        //這是原始類定義的方法

        [person originTest];

        //這是同一個檔案的擴充類定義的方法

        [person test];

        //這是獨立一個檔案的擴充類定義的方法

        [person haveMoney];

        //這是對立一個檔案的延展類定義的方法

        [person haveFamily];

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.