ObjectiveC类的使用

来源:互联网 发布:win7网络共享无法访问 编辑:程序博客网 时间:2024/04/30 13:46

基本使用

lijun.h

////  Header.h//  example////  Created by junli on 15/4/3.//  Copyright (c) 2015年 junli. All rights reserved.//#ifndef example_Header_h#define example_Header_h/* Title:demo class usage in objective C. author:kagula date:2015-04-04 precondition:familiar with visual c++. environment:xcode 6.2, Mac OS 10.10.2 note: [1]if you class no parent,must be derived from NSObject class. [2]the language only allow one parent class, but allow multi interface,    here, class name interface, interface name protocol. [3]disallow friend keyword. [4]disallow operator redefine.  frequently shortcut different from visual studio: cmd is win key: cmd+b build cmd+r run ctrl+cmd+up(down) switch to corresponding file cmd+alt+left fold code block cmd+alt+right expand code block alt+left skip to left word alt+right skip to right word more shortcuts you should looking for in the top menu.  */@interface lijun:NSObject{    int privateMember;//default is private access mode.@protected    int protectedMember;@public    int publicMember;    int publicMember2;<span style="font-family: Arial, Helvetica, sans-serif;">//member variable only can be used in interior or this level.</span>    //if you want exterior access, need using property.}/* Objective C only have two type member function. one is normal, the other is static store type. */-(id)init:(int) a labelB:(int)b;-(void)func;-(int)funcWithReturnValue;-(int)func1:(int) x;-(int)func2:(int)x andY:(int) y;+(int)funcStatic;//this is static store type member function.@end#endif


lijun.m

////  lijun.m//  example////  Created by junli on 15/4/4.//  Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>#import "lijun.h"@implementation lijun-(id)init{    if (self=[super init]) {        privateMember = 10;        protectedMember = 20;        publicMember = 100;    }    NSLog(@"i am constructor,all your initialization should be here!");    return self;//self like this notation in c++.}-(id)init:(int) a labelB:(int)b{        if (self=[super init]) {        privateMember = a;        protectedMember = b;        publicMember = a*b;    }    NSLog(@"i am constructor,all your initialization should be here!");    return self;//self like this notation in c++.}-(void)func{    NSLog(@"i am no return no argument function!");}-(int)funcWithReturnValue{    return privateMember;}-(int)func1:(int) x{    return x*10;}-(int)func2:(int)x andY:(int) y{    return x*y;}+(int)funcStatic{    NSLog(@"demo static member function invoke!");    return 0;}-(void)dealloc{    NSLog(@"i am destructor equality with c++!");}@end


main.m

////  main.m//  example////  Created by junli on 15/4/3.//  Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>#import "lijun.h"void illustrateClassUsage(){    {        lijun *obj = [[lijun alloc]init];                NSLog(@"demo invoke function without arguments[%d]",[obj funcWithReturnValue]);        NSLog(@"demo invoke function with one arguments[%d]",[obj func1:123]);        NSLog(@"demo invoke function with one arguments[%d]",[obj func2:1 andY:2]);                NSLog(@"demo access public member of the object %d",obj->publicMember);                //obj of lijun will be release in automatic.    }        {        //initialization object with arguments        lijun *obj = [[lijun alloc]init:10 labelB:2];        NSLog(@"demo access public member of the object %d",obj->publicMember);    }}int main(int argc, const char * argv[]) {    @autoreleasepool {        illustrateClassUsage();    }        //invoke static member function of lijun.    [lijun funcStatic];    return 0;}


继承与多态


polymorphism.h

////  Polymorphism.h//  example////  Created by junli on 15/4/7.//  Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>#import "Ancestor.h"//protocl as c++'s pure virtual abstract class!//        as other like "C" language's interface.@protocol MouseListener-(BOOL)mousePressed;@optional//this indicator will suppress no impplement "mouseClicked" warning in xcode.-(BOOL)mouseClicked;//this method can not be implemented by class.@end@protocol KeyboardListener-(BOOL)keyPressed;@end/*[1]only allow one class(in objective C name is interface) to derived from, but you can have many interface(in objective C name is protocol).[2]no access qualifier to instrain parent interface and protocol.[3]no access qualifier for interface's  member method. @interface Polymorphism : NSObject<MouseListener,KeyboardListener>if no Ancestor,should using NSObject instead, demo is above. */@interface Polymorphism : Ancestor<MouseListener,KeyboardListener>/* Here, do not need declare protocol's method,you can implement a method in protocol or not. */@end


polymorphism.m

////  Polymorphism.m//  example////  Created by junli on 15/4/7.//  Copyright (c) 2015年 junli. All rights reserved.//#import "Polymorphism.h"@implementation Polymorphism-(BOOL)mousePressed{    NSLog(@"Polymorphism mousePressed");    return TRUE;}-(BOOL)keyPressed{    NSLog(@"Polymorphism keyPressed");    return TRUE;}@end


ancestor.h

////  Ancestor.h//  example////  Created by junli on 15/4/7.//  Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>@interface Ancestor : NSObject-(void)live;@end


ancestor.m

////  Ancestor.m//  example////  Created by junli on 15/4/7.//  Copyright (c) 2015年 junli. All rights reserved.//#import "Ancestor.h"@implementation Ancestor-(void)live{    NSLog(@"Ancestor is living inside!");}@end


how to use them?

void illustratePolymorphism(){    //invoke interface method    Polymorphism *pol = [[Polymorphism alloc]init];    BOOL bR = [pol mousePressed];    NSLog(@"%@",bR?@"YES":@"NO");        //interface member will not be find.    if([pol respondsToSelector:@selector(mouseClicked)])    {        [pol mouseClicked];    }    else    {        NSLog(@"the method mouseClicked is not implemented!");    }        //invoke ancestor method    [pol live];        //upcast pointer, and invoke it's member.    Ancestor *pAncestor = (Ancestor*)pol;    [pAncestor live];        //downcast pointer, and invoke it's member.    Polymorphism *pPol = (Polymorphism*)pAncestor;    [pPol live];}

in a interface, the method name can be same but argument list must be difference for compiler discriminate who is who.


0 0
原创粉丝点击