@fiy-fish
2015-05-18T23:02:57.000000Z
字数 942
阅读 1321
Objective-c
// Cube.h
// setter和getter
//
// Created by Aaron on 15/5/18.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Cube : NSObject
{
int _length;
int _width;
int _height;
}
-(void)setLength:(int)length;
-(void)setWidth:(int)width;
-(void)setHeight:(int)height;
-(int)tiJi;
@end
// Cube.m
// setter和getter
//
// Created by Aaron on 15/5/18.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import "Cube.h"
@implementation Cube
-(void)setLength:(int)length
{
_length = length;
}
-(void)setWidth:(int)width
{
_width = width;
}
-(void)setHeight:(int)height
{
_height = height;
}
-(int)tiJi
{
return _length*_width*_height;
}
@end
// main.m
// 立方体练习
//
// Created by Aaron on 15/5/18.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Cube.h"
/*
创建两个立方体对象,长宽高分别为4,5,6和7,8,9,求体积各是多少?
*/
int main(int argc, const char * argv[])
{
@autoreleasepool {
Cube *cub1 = [Cube alloc];
[cub1 setLength:5];
[cub1 setWidth:6];
[cub1 setHeight:7];
NSLog(@"%d",[cub1 tiJi]);
}
return 0;
}