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 と同じです。-> の必要がないので、使用しないでください。別の onject から変数にアクセスする必要がある場合は、getter メソッドと setter メソッドを使用します。

そうそう、他の方が指摘されている通りです。NSLog のフォーマット指定子も間違っています。

于 2010-06-03T15:11:42.917 に答える
-1

いくつかのこと。ゲッター/セッターで、なぜself->real具体的に->構文を使用しているのですか。私は見た/使用しただけself.realです。あなたはうまくいくかもしれませんが(?)、慣習はピロイドを使用することです。また、ゲッター/セッターを手動で作成するのはなぜですか。さらにロジックを実行する必要がない限り、戻ってそれらに割り当てるだけです。これに変更することをお勧めします。

//.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

@property 行を介して、objective-c に基本的なゲッター/セッターを作成させます。その後、次の方法でアクセス/設定できます。

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 に答える