[問題] obj-c 基本問題
這程式是書本上一題簡單的例題,用物件 方法顯示出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;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.162.222.132
→
01/24 23:00, , 1F
01/24 23:00, 1F
→
01/24 23:22, , 2F
01/24 23:22, 2F
→
01/24 23:23, , 3F
01/24 23:23, 3F
→
01/25 01:48, , 4F
01/25 01:48, 4F
→
01/25 01:54, , 5F
01/25 01:54, 5F
→
01/25 01:55, , 6F
01/25 01:55, 6F
→
01/25 03:26, , 7F
01/25 03:26, 7F
→
01/25 03:27, , 8F
01/25 03:27, 8F
→
01/25 03:29, , 9F
01/25 03:29, 9F
→
01/25 03:29, , 10F
01/25 03:29, 10F
→
01/25 03:35, , 11F
01/25 03:35, 11F
→
01/25 09:44, , 12F
01/25 09:44, 12F
→
01/25 09:45, , 13F
01/25 09:45, 13F
→
01/25 09:46, , 14F
01/25 09:46, 14F
→
01/25 15:32, , 15F
01/25 15:32, 15F
→
01/25 15:32, , 16F
01/25 15:32, 16F
→
01/25 15:33, , 17F
01/25 15:33, 17F
推
01/25 23:31, , 18F
01/25 23:31, 18F
→
01/26 00:44, , 19F
01/26 00:44, 19F
→
01/26 00:44, , 20F
01/26 00:44, 20F
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章