Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以得到与“类似的最终结果”;“受保护”;使用C#但使用Swift?_Swift_Multithreading_Protected - Fatal编程技术网

我可以得到与“类似的最终结果”;“受保护”;使用C#但使用Swift?

我可以得到与“类似的最终结果”;“受保护”;使用C#但使用Swift?,swift,multithreading,protected,Swift,Multithreading,Protected,我的目标是编写一个类,其中包含一个函数,该函数包含要在后台线程中运行的代码(例如在run()函数中)。以下是我的课程(以及相关课程)的一些要求: 确保在编写run()函数时,代码保证在后台线程中运行,而程序员不需要了解任何关于线程的知识,也不需要记住调用该run()函数中与线程相关的任何其他代码 有一个在别处设置的完成函数,当后台线程完成其处理时调用该函数,而程序员无需在此run()函数中编写代码来进行调用 基本上是让某人从某个基类派生一个类,然后在run()函数中编写其背景代码,而不编写任何

我的目标是编写一个类,其中包含一个函数,该函数包含要在后台线程中运行的代码(例如在run()函数中)。以下是我的课程(以及相关课程)的一些要求:

  • 确保在编写run()函数时,代码保证在后台线程中运行,而程序员不需要了解任何关于线程的知识,也不需要记住调用该run()函数中与线程相关的任何其他代码
  • 有一个在别处设置的完成函数,当后台线程完成其处理时调用该函数,而程序员无需在此run()函数中编写代码来进行调用
  • 基本上是让某人从某个基类派生一个类,然后在run()函数中编写其背景代码,而不编写任何与管理背景线程、完成函数等相关的内容
下面是我可能用C#在两个不同文件中编写的代码:

class MyBase
{
    public Action completionFunction = NULL;
    public void begin()
    {
        RunInOtherThread( run ); // This function call is just pseudo-code for running that run() function in a background thread.
        if( completionFunction != Null )
            completionFunction();
    }
    virtual protected void run() = 0 // so this class cannot be instantiated
    {
    }
};
在另一个文件中:

class MySpecificClass : MyBase
{
    override void run()
    {
        // Do important stuff here!
    }
};
我不知道如何满足我对Swift的要求。他们忽略了“受保护”级别的保护,原因对我来说毫无意义(比如认为我不能解雇一个程序员,因为他通过公开一个受基类保护的函数来破坏代码)

我对Swift的一些特性还不够熟悉,无法获得这些结果。我不需要让类像C代码一样工作,尽管它对我和我的C程序员来说是一个非常好的解决方案

如何满足我的要求

顺便说一句,我看到过关于这个问题的问答,没有合理的方法来伪造受保护的访问级别。我不需要伪造它,我只需要另一种方法来实现在派生类中隐藏代码的目标,除了基类或类似的东西。我很想使用它,而不是子分类。例如:

public protocol MyBase {
    // *** This requires any object that conforms to the protocol
    // *** to provide these, without specifying what they do
    var completionFunction: (() -> ())? {get set}
    func run() 
}
public extension MyBase {
    // *** This extension provides all objects that conform to the 
    // *** protocol with this function
    func begin() {
        RunInOtherThread( self.run ) // This function call is just pseudo-code for running that run() function in a background thread.
        if let c = self.completionFunction {
            c()
        }
    }
}

public class MySpecificClass: MyBase {
    // *** Now, rather than subclassing, we simply conform to the protocol 
    public var completionFunction: (() -> ())? = nil
    public func run() { 
        // *** No need to `override` - but *must* be supplied
        // Do important stuff here!
    }
}

唯一的问题是run()函数是公共函数,因此使用MySpecificClass的人可能会调用它而不是begin函数。否则,我只能使用public run()函数实现我的类,这是一个很好的例子,说明了除了public run()函数之外,如何在Swift中实现这些类。协议和扩展当然是处理抽象基类问题的好方法。谢谢。@DavidRector您可以尝试给出抽象基类
internal init()
,并在具体子类中将其重写为
override public init()
。这将阻止任何人在类层次结构之外实例化基类。它可以编译,但不能测试它是否在操场上工作。当然,它不会强制您重写
run()
,但是您可以使
run
内部
。。。