zoukankan      html  css  js  c++  java
  • Class definition

    Prerequisite Articles

    (None)

    Related Articles

    A class definition is the specification of a class of objects through the use of certain files and syntax. A class definition minimally consists of two parts: a public interface, and a private implementation. You typically split the interface and implementation into two separate files—the header file and the implementation file. By separating the public and private parts of your code, you retain the class interface as an independent entity.

    You usually name the interface and implementation files after the class. Because it’s included in other source files, the name of the interface file usually has the .h extension typical of header files. The name of the implementation file has a .m extension, indicating that it contains Objective-C source code. For example, the MyClass class would be declared in MyClass.h and defined in MyClass.m.

    Interface

    In the interface, you do several things:

    • You name the class and its superclass.

      You may also specify any protocols that your class conforms to (see Protocol).

    • You specify the class’s instance variables.

    • You specify the methods and declared properties (see Declared property) that are available for the class.

    In the interface file, you first import any required frameworks. (This will often be just Cocoa/Cocoa.h.) You start the declaration of the class interface itself with the compiler directive @interface and finish it with the directive @end.

    #import <Cocoa/Cocoa.h>
     
    @interface MyClass : SuperClass {
        int integerInstanceVariable;
    }
    + (void)aClassMethod;
    - (void)anInstanceMethod;
     
    @end

    Implementation

    Whereas you declare a class’s methods in the interface, you define those methods (that is, write the code for implementing them) in the implementation.

    In the interface file, you first import any required header files. (Minimally this will be your class’s header file.) You start the implementation of the class with the compiler directive @implementation and finish it with the directive @end.

    #import "MyClass.h"
     
    @implementation MyClass
     
    + (void)aClassMethod {
        printf("This is a class method
    ");
    }
     
    - (void)anInstanceMethod {
        printf("This is an instance method
    ");
        printf("The value of integerInstanceVariable is %d
    ", integerInstanceVariable);
    }
     
    @end

    Definitive Discussion

    “Defining Classes”
  • 相关阅读:
    C语言编译包含math库加参数-lm
    C语言浮点类型有效位(float, double,long double)
    C语言速记(宏)
    C语言速记6(结构体)
    asp.net Core依赖注入汇总
    跨域请求(转载)
    UnobtrusiveJavaScriptEnabled、ClientValidationEnabled(转载)
    到值类型“System.DateTime”的强制转换失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可以为 null 的类型。
    软件开发PPT中造图片软件:ProcessOn
    EF接收数据通用实体模型
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3296432.html
Copyright © 2011-2022 走看看