2010-02-27 44 views
0

,如果我有一类名为将方法设置为对象或甚至将方法参数设置为对象意味着什么?

@interface myClass : NSObject { 

int firstnum; 
int secondnum; 

} 

//an a method that looks like this 

-(myClass *) myMethod (myClass *) f; 

@end 

那有什么方法回报?即时通讯过去看到像(int)myMethod知道它返回一个整数。但是当它在这里返回一个对象,比如类名时,它可能会返回什么呢?如果你想写出正在工作/学习的整个项目,就不好写了。 (有些方法已被截断,以保持问题简单,但如果你想生病后它让我知道,日Thnx

#import <Foundation/Foundation.h> 

@interface Fraction : NSObject { 
    int numerator; 
    int denominator; 
} 

@property int numerator, denominator; 

-(Fraction *) add: (Fraction *) f; 

@end 

#import "Fraction.h" 

@implementation Fraction 
@synthesize numerator, denominator; 

-(Fraction *) add: (Fraction *) f 
{ 
    Fraction *result = [[Fraction alloc] init]; 

    // To add two fractions: 
    // a/b + c/d = ((a*d) + (b*c))/(b * d) 

    result.numerator = numerator * f.denominator + denominator * f.numerator; 
    result.denominator = denominator * f.denominator; 

    [result reduce]; 
    return result; 
} 

@end 
+0

哦,也请用大写字母! :)在你的问题,那是;您的代码大部分遵循Obj-C约定。 (虽然'firstnum'和'secondnum'通常会驼峰likeThis) – andyvn22

+1

你需要一个冒号( ':')myMethod'后' – Felixyz

回答

1

该方法返回正是它声称:。一个指向myClass一个实例。在你add:方法的情况下,它返回指针result您创建

顺便说一下,除非你已经启用了垃圾收集,你应该添加[result autorelease]返回前该方法,否则你的结果部分将围绕永远和你“会泄漏内存。

CocoaDevCentral's Objective-C Tutorial将有助于这种内存管理,也可能让你更容易返回指向对象的方法。

1

的问题可能是:什么是对象?一个对象是留出一些内存来存放一堆值,并且与一些与这些值交互的方法相关联。你很少想要复制整个内存块。相反,您将指针传递到内存位置。所以当你传入或返回一个对象时,实际上只是传递一个指向内存中对象位置的指针。指针基本上只是整数值,所以这就是返回的内容。

在大多数语言中,你甚至不必去想这个,事实上,你很少有这样做的Objective-C的两种。请记住MyClass*是'MyClass'类的对象类型,它们响应在该类上声明的所有消息。

0

我要挑剔和尝试,并重写这个有点

#import <Foundation/Foundation.h> 

@interface Fraction : NSObject { 
    // You should really use the recommended type instead of int 
    NSInteger numerator; 
    NSInteger denominator; 
} 

// Don't try and keep these on one line. 
// Also, don't depend on default values for assign, retain, or copy when declaring 
@property (assign, nonatomic) numerator; 
@property (assign, nonatomic) denominator; 

// declare the add: function which takes a pointer to a Fraction as a parameter 
// and returns a pointer to the restulting fraction 
- (Fraction *)add:(Fraction *)aFraction; 

@end 

#import "Fraction.h" 

@implementation Fraction 

// Again, don't try and keep these on one line. 
// It's easier to visually note the number of synthesised properties matches 
// the number of declared properties. 
@synthesize numerator; 
@synthesize denominator; 

// Assume that there is an init function. 

- (Fraction *)add:(Fraction *)aFraction 
{ 
    Fraction *result = [[Fraction alloc] init]; 

    // To add two fractions: 
    // a/b + c/d = ((a*d) + (b*c))/(b * d) 

    result.numerator = self.numerator * aFraction.denominator 
     + denominator * aFraction.numerator; 
    result.denominator = self.denominator * aFraction.denominator; 

    [result reduce]; // I assume this is implemented. 
    return result; // Assuming garbage collection, otherwise use 
         // return [result autorelease]; 
} 

@end 
1

它看起来像你通过在Objective-C书的编程的拷贝工作。在我的副本中,第13章(底层C语言特性)的末尾有一节叫做“工作原理”。我认为你正在寻找“事实#2:对象变量确实是一个指针”。在你的情况下,add方法正在返回一个指向Fraction类的实例的指针。 (星号*,表示指针,类名称在它指出它指向的是什么类型之前)如果你不介意跳过,那么本章前面还有更多关于指针的内容。

相关问题