#import <Foundation/Foundation.h>
@interface Calculator : NSObject
//对象方法以 ‘-’ 减号标识
- (double) pi;
//参数类型之前用':'标识
- (int) square:(int)num;
//':'也是方法名的一部分
- (int) sumWithNum1:(int)num1 andNum2:(int)num2;//参数与参数之间要添加空格
//类方法以'+'加号标识(Java的静态方法)
//类方法与对象方法可以同名
//对象不能访问类方法
+ (double) pi;
@end
@implementation Calculator
- (double) pi {
return 3.14;
}
+ (double) pi {
return 3.14159;
}
- (int) square:(int)num {
return num * num;
}
//参数与参数之间要添加空格
- (int) sumWithNum1:(int)num1 andNum2:(int)num2 {
return num1 + num2;
}
@end
int main() {
Calculator *calculator = [Calculator new];
NSLog(@"%f", [calculator pi]);
NSLog(@"%i", [calculator square:10]);
NSLog(@"%i", [calculator sumWithNum1:10 andNum2:10]);
}
网友评论