//
// main.m
// 类与对象
// Created by wu jianhua on 16-8-3.
// Copyright (c) 2016年 wujianhua. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Box:NSObject
{
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
}
@property(nonatomic, readwrite) double height; // Property
-(double) volume;
@end
@implementation Box
//和@property配对,实现@property声明的方法,在高版本的xcode中可以省略。
@synthesize height;
-(id)init
{
self = [super init];
length = 2.0;
breadth = 1.0;
return self;
}
-(double) volume
{
return length*breadth*height;
}
@end
int main( )
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Box *box1 = [[Box alloc]init]; // Create box1 object of type Box
Box *box2 = [[Box alloc]init]; // Create box2 object of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
box1.height = 5.0;
// box 2 specification
box2.height = 10.0;
// volume of box 1
volume = [box1 volume];
NSLog(@"Volume of Box1 : %f", volume);
// volume of box 2
volume = [box2 volume];
NSLog(@"Volume of Box2 : %f", volume);
[pool drain];
return 0;
}
objective-c之类与对象
最新推荐文章于 2020-01-07 01:44:53 发布

5222

被折叠的 条评论
为什么被折叠?



