OC视频教程29课-第02讲 1、2 Objective-C 类的使用

来源:互联网 发布:游族网络董事长 编辑:程序博客网 时间:2024/05/22 08:22

02 12 Objective-C类的使用

声明部分:

    #import <Foundation/Foundation.h>    //foundation |faʊnˈdeɪʃn| noun地基、基础

    @interface Circle : NSObject

    {

        int radius;

        int x;

        int y;

    }

    //circle |ˈsɜːkl| noun


    @property (nonatomic) int radius,x,y;

    //radius |ˈreɪdɪəs| noun半径


    -(void)print;

    @end


实现部分:

    #import “Circle.h”


    @implementation Circle


    @synthesize radius; // synthesize |ˈsɪnθəsaɪz| v合成、综合


    - (void)print

    {

        NSLog(@“radius:%d”,radius);

    }


    @end




Implementation Section

    General Format

        @implementation className

        Synthesized Property

        methodDefinition

        methodDefinition

        ….

        @end

    Unless the methods for a category are being implemented. all the methods declared in the interface section must be defined in the implementation section.

    If one or more protocols were listed in the interface section,all the protocols’ methods must be defined.


Method Definition

    General Format

        myType (returnType) name1:(type1)param1 name2:(type2)param2…

        {

            variableDeclarations

            …

            Return expression;

        } //declare |dɪˈkleə(r)| v 宣告、宣布、声明;declaration |ˌdekləˈreɪʃn| n



    This method declaration must be consistent with the corresponding method declaration from the interface section or from a previously defined protocol definition.

        consistent |kənˈsɪstənt| adj一贯的、始终如一的;correspond |ˌkɒrɪˈspɒnd, American ˌkɔːr-| v 相符、相当。corresponding相关的;


    An instance method can reference the class’s instance variables and any variables it has inherited directedby name.If a class method is being defined, it cannot reference any instance variables.

        inherit |ɪnˈherɪt| verb继承

    The identifier self can be used inside a method to reference the object on which the method was invoked.

0 0