zoukankan      html  css  js  c++  java
  • 精通ObjectiveC系列 3) 类的申明和实现

    如果学过Java和C#的人,这里容易搞混,

    Objective-C中的@interface关键字是申明类

    @interface Circle : NSObject
    {
        @private
        ShapeColor fillColor;
        ShapeRect bounds;
    }
    
    - (void) setFillColor: (ShapeColor) fillColor;
    - (void) setBounds: (ShapeRect) bounds;
    - (void) draw;
    @end // Circle

    如上,这样的申明类的方式确实让我们Java和C#程序员有点不太习惯,连缩进方式也是

    还是来看一下完整代码吧

    #import <Foundation/Foundation.h>
    
    // --------------------------------------------------
    // constants for the different kinds of shapes and their colors
    
    typedef enum {
        kRedColor,
        kGreenColor,
        kBlueColor
    } ShapeColor;
    
    
    // --------------------------------------------------
    // Shape bounding rectangle
    
    
    typedef struct {
        int x, y, width, height;
    } ShapeRect;
    
    
    // --------------------------------------------------
    // convert from the ShapeColor enum value to a human-readable name
    
    NSString *colorName (ShapeColor color)
    {
        switch (color) {
            case kRedColor:
                return @"red";
                break;
            case kGreenColor:
                return @"green";
                break;
            case kBlueColor:
                return @"blue";
                break;
        }
        
        return @"no clue";
        
    } // colorName
    
    
    // --------------------------------------------------
    // All about Circles
    
    @interface Circle : NSObject
    {
        ShapeColor  fillColor;
        ShapeRect   bounds;
    }
    
    - (void) setFillColor: (ShapeColor) fillColor;
    
    - (void) setBounds: (ShapeRect) bounds;
    
    - (void) draw;
    
    @end // Circle
    
    
    @implementation Circle
    
    - (void) setFillColor: (ShapeColor) c
    {
        fillColor = c;
    } // setFillColor
    
    
    - (void) setBounds: (ShapeRect) b
    {
        bounds = b;
    } // setBounds
    
    
    - (void) draw
    {
        NSLog (@"drawing a circle at (%d %d %d %d) in %@",
               bounds.x, bounds.y, 
               bounds.width, bounds.height,
               colorName(fillColor));
    } // draw
    
    @end // Circle
    
    
    
    
    // --------------------------------------------------
    // All about Rectangles
    
    @interface Rectangle : NSObject
    {
        ShapeColor    fillColor;
        ShapeRect    bounds;
    }
    
    - (void) setFillColor: (ShapeColor) fillColor;
    
    - (void) setBounds: (ShapeRect) bounds;
    
    - (void) draw;
    
    @end // Rectangle
    
    
    @implementation Rectangle
    
    - (void) setFillColor: (ShapeColor) c
    {
        fillColor = c;
    } // setFillColor
    
    
    - (void) setBounds: (ShapeRect) b
    {
        bounds = b;
    } // setBounds
    
    
    - (void) draw
    {
        NSLog (@"drawing a rectangle at (%d %d %d %d) in %@",
               bounds.x, bounds.y, 
               bounds.width, bounds.height,
               colorName(fillColor));
    } // draw
    
    @end // Rectangle
    
    
    // --------------------------------------------------
    // All about OblateSphereoids
    
    @interface OblateSphereoid : NSObject
    {
        ShapeColor    fillColor;
        ShapeRect    bounds;
    }
    
    - (void) setFillColor: (ShapeColor) fillColor;
    
    - (void) setBounds: (ShapeRect) bounds;
    
    - (void) draw;
    
    @end // OblateSphereoid
    
    
    @implementation OblateSphereoid
    
    - (void) setFillColor: (ShapeColor) c
    {
        fillColor = c;
    } // setFillColor
    
    
    - (void) setBounds: (ShapeRect) b
    {
        bounds = b;
    } // setBounds
    
    
    - (void) draw
    {
        NSLog (@"drawing an egg at (%d %d %d %d) in %@",
               bounds.x, bounds.y, 
               bounds.width, bounds.height,
               colorName(fillColor));
    } // draw
    
    @end // OblateSphereoid
    
    
    
    // --------------------------------------------------
    // Draw the shapes
    
    void drawShapes (id shapes[], int count)
    {
        for (int i = 0; i < count; i++) {
            id shape = shapes[i];
            [shape draw];
        }
        
    } // drawShapes
    
    
    
    // --------------------------------------------------
    // The main function.  Make the shapes and draw them
    
    int main (int argc, const char * argv[]) 
    {
        id shapes[3];
        
        ShapeRect rect0 = { 0, 0, 10, 30 };
        shapes[0] = [Circle new];
        [shapes[0] setBounds: rect0];
        [shapes[0] setFillColor: kRedColor];
        
        ShapeRect rect1 = { 30, 40, 50, 60 };
        shapes[1] = [Rectangle new];
        [shapes[1] setBounds: rect1];
        [shapes[1] setFillColor: kGreenColor];
        
        ShapeRect rect2 = { 15, 19, 37, 29 };
        shapes[2] = [OblateSphereoid new];
        [shapes[2] setBounds: rect2];
        [shapes[2] setFillColor: kBlueColor];
        
        drawShapes (shapes, 3);
        
        return (0);
        
    } // main

    运行结果:

    drawing a circle at (0 0 10 30) in red
    drawing a rectangle at (30 40 50 60) in green
    drawing an egg at (15 19 37 29) in blue
    drawing a triangle at (47 32 80 50) in red

    技术改变世界
  • 相关阅读:
    剑指offer-二维数组中的查找
    TF-IDF(term frequency–inverse document frequency)
    Java实现中文字符串的排序功能
    当前课程
    【R】资源整理
    CentOS相关
    【转】Setting up SDL Extension Libraries on MinGW
    【转】Setting up SDL Extension Libraries on Visual Studio 2010 Ultimate
    【转】Setting up SDL Extension Libraries on Code::Blocks 12.11
    【转】Setting up SDL Extension Libraries on Visual Studio 2019 Community
  • 原文地址:https://www.cnblogs.com/davidgu/p/2918225.html
Copyright © 2011-2022 走看看