Obj-C"@interface"的类文件? [英] Class files of Obj-C "@interface"?

查看:69
本文介绍了Obj-C"@interface"的类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
.h和.m文件中@interface定义之间的区别

Possible Duplicate:
Difference between @interface definition in .h and .m file

Obj C类文件有两个文件.h和.m,其中.h保存接口定义(@interface),而.m保存其实现(@implementation)

Obj C class files have two files .h and .m ,in which the .h holds interface definition (@interface) and .m holds its implementation (@implementation)

但是我看到在某些类中,.h和.m都存在一个@interface?

But i saw in some classes there is an @interface occurring in both .h and .m?

两个文件中都需要@interface吗?是否有任何特定原因?这样做的好处是什么?

What is the need for the @interface in both files?Is there any specific reason to do so?And what are the advantages if did so?

推荐答案

.h文件中的@interface通常是公共接口,这是您要声明继承的接口,例如

The @interface in the .h file is generally the public interface, this is the one that you would declare the inheritance in such as

   @interface theMainInterface : NSObject

请注意冒号:,然后是此@interfaceNSObject继承的超级对象,我确实相信只能在.h文件中完成.您还可以使用诸如

之类的类别声明@interface

note the colons : and then the super object that this @interface is inheriting from NSObject, I do believe that this can only be done in the .h file. You can also declare the @interface with a category as well such as

   @interface theMainInterface(MyNewCatergory)

因此,这意味着您可以在一个.h文件中包含多个@interface,例如

So this means that you can have multiple @interfaces in one .h file like

   @interface theMainInterface : NSObject

        // What ever you want to declare can go in here. 

   @end

   @interface theMainInterface(MyNewCategory)

        // Lets declare some more in here.

   @end

在.h文件中声明这些类型的@interface通常会使所有在其中声明的内容都是公开的.

Declaring these types of @interfaces in the .h file generally makes everything declared in them public.

但是您可以在.m文件中声明私有@interface,这将做以下三件事之一:它将私有扩展所选的@interface或将新的类别添加到所选的@interface或声明新的私有@interface

But you can declare private @interfaces in the .m file which will do one of three things it will privately extend the selected @interface or add a new category to a selected @interface or declare a new private @interface

您可以通过向.m文件中添加类似内容来实现此目的.

You can do this by adding something like this to the .m file.

  @interface theMainInterface()
      // This is used to extend theMainInterface that we set in the .h file.
      // This will use the same @implemenation
  @end

  @implemenation theMainInterface()
      // The main implementation.
  @end

  @interface theMainInterface(SomeOtherNewCategory)
      // This adds a new category to theMainInterface we will need another @implementation for this.
  @end 

  @implementation theMainInterface(SomeOtherNewCategory)
      // This is a private category added the .m file
  @end

  @interface theSecondInterface()
      // This is a whole new @interface that we have created, this is private
  @end

  @implementation theSecondInterface()
     // The private implementation to theSecondInterface
  @end

所有这些都以相同的方式工作,唯一的区别是有些是private,有些是public,有些是categories

These all work the same way, the only difference is that some are private, some are public and some have categories

我不确定是否可以继承.m文件中的@interface.

I am unsure if you can inherit on an @interface in the .m file.

希望这会有所帮助.

这篇关于Obj-C"@interface"的类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆