2010-02-27 80 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類的實例的指針。 (星號*,表示指針,類名稱在它指出它指向的是什麼類型之前)如果你不介意跳過,那麼本章前面還有更多關於指針的內容。

相關問題