Objective C访问子类中的基类属性?

时间:2011-07-26 15:41:44

标签: objective-c inheritance

我是ObjectiveC的新手,我有继承。

    @interface CGameEntity : NSObject {
        b2Body *entityBody;
    }

    @property(nonatomic,retain) CCSprite *entitySprite;

   -(id)initEntity:(CCNode*)parentNode :(b2World*)world;
   @end

实施:

@implementation CGameEntity

@synthesize entitySprite=entitySprite;
.
.
.
@end

扩展课程如下:

@interface CPlanet : CGameEntity {

}
@end

实施如下:

@implementation CPlanet

-(id)initEntity:(CCNode*)parentNode :(b2World*)world
{
    if((self = [super init]))
    {
        //cannot access "entitySprite" ????
        entitySprite=[CCSprite spriteWithFile:@"planet.png"];

    }
    return self;
}

@end

在扩展类中,我无法访问属性“entitySprite”。 如何访问基类的属性?

谢谢

1 个答案:

答案 0 :(得分:5)

首先,替换......

@synthesize entitySprite=entitySprite;

通过......

@synthesize entitySprite;

然后,替换这一行...

entitySprite=[CCSprite spriteWithFile:@"planet.png"];

由此......

self.entitySprite = [CCSprite spriteWithFile:@"planet.png"];

这将起作用=)!