Objective c 继承与实例变量的混淆

Objective c 继承与实例变量的混淆,objective-c,Objective C,我不明白为什么我的实例变量在我的子类中不起作用,即使实例变量是在父类的接口文件中声明的。我的子类从父类继承一个方法来定义实例变量。然后子类使用它自己的一个方法来显示实例变量的值,但是这些值是零?为什么呢 /interface file of parent class **/ #import <Foundation/Foundation.h> @interface Rectangle : NSObject { int w; int h; int j;

我不明白为什么我的实例变量在我的子类中不起作用,即使实例变量是在父类的接口文件中声明的。我的子类从父类继承一个方法来定义实例变量。然后子类使用它自己的一个方法来显示实例变量的值,但是这些值是零?为什么呢

/interface file of parent class **/
#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
    int w;
    int h;
    int j;
    int k;
    int a;
    int b;
}

 @property int width, height;


 -(void) define;


@end

/**implementation file of parent class **/   
#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

-(void)define{

    a = width;
    b = height;

}


@end

/**interface file of subclass **/
#import "Rectangle.h"

@interface Rectangle2 : Rectangle

@property int width, height;

-(void) show;

@end

/**implementation file of subclass **/

#import "Rectangle2.h"

@implementation Rectangle2

@synthesize width, height;


-(void) show{
    NSLog(@"the width and height are %i and %i", width, height);
    NSLog(@" a and b are %i and %i", a, b);
}



@end

/**Main**/
#import "Rectangle.h"
#import "Rectangle2.h"

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

        Rectangle * shape1;
        shape1 = [[Rectangle alloc] init];

        Rectangle2 * shape2;
        shape2 = [[Rectangle2 alloc] init];


        shape1.width =10;
        shape1.height = 5;


        shape2.width =2;
        shape2.height = 3;

        [shape2 define];
        [shape2 show];



    }

    return(0);

}
父类的接口文件**/ #进口 @接口矩形:NSObject { int w; int-h; int j; int k; INTA; int b; } @属性int宽度、高度; -(无效)定义; @结束 /**父类的实现文件**/ #导入“Rectangle.h” @实现矩形 @综合宽度、高度; -(无效)定义{ a=宽度; b=高度; } @结束 /**子类的接口文件**/ #导入“Rectangle.h” @接口矩形2:矩形 @属性int宽度、高度; -(无效)显示; @结束 /**子类的实现文件**/ #导入“矩形2.h” @实现矩形2 @综合宽度、高度; -(无效)显示{ NSLog(@“宽度和高度分别为%i和%i”,宽度和高度); NSLog(@“a和b是%i和%i”,a,b); } @结束 /**主要**/ #导入“Rectangle.h” #导入“矩形2.h” int main(int argc,const char*argv[] { @自动释放池{ 矩形*形状1; shape1=[[Rectangle alloc]init]; 矩形2*形状2; shape2=[[Rectangle2 alloc]init]; 形状1.宽度=10; 形状1.高度=5; 形状2.宽度=2; 形状2.高度=3; [shape2定义]; [shape2 show]; } 返回(0); } 我的程序显示以下内容:

矩形6[900:303]宽度和高度分别为2和3

2013-07-15 20:09:35.625矩形6[900:303]a和b分别为0和0


为什么a和b是0?既然这些实例变量是在父类的继承文件中声明的,我难道不能在子类中使用它们吗?我没有收到任何错误,因此我知道我们正在访问实例变量,但为什么我在运行时没有显示正确的值?

您应该使用派生类对象调用基类方法和变量

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
int w;
int h;
int j;
int k;
int a;
int b;
}

@property int width, height;
-(void) define;
@end

#import "Rectangle.h"

@implementation Rectangle
@synthesize width, height;

-(id)init
{
    if (self=[super init]) {

    }
    return self;
}

-(void)define{
    a = width;
    b = height;
}

@end
输出为:-------

[参考译文]宽度和高度分别是2和3
2013-07-16 09:26:49.277 rec[894:11303]a和b分别是5和10

您应该使用派生类对象调用基类方法和变量

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
int w;
int h;
int j;
int k;
int a;
int b;
}

@property int width, height;
-(void) define;
@end

#import "Rectangle.h"

@implementation Rectangle
@synthesize width, height;

-(id)init
{
    if (self=[super init]) {

    }
    return self;
}

-(void)define{
    a = width;
    b = height;
}

@end
输出为:-------

[参考译文]宽度和高度分别是2和3
2013-07-16 09:26:49.277 rec[894:11303]a和b是5和10

我们可以看看这在其他类中是如何使用的吗?是的,请显示子类。@rezand抱歉,伙计们,不确定你们的意思。我不是在展示子类是如何工作的吗?你能澄清一下你想让我展示什么吗?代码在那里,我想由于空格的原因,我们忽略了滚动条。我们能看看这在其他类中是如何使用的吗?是的,请展示子类。@rezand抱歉,伙计们,不确定你们的意思。我不是在展示子类是如何工作的吗?你能澄清一下你想让我展示什么吗?代码在那里,我想由于间距的原因,我们忽略了滚动条。@Hercules我还是objective-c的新手,所以我不确定我是否遵循了你试图证明的内容。代码的init方法做什么?(id)init{if(self=[super init]){}返回self;}程序检查当前对象是否等于超类的init方法的结果,然后返回self?为什么if语句的括号中没有任何内容?@somid可能会也可能不会对您的question@somid作为u init派生类,该基类的init也使用派生类的init方法中的[super init]调用进行初始化。感谢您尝试解释这一点,但我在后面遇到了困难。我想这会有助于理解为什么我提供的代码不起作用。谢谢你到目前为止对她的帮助ules@Hercules我对objective-c还不熟悉,所以我不确定我是否遵循了你试图证明的内容。代码的init方法做什么?(id)init{if(self=[super init]){}返回self;}程序检查当前对象是否等于超类的init方法的结果,然后返回self?为什么if语句的括号中没有任何内容?@somid可能会也可能不会对您的question@somid作为u init派生类,该基类的init也使用派生类的init方法中的[super init]调用进行初始化。感谢您尝试解释这一点,但我在后面遇到了困难。我想这会有助于理解为什么我提供的代码不起作用。谢谢你到目前为止的帮助@Herçules
int main(int argc, char *argv[])
{
    @autoreleasepool {

        Rectangle2 * shape2;
        shape2 = [[Rectangle2 alloc] init];

        shape2.width =2;
        shape2.height = 3;

        [shape2 show];


        return(0);
    }
}