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的知识后续需要整理!
     
  • 相关阅读:
    计算机网络中协议相关的问题(转)
    Vs2013打开项目时,一直处理等待状态,并显示“Microsoft Visual Studio正忙”的提示窗,处理方法(转)
    ASP.NET Core 2.2 十九. Action参数的映射与模型绑定(转)
    ASP.NET Core 2.2 十八.各种Filter的内部处理机制及执行顺序(转)
    ASP.NET Core 2.2 : 十七.Action的执行(Endpoint.RequestDelegate后面的故事)(转)
    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案(转)
    【Leetcode】771. Jewels and Stones
    【Leetcode】50. Pow(x, n)
    【Leetcode】 328. Odd Even Linked List
    【leetcode】59.Spiral Matrix II
  • 原文地址:https://www.cnblogs.com/rollrock/p/2460610.html
Copyright © 2011-2022 走看看