20150609构造方法和self指针

来源:互联网 发布:mac可以玩什么国内网游 编辑:程序博客网 时间:2024/06/05 08:52

//

//  Dog.h

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Dog : NSObject

{

    NSString *_name;

    NSInteger _age;

}

//构造方法必须以init开头,一般是initWithXxx,返回类型一定是id类型

//在一个类里面可以有多个构造方法但是一个对象只可以调用一次构造方法

//构造方法作用:(1).构造出来一个类的实例 (2).对构造出来个一个类的实例(对象)初始化。

//无参构造方法

- (id)init; //返回对象的地址


//带一个参数

- (id)initWithName:(NSString *)name;

- (id)initWithAge:(NSInteger)age;


//带两个参数

- (id)initWithAge:(NSString *)name andAge:(NSInteger)age;


//getter方法

- (NSString *)name;

- (NSInteger)age;


//setter方法

- (void)setName:(NSString *)name setAge:(NSInteger)age;


//类方法

+ (void)testDog;

@end



//******************************************************************

//

//  Dog.m

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


#import "Dog.h"


@implementation Dog


- (id)init

{

    if (self = [super init]) {

        

    }

    

    return self;

}


- (id)initWithName:(NSString *)name

{

    if (self = [super init]) {

        _name = name;

    }

    

    return self;

}


- (id)initWithAge:(NSInteger)age

{

    if (self = [super init]){

        _age = age;

    }

    

    return self;

}


- (id)initWithAge:(NSString *)name andAge:(NSInteger)age

{

    if (self = [super init]){

        _age = age;

        _name = name;

    }

    

    return self;

}


- (void)setName:(NSString *)name setAge:(NSInteger)age

{

    _name = name;

    _age = age;

}


- (NSString *)name

{

    return _name;

}


- (NSInteger)age

{

    return _age;

}


+ (void)testDog

{

    id dog = [self alloc];

    dog = [dog initWithAge:@"小白" andAge:3];

    NSLog(@"name = %@,age = %ld",[dog name],[dog age]);

}

@end

//===============================================

//

//  Cat.h

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


//*************************

//*       类方法(+)        *

//*************************

#import <Foundation/Foundation.h>


@interface Cat : NSObject

{

    NSString *_name;

    NSInteger _age;

}

+ (id)catCreate;


- (NSString *)name;

- (NSInteger)age;


- (void)setName:(NSString *)name setAge:(NSInteger)age;

@end

//==============================================

//

//  Cat.m

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


#import "Cat.h"


@implementation Cat

+(id)catCreate

{

    id cat = [[self alloc] init];

    

    return cat;


}


- (NSString *)name

{

    return _name;

}


- (NSInteger)age

{

    return _age;

}


- (void)setName:(NSString *)name setAge:(NSInteger)age

{

    _age = age;

    _name = name;

}

@end

//=============================================

//

//  Calculate.h

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Calculate : NSObject


+ (NSInteger)calculateSum:(NSInteger)x andX:(NSInteger)y;

@end

//==============================================

//

//  Calculate.m

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


#import "Calculate.h"


@implementation Calculate


+ (NSInteger)calculateSum:(NSInteger)x andX:(NSInteger)y

{

    return x+y;

}


@end

//============================================

//

//  main.m

//  IOS150609_ObjectiveC_ConstructFunctionAndSelf

//

//  Created by Peng Junlong on 15/6/9.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


//****************************

//*   构造方法与self指针       *

//****************************


#import <Foundation/Foundation.h>

#import "Dog.h"

#import "Cat.h"

#import "Calculate.h"

//#include  #import区别:

//1.#ifndef "Dog.h" #define "Dog.h" #endif   防止头文件被重定义

//2.#import不需要担心头文件被重复包含,重复包含会报错;#include则需要使用预编译指令


//@class打断循环链,防止循环包含


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        [Dog testDog];

        Dog *xiaohei = [[Dog alloc] initWithName:@"小黑"];

        NSLog(@"name = %@,age = %ld",[xiaohei name],[xiaohei age]);

        

        Cat *cat = [Cat catCreate];

        [cat setName:@"小花" setAge:4];

        NSLog(@"name = %@,age = %ld",[cat name],[cat age]);

        

       

        NSLog(@"Sum = %ld",[Calculate calculateSum:4 andX:7]);

        

    }

    return 0;

}


0 0
原创粉丝点击