实例方法—传 类参数

@interface Dog : NSObject

{

    NSString *dogName;  //狗狗的名称

    int       fighting; //狗的战斗力

}

 

//显示狗的信息

- (void)display:(NSString *)newName WithFighting:(int) newfighting;

//获取其他狗的信息

- (void)getOtherDogInfo:(Dog *) otherDog;

//两只打架并分出胜负

- (void)WinWithDog:(Dog *)otherDog;

@end

 
——————

#import "Dog.h"

 

@implementation Dog

 

#pragma mark 获取当前狗狗的信息

- (void)display:(NSString *)newName WithFighting:(int)newfighting

{

    dogName = newName;

    fighting = newfighting;

    NSLog(@"name:%@,fighting:%d",newName,newfighting);

}

 

#pragma mark 获取另一只狗狗的信息

- (void)getOtherDogInfo:(Dog *)otherDog

{

    NSLog(@"now:%@ - OtherDogInfo:name:%@,fighting:%d",dogName,otherDog->dogName,otherDog->fighting);

}

 

#pragma mark 两只狗互殴,分出胜负

- (void)WinWithDog:(Dog *)otherDog

{

    int ret =  fighting > otherDog->fighting?1:0;

    if(ret)

    {

        NSLog(@"%@:战5渣",dogName);

    }else

        NSLog(@"%@:你赢了",dogName);

}

@end

 

posted on 2016-04-16 13:20  小|晄  阅读(129)  评论(0编辑  收藏  举报

导航