在 Objective C、XCode 中定义类

发布于 2024-09-04 06:15:39 字数 1673 浏览 2 评论 0原文

我是 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

在用于调试目的的应用程序委托中,我添加了:

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

I am new to Objective C, and am trying to write a class that defines a complex number. The code seems fine but when I print to the console, my values for instance variables are 0.

Here is the code:

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

In the App Delegate for debugging purposes I added:

- (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;
}

But the console returns 0 everytime. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜夜流光相皎洁 2024-09-11 06:15:40

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

Instead of %d, use %f. D is for integers, F is for floating point ("real").

阿楠 2024-09-11 06:15:40

问题是您没有分配 -(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];

作为旁注:

self->real=a;与 real=a 相同。因为没有必要->你不应该使用它。如果您需要从另一个对象访问变量,您可以使用 getter 和 setter 方法。

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

The problem is that you don't allocate the return value of your -(ComplexNumber *)squared method.

You do it correctly when you create testNumber. You have..

ComplexNumber *d;

and

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

they should follow the same pattern (It's the general pattern for creating a new instance of an object).

You might want to define a custom initializer:-

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

and then instead of

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

You could use

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

As a sidenote:

self->real=a; is the same as real=a. As there is no need for -> you should not use it. If you need to access the variables from another onject you use the getter and setter methods.

Oh yeah, and as the other have pointed out. The format specifier in the NSLog is wrong as well.

宛菡 2024-09-11 06:15:40

有几件事。在您的 getter/setter 中,为什么要使用 self->real 特别是 -> 语法。我只见过/使用过 self.real。你的方法可能有效(?),但惯例是使用周期。另外为什么要手动创建 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];

A couple things. In your getters/setters why are you using self->real specifically the -> syntax. I've only seen/used self.real. You way may work (?) but the convention is to use a peroid. Also why are you manually creating the geters/seters. Unless you need to do more logic then just return and assign in them I'd suggest changing to this.

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

Let objective-c create your basic getters/setters for you via the @property line. You can then access/set them the following ways.

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文