@fiy-fish
2015-05-19T00:09:16.000000Z
字数 903
阅读 1411
Objective-c
// Person.h
// setter和getter
//
// Created by Aaron on 15/5/18.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
/*
4.有一个人,最大承重100KG,现在有2T货物,问最少要搬多少次?
*/
@interface Person : NSObject
{
int _maxWeight;
}
-(void)setMaxWeight:(int)weight;
-(int)timesForCarry:(int)weight;
@end
// Person.m
// setter和getter
//
// Created by Aaron on 15/5/18.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import "Person.h"
@implementation Person
-(void)setMaxWeight:(int)weight
{
if(_maxWeight != weight)
{
_maxWeight = weight;
}
}
-(int)timesForCarry:(int)weight
{
if(weight%_maxWeight == 0)
{
return weight/_maxWeight;
}
else
{
return weight/_maxWeight+1;
}
}
@end
// main.m
// 搬货物练习
//
// Created by Aaron on 15/5/18.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *p = [Person alloc];
[p setMaxWeight:100];
NSLog(@"需要%d次",
[p timesForCarry:2001]);
}
return 0;
}