【问题标题】:Protocol subclass implementation协议子类实现
【发布时间】:2015-05-25 22:44:03
【问题描述】:

我有以下代码试图强制抽象(抽象类/一致性):

播放器协议:

protocol PlayerProtocol {
    func play();
    func stop();
    func pause();

    func getVolume() -> UInt32;
    func setVolume(level: UInt32);

    func isPaused() -> Bool;
    func isStopped() -> Bool;

    func onSessionResume();
    func onSessionInterupt();
}

基本播放器:

class BasicPlayer : PlayerProtocol {
    //some variables here..

    init() {
       //init some variables here..
    }

    func play() {
        fatalError("play() - pure virtual function called.");
    }

    func stop() {
        fatalError("stop() - pure virtual function called.");
    }

    func pause() {
        fatalError("stop() - pure virtual function called.");
    }

    func getVolume() -> UInt32 {
        fatalError("getVolume() - pure virtual function called.");
    }

    func setVolume(level: UInt32) {
        fatalError("setVolume() - pure virtual function called.");
    }

    func isPaused() -> Bool {
        fatalError("isPaused() - pure virtual function called.");
    }

    func isStopped() -> Bool {
        fatalError("isStopped() - pure virtual function called.");
    }

    func onSessionInterupt() {
        fatalError("onSessionInterupt() - pure virtual function called.");
    }

    func onSessionResume() {
        fatalError("onSessionResume() - pure virtual function called.");
    }
}

音频播放器:

class AudioPlayer : BasicPlayer, PlayerProtocol {
    private var device: COpaquePointer = nil;
    private var context: COpaquePointer = nil;
    private var source: ALuint = 0;
    private var buffer: ALuint = 0;
    private var interrupted: Bool = false;
    private var volume: Float = 50;


    override init() {
        super.init();
        //..
    }

    deinit {
        //..
    }

    override func play() {
        //..
    }

    override func stop() {
        //..
    }

    override func pause() {
        //..
    }

    override func setVolume(volume: UInt32) {
        //..
    }

    override func getVolume() -> UInt32 {
        //..
    }

    func isPlaying() -> Bool {
        //..
    }

    override func isPaused() -> Bool {
        //..
    }

    override func isStopped() -> Bool {
        //..
    }

    func isAudioPlaying() -> Bool {
        return AVAudioSession.sharedInstance().otherAudioPlaying;
    }

    override func onSessionInterupt() {
        self.pause();
    }

    override func onSessionResume() {
        self.play();
    }

    func setData(buffer: ALuint, source: ALuint) {
        self.buffer = buffer;
        self.source = source;
    }
}

但即使我指定AudioPlayer 实现PlayerProtocol,它也不会强制我实现所有成员函数,如playstop 等。我可以删除它们,但它没有抱怨。可能是因为超类实现了它,但我不知道如何让它在超类中不实现,而让派生类来实现。

基本上,BasicPlayer 应该是抽象的,任何继承它的类都必须实现“某些”成员(不是全部)。 OnSessionInterrupt 未在派生类中实现。我需要它来出错。

我该怎么做?如何在编译时对派生类中未实现的成员而不是抽象类使其出错?

【问题讨论】:

    标签: swift inheritance abstract-class abstract


    【解决方案1】:

    AudioPlayer 是已经符合PlayerProtocol 的类的子类。因为您已经实现了超类中的所有方法,所以这些实现在您的子类中可用,因此您不必重新声明它们。

    在我看来,您在概念上以两种不同的方式抽象您的接口:通过协议和通过抽象超类。这可能是多余的?一个或另一个应该能够满足您的目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 2023-03-23
      相关资源
      最近更新 更多