zoukankan      html  css  js  c++  java
  • objectc 第二个类

    #import <Foundation/Foundation.h>

     

     

    typedefenum

    {

        kCircle,

        kRectangle,

        kOblateSpheroid

    }ShapeType;

     

     

    typedefenum

    {

        kRedColor,

        kGreenColor,

        kBlueColor

    }ShapeColor;

     

    typedefstruct

    {

        int x,y,width,height;

    }ShapeRect;

     

     

    typedefstruct

    {

        ShapeType type;

        ShapeColor fillColor;

        ShapeRect bounds;

    }Shape;

     

     

     

    NSString * colorName(ShapeColor colorName )

    {

        switch(colorName)

        {

            case kRedColor:

                return @"red";

            casekGreenColor:

                return @"green";

            case kBlueColor:

                return @"blue";

        }

        

        return@"no color";

    }

     

     

    @interface Circle:NSObject

    {

        ShapeColor fillColor;

        ShapeRect bounds;

    }

     

    -(void) setFillColor:(ShapeColor) fillColor;

    -(void) SetBounds:(ShapeRect) bounds;

    -(void) draw;

     

    @end

     

    @implementation Circle

    -(void)setFillColor:(ShapeColor) c

    {

        fillColor = c;

    }

     

    -(void) SetBounds:(ShapeRect) b

    {

        bounds = b;

    }

     

    -(void) draw

    {

        NSLog(@"drawing a circle at (%d %d %d %d) in %@ ",

              bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));

     

    }

    @end

     

     

    @interface Rectangel:NSObject

    {

        ShapeColor fillColor;

        ShapeRect bounds;

    }

    -(void) setFillColor:(ShapeColor) fillColor;

    -(void) SetBounds:(ShapeRect) bounds;

    -(void) draw;

     

    @end

     

    @implementation Rectangel

    -(void)setFillColor:(ShapeColor) c

    {

        fillColor = c;

    }

     

    -(void) SetBounds:(ShapeRect) b

    {

        bounds = b;

    }

     

    -(void) draw

    {

        NSLog(@"drawing a Rectangle at (%d %d %d %d) in %@ ",

              bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));

        

    }

    @end

     

     

    void drawShapes(id shapes[],int count)

    {

        for( int i = 0; i < count; ++ i )

        {

            id shape = shapes[i];

            [shape draw];

        }

    }

     

     

    int main(int argc, const char * argv[])

    {

     

        @autoreleasepool {

        

            id shapes[3];

            

            ShapeRect rect0 = {0,0,30,40};

            shapes[0] = [Circle new];

            [shapes[0] SetBounds:rect0];

            [shapes[0] setFillColor:kRedColor];

            

            ShapeRect rect1 = {0,0,30,40};

            shapes[1] = [Rectangel new];

            [shapes[1] SetBounds:rect1];

            [shapes[1] setFillColor:kGreenColor];

            

            

            drawShapes(shapes, 2);

            

        }

        return 0;

    }

     
     
    我用的xcode是4.2的 ,在写到 void drawShapes(id shapes[],int count)这个函数的时候,出现了有关ARC 的错误,具体的解决方式参考了
    http://blog.csdn.net/startexcel/article/details/7215947 中的方法,对单独的文件进行了设置。
    有关arc的知识后续需要整理!
     
  • 相关阅读:
    测试管理工具
    测试用例--zy
    测试计划和测试用例
    测试用例
    软件测试基础
    异步任务 ---- django-celery
    图片验证码接口
    测试作业
    数据库原理
    HTTPS原理
  • 原文地址:https://www.cnblogs.com/rollrock/p/2460610.html
Copyright © 2011-2022 走看看