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的知识后续需要整理!
     
  • 相关阅读:
    2017 Multi-University Training Contest 2.Balala Power!(贪心)
    2017ICPCECIC C.A math problem(高次剩余)
    Atcoder 068E
    51nod 1385 凑数字(贪心+构造)
    cf round #418 div2 D. An overnight dance in discotheque(贪心)
    cf round #418 div2 C. An impassioned circulation of affection(暴力)
    cf round #424 div2 E. Cards Sorting(线段树)
    Atcoder 077E
    hdu 6162 Ch’s gift(树链剖分+主席树)
    Educational Codeforces Round 26 D. Round Subset(dp)
  • 原文地址:https://www.cnblogs.com/rollrock/p/2460610.html
Copyright © 2011-2022 走看看