抽象类的例子(151)

#import <Foundation/NSObject.h>
#import <Foundation/NSGeometry.h>

@class NSString;

@interface Figure : NSObject
@property(assign) NSPoint location; //设置图形位置
- (void)setSize:(NSSize)newsize; //指定图形大小
- (double)area; //计算图形面积
- (NSString *)figureName;
- (NSString *)stringOfSize; //返回表示图形大小的字符串
- (NSString *)description; //当前图形的位置,大小。返回表示图形面积的字符串

@end



#import "Figure.h"
#import <Foundation/NSString.h>

@implementation Figure

@synthesize location;
- (void)setSize:(NSSize)newsize { /* virtual */ }
- (double)area { return 0.0; } /* virtual */
- (NSString *)figureName { return nil; } /* virtual */
- (NSString *)stringOfSize { return nil; } /* virtual */

- (NSString *)description {
    NSPoint loc = self.location;
    return [NSString stringWithFormat:@"%@: location=(%.2f, %.2f), %@, area=%.2f", [self figureName], loc.x, loc.y, [self stringOfSize], [self area]];
}

@end


//
//  Circle.h
//  Test
//
//  Created by zhen7216 on 2017/3/12.
//  Copyright © 2017年 chenzhen. All rights reserved.
//

#import "Figure.h"

@interface Circle : Figure {
    double radius;
}

@end


//
//  Circle.m
//  Test
//
//  Created by zhen7216 on 2017/3/12.
//  Copyright © 2017年 chenzhen. All rights reserved.
//

#import "Circle.h"
#import <Foundation/NSString.h>
#import <math.h>

#define PI 3.14159

@implementation Circle

- (void)setSize:(NSSize)newsize {
    double x = newsize.width;
    double y = newsize.height;
    radius = sqrt(x * x + y * y);
}

- (double)area {
    return radius * radius * PI;
}

- (NSString *)figureName {
    return @"Circle";
}

- (NSString *)stringOfSize {
    return [NSString stringWithFormat:@"radius=%.2f", radius];
}

@end


#import "Figure.h"

@interface Rectangle : Figure {
    NSSize size;
}

@end


#import "Rectangle.h"
#import <Foundation/NSString.h>

@implementation Rectangle

- (void)setSize:(NSSize)newsize { size = newsize; }

- (double)area { return size.width * size.height; }

- (NSString *)figureName {
    return (size.width == size.height) ? @"Square" : @"Rectangle";
}

- (NSString *)stringOfSize {
    return [NSString stringWithFormat:@"size=%.2f x %.2f", size.width, size.height];
}

@end


#import <Foundation/NSString.h>
#import "Figure.h"
#import "Circle.h"
#import "Rectangle.h"
#import <stdio.h>

BOOL testloop(void) {
    Figure *fig = nil;
    double x, y, w, h;
    char buf[64], com;
    
    do {
        printf("Shape (C=Circle, R=Rectangle. Q=Quit) ? ");
        if (scanf("%s", buf) == 0 || (com = buf[0]) == 'Q' || com == 'q')
            return NO;
        switch (com) {
        case 'C': case 'c':
                fig = [[Circle alloc] init];
                break;
        case 'R': case 'r':
                fig = [[Rectangle alloc] init];
                break;
        }
    }while (fig == nil);
    
    printf("Location ?");
    scanf("%lf%lf", &x, &y);
    fig.location = NSMakePoint(x, y);
    printf("Size? ");
    scanf("%lf%lf", &w, &h);
    [fig setSize:NSMakeSize(w, h)];
    printf("%s\n", [[fig description] UTF8String]);
    return YES;
}

int main(void) {
    BOOL flag;
    do {
        @autoreleasepool {
            flag = testloop();
        }
    }while (flag);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值