我是 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。帮助?