OC6_类方法

//

//  Dog.h

//  OC6_类方法

//

//  Created by zhangxueming on 15/6/9.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface Dog : NSObject

{

    NSString *_name;

    NSInteger _age;

}



+ (id)dogCreate;



- (NSString *)name;

- (NSInteger)age;



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



@end
//

//  Dog.m

//  OC6_类方法

//

//  Created by zhangxueming on 15/6/9.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "Dog.h"



@implementation Dog



+ (id)dogCreate

{

    Dog *dog = [[Dog alloc] init];

    return dog;

}



- (NSString *)name

{

    return _name;

}



- (NSInteger)age

{

    return _age;

}



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

{

    _name = name;

    

    _age = age;

}



@end
//

//  main.m

//  OC6_类方法

//

//  Created by zhangxueming on 15/6/9.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "Dog.h"



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

    @autoreleasepool {

        

        Dog *xiaoBai = [Dog dogCreate];

        

         //Dog *dog = [[Dog alloc] init];



        

        [xiaoBai setName:@"小白" andAge:12];

        NSLog(@"name = %@ age = %li", [xiaoBai name], [xiaoBai age]);

    }

    return 0;

}

 

你可能感兴趣的:(方法)