Calculate.h文件函數
#import <Foundation/Foundation.h>spa
@interface Calculate : NSObject{.net
float number1;3d
float number2;orm
float sum;get
float mean;io
}class
-(void)setNumber1:(float)newNumber1 setNumber2:(float)newNumber2;import
-(float)sum;float
-(float)mean;
-(void)NSLog;
Calculate.文件
#import "Calculate.h"
@implementation Calculate
/*self的使用方法:
self的意思是:自己
本身調用本類函數,意思就是你如今創建了一個類,然而你在這個類裏面多個方法, 當你要在當前這個類裏面,某個方法須要用到本類中的另外一個方法時就會用到self。
*/
-(void)setNumber1:(float)newNumber1 setNumber2:(float)newNumber2{
number1=newNumber1;
number2=newNumber2;
}
-(float)sum{
sum= number1+number2;
return sum;
}
-(float)mean{
//self調用
mean= [self sum]/2;
return mean;
}
-(void)NSLog{
NSLog(@"這兩個數之和爲:%.2f 這兩個數的平均數爲:%.2f", /*self調用*/[self sum],[self mean]);
}
main.m文件
#import <Foundation/Foundation.h>
#import "Calculate.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Calculate* result=[Calculate new];
[result setNumber1:15 setNumber2:5];
[result NSLog];
}
return 0;
}