`
ydbc
  • 浏览: 720225 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Object-c基础之三:面对对象开发@interface,@implementation

 
阅读更多

1.@interface

在java等语言编程中,创建类都是用class,但在object-c中,用@interface。

例子:

@interface circle :NSObject //定义名为circle的类,继承自NSObject

{

ShapeColorfillColor;//定义ShapeColor类型的实例变量 fillColor;

Shapebounds;//定义Shape类型的实例变量 bounds;

}

- (void)setFillColor:(ShapeColor)fillColor;//定义名为setFillColor的方法

2.@implementation

在object-c中,@interface定义放在.h文件中,而@implementation放在.m 文件,是.h文件的详细实现。

例子:

@implementation circle //结尾没有分号。

- (void)setFillColor:(ShapeColor)c

{

fillColor= c;//把参数c赋值给@interface中定义的fillColor;

}

3.实例化对象

为了使用我们已经定义的类,我们需要实例化对象,我们可以在我们创建的文件中的main()函数实例化:如下

int main(int argc, const char * argv[])

{

idshapes[3];//定义id类型数组

shapes[0]= [circle new];//实例化circle并赋值给shapes

[shapes[0]setFillColor:kRedColor];//调用方法进行赋值

}

今天就到这里,下次讲一下xcode4.x如何使用。

1.@interface

在java等语言编程中,创建类都是用class,但在object-c中,用@interface。

例子:

@interface circle :NSObject //定义名为circle的类,继承自NSObject

{

ShapeColorfillColor;//定义ShapeColor类型的实例变量 fillColor;

Shapebounds;//定义Shape类型的实例变量 bounds;

}

- (void)setFillColor:(ShapeColor)fillColor;//定义名为setFillColor的方法

2.@implementation

在object-c中,@interface定义放在.h文件中,而@implementation放在.m 文件,是.h文件的详细实现。

例子:

@implementation circle //结尾没有分号。

- (void)setFillColor:(ShapeColor)c

{

fillColor= c;//把参数c赋值给@interface中定义的fillColor;

}

3.实例化对象

为了使用我们已经定义的类,我们需要实例化对象,我们可以在我们创建的文件中的main()函数实例化:如下

int main(int argc, const char * argv[])

{

idshapes[3];//定义id类型数组

shapes[0]= [circle new];//实例化circle并赋值给shapes

[shapes[0]setFillColor:kRedColor];//调用方法进行赋值

}

今天就到这里,下次讲一下xcode4.x如何使用。
分享到:
评论

相关推荐

    Objective-C基础教程 中文 PDF 清晰版 [31M]

    本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中的框架,以及继承、复合、源文件组织等众多重要的面向对象编程技术。...

    object-c基础教程

    o @interface o @implementation o 把它们凑在一起  详细说明... o 多重参数 o 建构子(Constructors) o 访问权限 o Class level access o 异常情况(Exceptions)处理  继承、多型(Inheritance, ...

    delegatedemo

    #import "ViewController.h" @interface ViewController () ...// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testNotifi:) name:@"TestNotifi" object:nil]; }

    应用Dephi 开发佳能照相机API

    interface uses EDSDKType, EDSDKError; const edsdk = 'EDSDK.DLL'; { ****************************************************************************** ********************* Initialize / Terminate ...

    Object-Oriented Software Construction 2nd

    PART C: OBJECT-ORIENTED TECHNIQUES 163 Chapter 7: The static structure: classes 165 7.1 OBJECTS ARE NOT THE SUBJECT 165 7.2 AVOIDING THE STANDARD CONFUSION 166 7.3 THE ROLE OF CLASSES 169 7.4 A ...

    YSObserver:一个轻量级的基于block某些通知的对象KVO扩展

    YSObserver 一个轻量级的通过Block执行通知替代的对象KVO扩展,整个库一共200行代码。 例子 要运行示例项目,请克隆存储库,然后... if ([keyPath isEqualToString:@"frame"]){ NSLog(@"the frame=%@", change[NSKe

    Physically Based Rendering from Theory to Implementation - part1

    Physically Based Rendering from Theory to Implementation 一共3部分~~~~~~~~~, 源码下载to h t t p ://www.p b r t . o r g /downloads.php /////////////////////////////////// CHAPTER 01. ...

    CheckMem.pas

    8) 0000000000F33A5C - 38($0026)字节 - 不是对象 9) 0000000000F33A80 - 42($002A)字节 - 不是对象 把bb:=Tbutton.Create(nil);注释掉://bb:=Tbutton.Create(nil);再重新编译,然后运行,再点button1。出现的...

    来电显示的Delphi开发控件

    开发来电显示的Delphi控件包: 使用方法: (1) 下载控件包Component.zip (2) 先编译 CPortLib6.dpk ,已安装了CPortLib 控件组的可省略这一步骤 (3) 然后编译 TelePort.dpk ,并安装 (4) 安装完成后,在 ...

    Effective C++(第三版)

    differentiate between inheritance of interface and inheritance of implementation. 条款35:考虑virtual函数以外的其他选择 consider alternatives to virtual functions. 条款36:绝不重新定义继承而来的non-...

    [Objective-c程序设计].杨正洪等.扫描版

    《Objective-C程序设计》(作者杨正洪、郑齐心、李建国)通过大量的实例系统地介绍了Objective-C语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读本书后,可以掌握Objective-C语言的基本内容,并进行...

    Software.Application.Development.A.Visual.Cplusplus.MFC.and.STL

    It covers everything from the design to the implementation of all software modules, resulting in a demonstration application prototype which may be used to efficiently represent mathematical ...

    Learning.Spring.Application.Development.1783987367

    Other highlights include learning how to build the Java DAO implementation layer by leveraging the Data Access Object design pattern, securing your applications against malicious intruders, and ...

    中间件课件(yjm)

    Basic concepts in object-oriented ...–Separating interface class from implementation class; –Construction and deconstruction of object; –Extension of interface; –Object lifetime management;

    IGXL平台上OI (operator interface)开发指导书

    This ILO training session focus mainly on developing an understanding of all the IG-XL Production Controls and its object members needed for the implementation of a simple operator interface ...

    servlet2.4doc

    Returns the object bound with the specified name in this session, or null if no object is bound under the name. getAttributeNames() - Method in interface javax.servlet.ServletContext Returns an ...

    JPEG2000 C语言源代码全集

    it affects only the implementation of a few application level objects -- the core system abstracts all such I/O considerations through interface classes which are implemented by applications. ...

    iOS 7开发(英文档)

    if ([managedObjectContext save:&error] == NO) { [ErrorHandler handleError:error fatal:NO]; } Let’s set up the scaffolding for the internal error-handling framework. Start by creating a new single ...

Global site tag (gtag.js) - Google Analytics