- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 objective-C 中我们可以这样做:
一个。在父类(super class)中导入文件
#import "MyAwesomeClass.h"
@interface MySuperViewController : UIViewController
@end
@implementation MySuperViewController
- (void)viewDidLoad {
[super viewDidLoad];
//MyAwesomeClass allocated, initialized, used
MyAwesomeClass *awesomeClass = [MyAwesomeClass new];
}
@end
在父类(super class)中使用导入的文件,在子类中无需重新导入
@interface MySubViewController : MySuperViewController
@end
@implementation MySubViewController
- (void)viewDidLoad {
[super viewDidLoad];
//No compilation error, since MyAwesomeClass already imported in superclass
MyAwesomeClass *awesomeClass = [MyAwesomeClass new];
}
@end
尝试在 swift 中做同样的事情会出现编译错误:
一个。在 MySuperViewController 中导入 UIKit
import UIKit
class MySuperViewController : UIViewController {
@IBOutlet weak var enterPrice: UITextField!
}
在 MySubViewController 中声明和使用 UITextField 对象而不导入 UIKit
class MySubViewController: MySuperViewController {
// compilation error at below line
@IBOutlet weak var myButton: UIButton!
}
有什么方法可以避免在上述情况下重新导入 UIKit 吗?请提出建议。
最佳答案
简答:
是的。据我了解,您需要在项目的每个 Swift 文件 中导入所需的所有框架(这是逐个文件的要求,而不是逐个类的要求。如果您在一个文件中定义 2 个类单个文件,您只需要在文件顶部导入一个。)
C 中的#import/#include 语句是预处理器指令。就好像包含文件中的代码被复制/粘贴到包含的位置。如果您在父类(super class)的 header 中包含一个 header ,则父类(super class)的 header 现在包含扩展的内容。因此,当您在子类中包含父类(super class) header 时,系统框架 header 将作为父类(super class) header 的一部分包含在内。
Swift 的工作方式略有不同。
关于 swift : import UIKit in each subclass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729600/
我想知道在从另一个扩展的类中创建对象的这两种方法之间是否存在任何功能差异。 我只是在处理基本的继承,除了 Cat extends Mammal 之类的东西之外没有什么复杂的。 最佳答案 ParentC
class superClass {} class subClass extends superClass{} public class test { public static void m
我有一个跨很多很多项目的 C# 解决方案。我创建了一个从 Systme.Diagnostics.Tracing.EventSource 继承的跟踪类,称为 MyCustomEventSource,它处
考虑以下示例,其中 Student 继承自 Person: function Person(name) { this.name = name; } Person.prototype.say =
基本上是标题所说的内容,但有一些详细说明。我有一个带有几个子类的父类(super class)。我需要一个 ArrayList 来保存两种类型的子类,因此需要 SuperClass 类型的 Array
我正在阅读 mughal 的 SCJP(第 3 版)(我遇到的最好的 SCJP 书),在第 727 页上,它说了以下内容: class MyIntList extends ArrayList {}
我使用方法 Object process( JobContext jobContext ); 以及名为 JobProcessImpl 的 impl 来定义 JobProcess。每当执行此 JobPr
如果我想编写一个采用可变数量“TDerived”的方法,其中 TDerived 是类“Base”的任何子类,有什么办法可以做到这一点吗? 以下代码仅适用于单个特定的指定子类: void doStuff
有没有人(本地)弄清楚如何对 SKNode 进行子类化以包含通过关节连接的多个物体 - 例如汽车? 似乎没有办法将子类的关节添加到父场景的物理世界属性中。 此外,当尝试编译并运行下面的对象时,即使没有
我正在尝试编写 Node 库的扩展。该库导出一个扩展 EventEmitter 的类(class LibClass extends EventEmitter)。它负责建立一个 websocket 连接
Tensorflow 有一些 docs用于子类化 (tf) Keras Model 和 Layer。 但是,尚不清楚使用哪个“模块”或“ block ”(例如,几个层的集合)。 由于从技术上讲它是几个
我正在尝试对 android.os.AsyncTask 类进行通用子类化。我基本上只想给它添加一个属性。问题是,我仍然希望能够将它用作匿名类。 import android.content.C
是否可以在 swift 中对通用结构进行子类化? 假设我们有一个结构: struct Foo {} 我想要“子类化”它以添加一些功能: struct Something {} struct Bar
class A: pass class B(A): pass ListOfA = List[A] list_of_a : ListOfA = [A(), A()] for e in [
我想通过打印选择器和参数来拦截发送到代理对象的消息。即使代理没有实现它们并且没有目标对象。请帮忙。我已经查看了几个选项和 Apple 文档,但他们假设您已经知道目标对象。我想干净地完成此操作,而不会出
我有一个使用核心数据并有两个目标的项目。这些目标有几个共享实体。目前,我正在维护两个具有重复实体的独立模型,在我看来,这效率不高。 有没有办法将共享数据模型作为具体目标中数据模型的父级? 最佳答案 我
我收到以下错误: Could not find matching constructor for: org.crawler.CrawlerUtils$fetch(org.series.crawler.
在 Knockout 中,我有一个 Setting 实体数组,它们可能具有不同的类型(即设置实体的“子类”)。这些类型是动态的,用户可以在 UI 中更改它们。根据设置实体的类型,它需要不同的对象属性,
我有一个ArrayClass并且mergeSortArray扩展了它。并且 mergeSortArray 包含一个 mergeSort() 方法。但是,由于我使用 super 从父类(super cl
我是 PyTorch 的新手,在使用不同的工具包一段时间后尝试它。 我想了解如何对自定义层和函数进行编程。作为一个简单的测试,我写了这个: class Testme(nn.Module):
我是一名优秀的程序员,十分优秀!