2

我是 Objective C 的新手,正在尝试编写一个定义复数的类。代码看起来不错,但是当我打印到控制台时,实例变量的值为 0。

这是代码:

//
//  ComplexNumber.h
//  Mandelbrot Set
//
//  Created by Brett on 10-06-02.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <stdio.h>


@interface ComplexNumber : NSObject {

 double real;
 double imaginary;

}

// Getters
-(double) real;
-(double) imaginary;



// Setters
-(void)setReal: (double) a andImaginary: (double) b;

//Function
-(ComplexNumber *)squared;

@end


//
//  ComplexNumber.m
//  Mandelbrot Set
//
//  Created by Brett on 10-06-02.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ComplexNumber.h"
#import <math.h>
#import <stdio.h>

@implementation ComplexNumber

-(double)real{
 return self->real;
}

-(double)imaginary{
 return self->imaginary;
}


-(void)setReal: (double) a andImaginary: (double) b{

 self->real=a;
 self->imaginary=b;

}

-(ComplexNumber *)squared{

 double a = pow(real,2);
 double b = pow(imaginary, 2);
 double c = 2*real*imaginary;

 ComplexNumber *d;
 [d setReal:(a-b) andImaginary: c];

 return d;
}

@end

在用于调试目的的 App Delegate 中,我添加了:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

 ComplexNumber *testNumber = [[ComplexNumber alloc] init];
 [testNumber setReal:55.0 andImaginary:30.0];
 NSLog(@"%d", testNumber.real);

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

 return YES;
}

但是控制台每次都返回 0。帮助?

4

3 回答 3

5

而不是%d,使用%f. D 代表整数,F 代表浮点数(“实数”)。

于 2010-06-03T15:02:58.713 回答
2

问题是您没有分配 -(ComplexNumber *)squared 方法的返回值。

您在创建 testNumber 时正确地执行了此操作。你有..

ComplexNumber *d;

ComplexNumber *testNumber = [[ComplexNumber alloc] init];

它们应该遵循相同的模式(这是创建对象的新实例的一般模式)。

您可能想要定义一个自定义初始化程序:-

- (id)initWithReal:(double)a andImaginary:(double)b { 
  self = [super init];
  if(self){
    real = a;
    imaginary = b;
  }
  return self;
}

然后代替

ComplexNumber *testNumber = [[ComplexNumber alloc] init];
[testNumber setReal:55.0 andImaginary:30.0];

你可以使用

ComplexNumber *testNumber = [[ComplexNumber alloc] initWithReal:55.0 andImaginary:30.0];

作为旁注:

自我->真实=a;与 real=a 相同。因为不需要 -> 你不应该使用它。如果您需要从另一个对象访问变量,请使用 getter 和 setter 方法。

哦,是的,正如其他人指出的那样。NSLog 中的格式说明符也是错误的。

于 2010-06-03T15:11:42.917 回答
-1

几件事。在您的 getter/setter 中,您为什么要self->real专门使用->语法。我只见过/使用过self.real. 您的方式可能有效(?),但惯例是使用 peroid。另外,为什么要手动创建 geters/seters。除非您需要做更多的逻辑,否则只需返回并分配它们,我建议您更改为此。

//.h
#import <Foundation/Foundation.h>
#import <stdio.h>

@interface ComplexNumber : NSObject {

      double real;
      double imaginary;
}
   @property (nonatomic) double real;
   @property (nonatomic) double imaginary;

   //Function
   -(ComplexNumber *)squared;
@end



//.m
#import "ComplexNumber.h"
#import <math.h>
#import <stdio.h>

@implementation ComplexNumber

-(ComplexNumber *)squared{

 double a = pow(real,2);
 double b = pow(imaginary, 2);
 double c = 2*real*imaginary;

 ComplexNumber *d = [[ComplexNumber alloc] init];
 d.real = a-b;
 d.imaginary = c;
 return d;
}

@end

让objective-c 通过@property 行为您创建基本的getter/setter。然后,您可以通过以下方式访问/设置它们。

ComplexNumber *d = [[ComplexNumber alloc] init];
d.real = 55; //or
[d setReal:55];
//with reading as easy
double real = d.real; //or
double real = [d real];
于 2010-06-03T15:13:26.883 回答