声明类 rectangle
//
// rectangle.h
// 0827-矩形面积和周长
//
// Created by panba on 15-8-28.
// Copyright (c) 2015年 panba. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface rectangle : NSObject
@property int width,height;
-(void) setWidth:(int)width andheight:(int)height;
-(int) returnarea;
-(int) returnperimeter;
@end
定义类
//
// rectangle.m
// 0827-矩形面积和周长
//
// Created by panba on 15-8-28.
// Copyright (c) 2015年 panba. All rights reserved.
//
#import "rectangle.h"
@implementation rectangle
@synthesize width,height;
-(void) setWidth:(int)x andheight:(int)y
{
width = x;
height = y;
}
-(int) returnarea
{
return width*height;
}
-(int) returnperimeter
{
return (width+height)*2;
}
@end
主函数
//
// main.m
// 0827-矩形面积和周长
//
// Created by panba on 15-8-28.
// Copyright (c) 2015年 panba. All rights reserved.
//
#import <Foundation/Foundation.h>
#include "rectangle.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
rectangle *myrect = [[rectangle alloc] init];
[myrect setWidth:5 andheight:10];
NSLog(@"width = %i,height = %i",myrect.width,myrect.height);
NSLog(@"area is %i",[myrect returnarea]);
NSLog(@"preimeter is %i",[myrect returnperimeter]);
}
return 0;
}