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\n");
}
 
- (void)anInstanceMethod {
    printf("This is an instance method\n");
    printf("The value of integerInstanceVariable is %d\n", integerInstanceVariable);
}
 
@end

Definitive Discussion

“Defining Classes”

Class definition的更多相关文章

  1. foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'

    错误:foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because ' ...

  2. `UnityEditor.EditorUtility' does not contain a definition for `GetMiniThumbnail'

    I got the following errors with Untiy 4.0f7error CS0117: `UnityEditor.EditorUtility' does not contai ...

  3. 原创: How to build a query based on Definition Updates installed

    In SCCM 2012 R2, you can use following class. Use SMS_CombinedDeviceResources.EPAntivirusSignatureLa ...

  4. Definition of success-成功的定义

    "My definition of success is doing what you love. I fell many people do things because they fee ...

  5. XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

    XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...

  6. TFS Build Definition And Auto Deploy

    一台build machine上一般只有一个build service[对应一个build controller]来serve一个team project collection,但又workaroun ...

  7. multiple definition of `err_sys' 《UNIX环境高级编程》

    本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...

  8. Mysql运行SQL文件 错误Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    问题描述 想从服务器上DOWN下数据库.操作:先把数据库转存为SQL文件,然后在本地利用navicate运行SQL文件,出现错误信息: Incorrect table definition;there ...

  9. Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification version

    Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification ...

  10. 关于解决keil4和mdk共存后51不能使用go to definition Of 'XXXXXX'问题

    自己安装keil4和mdk共存后,(我是先安装的keil 后安装的 MDK),在51单片机工程里不能使用go to definition Of 'XXXXXX'问题, 类似的如图 已经困扰了好长时间, ...

随机推荐

  1. Cplex: MIP Control Callback

    *本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 之前,我们有简单提到Cplex中的MIP Callback Interface,包括了Informational callback, q ...

  2. bash循环for/while/until

    shell流程控制之一:for循环     for VAR in LIST; do         STATEMENT1         ...     done         例:         ...

  3. Monkeyrunner 简介及其环境搭建

    Monkeyrunner是通过坐标.控件ID和控件上的文字操作应用的界面元素,其测试用例是用python写的,这样就弥补了monkey只有简单命令无法执行复杂用例的缺陷.Monkeyrunner采用的 ...

  4. Notepad++ 使用正则表达式查找替换字符串

        最近在改一些别人写的PHP代码,由于之前的代码可维护性比较差,因此工作量比较多,因此想找一些高效的方式改一些有规律的代码. 比如: 我想将一些类似$rowss[MaxGetCash_num], ...

  5. 利用bochs调试Linux 0.11内核

    引导程序调试软件bochs,跟配套的linux0.11内核img下载地址分别是: http://sourceforge.net/projects/bochs/http://www.oldlinux.o ...

  6. xml和pandas结合处理的一个小例子-待完善

    #!/usr/bin/env python3 # -*- coding:utf-8 -*- import pandas import json import xml.etree.ElementTree ...

  7. 【转】Unicode utf8等编码类型的原理

    原文地址http://www.cnblogs.com/daxiong2014/p/4768681.html Unicode utf8等编码类型的原理 1.ASCII码  我们知道,在计算机内部,所有的 ...

  8. 设计模式(八)组合模式 Composite

    组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从 ...

  9. Codeforces963B - Destruction of a Tree

    Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...

  10. UVA11367 Full Tank? 【分层图最短路】

    题目 After going through the receipts from your car trip through Europe this summer, you realised that ...