//XYPoint為自訂的一個類別
@interface XYPoint : NSObject
{
int x;
int y;
}
@property int x, y;
-(void)setX:(int) xVal andY:(int) yVal;
@end
@implementation XYPoint
@synthesize x,y;
-(void)setX:(int)xVal andY:(int)yVal
{
x = xVal;
y = yVal;
}
@end
//主要的測試類別,成員包含自定類別XYPoint, NSMutableString, int
@interface Foo : NSObject
{
XYPoint *o;
NSMutableString *name;
int X;
}
-(XYPoint *)o;
-(void)setO:(XYPoint *)pt;
-(NSMutableString *)name;
-(void)setName:(NSMutableString *)n;
-(int)X;
-(void)setX:(int)value;
@end
@implementation Foo
-(XYPoint *)o
{
return o;
}
-(void)setO:(XYPoint *)pt
{
o = pt;
}
-(NSMutableString *)name
{
return name;
}
-(void)setName:(NSMutableString *)n
{
name = n;
}
-(int)X
{
return X;
}
-(void)setX:(int)value
{
X = value;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
//先準備等一會要傳入Foo的變數
XYPoint *myPoint = [[XYPoint alloc]init];
NSMutableString *myName = [[NSMutableString alloc]initWithString:@"Jon"];
[myPoint setX:100 andY:200];
int myValue = 10;
//建立一個Foo物件,將成員設定為上述的變數
Foo *f = [[Foo alloc]init];
f.o = myPoint;
[f setName:myName];
[f setX:myValue];
NSLog(@"Before change myPoint, f.o.x : %d f.o.y : %d",f.o.x,f.o.y);
NSLog(@"Before change myName, f.name : %@",f.name);
NSLog(@"Before change myValue, f.X : %d",f.X);
//更改這些變數,看看是否為影響f內的成員
[myPoint setX:50 andY:50];
[myName setString:@"Tom"];
myValue = 5;
NSLog(@"After change myPoint, f.o.x : %d f.o.y : %d",f.o.x,f.o.y);
NSLog(@"After change myName, f.name : %@",f.name);
NSLog(@"After change myValue, f.X : %d",f.X);
}
return 0;
}
結果如下:Before change myPoint, f.o.x : 100 f.o.y : 200
Before change myName, f.name : Jon
Before change myValue, f.X : 10
After change myPoint, f.o.x : 50 f.o.y : 50
After change myName, f.name : Tom
After change myValue, f.X : 10
沒有留言:
張貼留言