// 13-【掌握】有参方法的声明实现和调用
#import <Foundation/Foundation.h>
void test(){
}
@interface Person : NSObject
{
@public
NSString *_name;
int _age;
float _weight;
}
//声明了一个有参数的方法,方法的返回值类型void;方法的名字是eat:参数有1个参数的类型NSString *;参数的名字FoodName
- (void)eat:(NSString *)FoodName;
- (void)eatWith:(NSString *)FoodName;
// 返回值类型void;方法名run: : :,参数有3个都是int型的,参数的参数名steps,km,times
- (void)run:(int)steps :(int)km :(int)times;
// 声明 三个参数的方法的方法名是runWith: andWith: andWith:,
- (void)runWith:(int)steps andWith:(int)km andWith:(int)times;
//类方法+,返回值类型int,方法名sumOfNum1:and:,参数2个都是int,参数的名字num1,num2
+ (int)sumOfNum1:(int)num1 and:(int)num2;
@end
@implementation Person
//实现有参数方法
//方法的方法名是eat: ,参数名FoodName
- (void)eat:(NSString *)FoodName{
NSLog(@" 感谢壮士 给在下 %@ 真好吃, %@将铭记于心 ",FoodName,_name);
}
- (void)eatWith:(NSString *)FoodName{
NSLog(@" 感谢壮士 eatWith 给在下 %@ 真好吃, %@将铭记于心 ",FoodName,_name);
}
//实现有两个参数的方法
// 返回值类型void;方法名run: : ,参数有2个都是int型的参数的参数名steps,km
- (void)run:(int)steps :(int)km :(int)times{
NSLog(@" %@ 大步夸出 %d下 , 瞬间移动到了 %d 万里之外 大小了 %d声 ",_name,steps,km,times);
}
//对方法的实现
- (void)runWith:(int)steps andWith:(int)km andWith:(int)times{
NSLog(@"带有with的方式 %@ 大步夸出 %d下 , 瞬间移动到了 %d 万里之外 大小了 %d声 ",_name,steps,km,times);
}
//类方法+,返回值类型int,方法名sumOfNum1: and: ,参数2个都是int参数的名字num1,num2
+ (int)sumOfNum1:(int)num1 and:(int)num2{
return num1 + num2;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person * zhansgan= [Person new] ;
zhansgan->_name = @"张三丰";
//用张三这个对象调用 带参数的 对象方法
//[对象名 方法名 参数]
[zhansgan eat:@"辣条"];
[zhansgan eatWith:@"鸡爪子 "];
//调用连个参数的对象方法
// [对象名 方法名:参数:参数];
[zhansgan run:3 :10 :5];
[zhansgan runWith:5 andWith:20 andWith:2];
//调用类方法
int rel = [Person sumOfNum1:10 and:20];
NSLog(@" rel = %d ",rel);
}
return 0;
}