这程式是书本上一题简单的例题,用物件 方法显示出a+bi
a为实部 b为虚部
有疑问的地方已经注解在程式码中
(@implementation 区段里面 print定义之内容)
想请问要如何在方法里,呼叫方法来给值
code 如下:(因为是前面单元范例,所以档案未分割)
//
// main.m
// prog1
//
// Created by Max on 2014/1/17.
// Copyright (c) 2014年 Max. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Complex: NSObject
-(void) setReal: (double) a;
-(void) setImaginary: (double) b;
-(void) print; // display as a+bi
-(double) real;
-(double) imaginary;
@end
@implementation Complex
{
double real;
double imaginary;
}
-(void) setReal: (double) a
{
real = a;
}
-(void) setImaginary: (double) b
{
imaginary = b;
}
-(void) print // display as a+bi
{
NSLog(@"The complex numbers is %f + %fi", real, imaginary);
//为何不能用NSLog(@"The complex numbers is %f + %fi", [Complex real],[Complex imaginary]);
}
-(double) real
{
return real;
}
-(double) imaginary
{
return imaginary;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Complex *Fraction = [Complex new];
[Fraction setReal:2];
[Fraction setImaginary:100];
[Fraction print];
NSLog(@"The complex numbers is %f + %fi", [Fraction real],[Fraction imaginary]);
}
return 0;
}