ObjectiveC类的使用

基本使用

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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
顾名思义,单例模式的特点就是保证一个仅有一个实例。因为这个模式只和一个有关,没有之间的关系,所有就不给出图示了。那么还是先说一下基本的定义。 单例模式(Singleton),保证一个仅有一个实例,并提供一个访问它的全局访问点。 通常我们可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个对象。一个最好的办法就是,让自身负责保存它的唯一实例。这个可以保证没有其他实例可以被创建,并且它可以提供一个访问该实例的方法。平时,我们常用单例模式的地方通常是多线程。 因为Objective C传承了Smalltalk语言,所以在Objective C中实现单例模式和C++和C#以及Java都不太一样。因为要保证型对象的单一性,所以就要考虑Objective C在实例化对象时候的各种方式。因为在Objective C中创建的各个型都继承自NSObject型,所以我们需要考虑NSObject型里实例化的方法,下面让我们展开来说。 在Objective C的实例化对象的方式主要有三种,分别如下: obj = [NSObject new]; obj = [[NSObject alloc]init]; obj = [[NSObject allocWithZone]init]; NSObject参考文档里记录第三种方法是因为历史原因遗留下来的,在当前的Objective C中已经不再使用,所以我们就不考虑这种方式了。下面让我们主要看一下前两种方式。 第一种方式,用new方法初始化其实是第二种方式的总和,当调用new方法时,其实是先调用了alloc方法进行isa(is a pointer)操作,创建指针,指向内存中的数据结构,紧接着调用了init方法对数据进行初始化,NSObject参考文档里也有具体的说明,大家也可以查看文档,具体实现方式随后我会用代码向大家进行展示。 第二种方式看起来就很明确了,先调用alloc创建指针指向内存中的数据结构,再调用init方法初始化数据。这里需要注意的是,init方法只是起到了初始化数据的作用,其实也可以自定义初始化方法,即完全可以自定义一个普通返回NSObject型的方法来代替init方法,即init方法是可以随意被代替的。只不过NSObject型中new方法默认会调用init方法而已,init方法可以看作是NSObject型的默认构造函数。 所以综上所述,其实只有alloc方法是每次必须调用的方法,那么我们只要控制住alloc方法,对此方法进行覆盖就可以保证型对象的单一性了。好了,说了这么多,让我们看看如何实现吧。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值