如何从接口继承类?

时间:2021-03-07 20:13:00

Lets assume I have Class BaseClass in BaseClass.h. I want to create a SubClass and inherit from my BaseClass , as simple as that. BUT I want to make the inheritance in the interface of SubClass.

让我们假设我在BaseClass.h中有Class BaseClass。我想创建一个SubClass并从我的BaseClass继承,就这么简单。但我想在SubClass的接口中进行继承。

 // SubClass.h
//#import "BaseClass.h" -I dont want to make import to the header (Better convention - I think so).
//@class BaseClass; - That will work only for declaring an instance/property.

@interface SubClass : BaseClass{

 }

I also would like to keep the both classes in separate files. Do I have a simple/elegant solution for instance to group my classes in the Xcode project so they can recognize each other.

我也想将这两个类保存在单独的文件中。我是否有一个简单/优雅的解决方案,例如在Xcode项目中对我的类进行分组,以便他们可以相互识别。

3 个解决方案

#1


3  

//#import "BaseClass.h"

Uncomment that line. You must import the header of the superclass in order to make this a subclass of it. I don't see what your objection was to doing that.

取消注释该行。您必须导入超类的标头,以使其成为它的子类。我不明白你的反对意见是什么。

#2


2  

You have to import the superclass, otherwise your subclass has no reference of what to build off. In your subclass.h you should #import "BaseClass.h". You should not have issues with cyclical inclusion because the #import uses header guards to solve this problem.

您必须导入超类,否则您的子类没有引用要构建的内容。在您的subclass.h中,您应该#import“BaseClass.h”。您不应该遇到循环包含问题,因为#import使用标头保护来解决此问题。

#3


1  

There is one file in xcode project which is known as .pch file. In this file you can import the header files. After this no need to import in the other header files as well. But make sure the file which you import is being used in all the files.

xcode项目中有一个文件,称为.pch文件。在此文件中,您可以导入头文件。在此之后,无需导入其他头文件。但请确保您导入的文件正在所有文件中使用。

#1


3  

//#import "BaseClass.h"

Uncomment that line. You must import the header of the superclass in order to make this a subclass of it. I don't see what your objection was to doing that.

取消注释该行。您必须导入超类的标头,以使其成为它的子类。我不明白你的反对意见是什么。

#2


2  

You have to import the superclass, otherwise your subclass has no reference of what to build off. In your subclass.h you should #import "BaseClass.h". You should not have issues with cyclical inclusion because the #import uses header guards to solve this problem.

您必须导入超类,否则您的子类没有引用要构建的内容。在您的subclass.h中,您应该#import“BaseClass.h”。您不应该遇到循环包含问题,因为#import使用标头保护来解决此问题。

#3


1  

There is one file in xcode project which is known as .pch file. In this file you can import the header files. After this no need to import in the other header files as well. But make sure the file which you import is being used in all the files.

xcode项目中有一个文件,称为.pch文件。在此文件中,您可以导入头文件。在此之后,无需导入其他头文件。但请确保您导入的文件正在所有文件中使用。