Swift 2.0中的抽象类

时间:2015-09-28 11:56:38

标签: java swift

我试图将我在java中的抽象类转换为swift,我不确定我所做的是正确的。我需要建议。

实际代码

public abstract class ProtocolLayer 
{
    private ProtocolLayer mUpperProtocolLayer;
    private ProtocolLayer mLowerProtocolLayer;
    protected byte mVersion;

    public ProtocolLayer ( )
    {
        mUpperProtocolLayer = null;
        mLowerProtocolLayer = null;
        mVersion = 0;
    }

    /**
     * Sets the upper layer of the protocol used to pass the eventual message decoded at this level. Set <i>null</i> for the highest layer.
     * @param pProtocolLayer upper protocol layer to set
     * @see ProtocolLayer#getUpperProtocolLayer()
     */ 
    public void setUpperProtocolLayer ( ProtocolLayer pProtocolLayer )
    {
        mUpperProtocolLayer = pProtocolLayer;
    }

    /**
     * Gets the upper layer of the protocol used to pass the eventual message decoded at this level.
     * @return pProtocolLayer return the upper protocol layer set
     * @see ProtocolLayer#setUpperProtocolLayer(ProtocolLayer)
     */ 
    public ProtocolLayer getUpperProtocolLayer ( )
    {
        return mUpperProtocolLayer;
    }

    /**
     * Sets the lower layer of the protocol used to pass the eventual message encoded at this level. Set <i>null</i> for the lowest layer.
     * @param pProtocolLayer lower protocol layer to set
     * @see ProtocolLayer#getLowerProtocolLayer()
     */ 
    public void setLowerProtocolLayer ( ProtocolLayer pProtocolLayer )
    {
        mLowerProtocolLayer = pProtocolLayer;
    }

    /**
     * Gets the lower layer of the protocol used to pass the eventual message encoded at this level.
     * @return return the lower protocol layer set
     * @see ProtocolLayer#setLowerProtocolLayer(ProtocolLayer)
     */ 
    public ProtocolLayer getLowerProtocolLayer ( )
    {
        return mLowerProtocolLayer;
    }

    /**
     * Gets the protocol layer version of the message received
     * @return version of the protocol
     */ 
    public byte getVersion ()
    {
        return mVersion;
    }

    /**
     * Method to decode the received message
     * @param pDataReceived buffer containing data related to this protocol layer
     * @return message received
     * @see ProtocolLayer#transmitt()
     */ 
    public abstract Object receive ( byte[] pDataReceived );

    /**
     * Method to encode and so transmit a message
     * @return message encoded
     * @see ProtocolLayer#receive(byte[])
     */ 
    public abstract byte[] transmitt ( );

}

我目前在swift中做了什么

protocol ProtocolLayerProtocol: class {


}

public class ProtocolLayer {
    var mUpperProtocolLayer : ProtocolLayer!
    var mLowerProtocolLayer : ProtocolLayer!
    var mVersion : UInt8!

    /**
    * Sets the upper layer of the protocol used to pass the eventual message decoded at this level. Set <i>null</i> for the highest layer.
    * @param pProtocolLayer upper protocol layer to set
    * @see ProtocolLayer#getUpperProtocolLayer()
    */
    func setUpperProtocolLayer(pProtocolLayer: ProtocolLayer!) {
        self.mUpperProtocolLayer = pProtocolLayer
    }

    /**
    * Gets the upper layer of the protocol used to pass the eventual message decoded at this level.
    * @return pProtocolLayer return the upper protocol layer set
    * @see ProtocolLayer#setUpperProtocolLayer(ProtocolLayer)
    */
    func getUpperProtocolLayer() -> ProtocolLayer? {
        return self.mUpperProtocolLayer
    }

    /**
    * Sets the lower layer of the protocol used to pass the eventual message encoded at this level. Set <i>null</i> for the lowest layer.
    * @param pProtocolLayer lower protocol layer to set
    * @see ProtocolLayer#getLowerProtocolLayer()
    */
    func setLowerProtocolLayer(pProtocolLayer : ProtocolLayer!) {
        mLowerProtocolLayer = pProtocolLayer
    }

    /**
    * Gets the lower layer of the protocol used to pass the eventual message encoded at this level.
    * @return return the lower protocol layer set
    * @see ProtocolLayer#setLowerProtocolLayer(ProtocolLayer)
    */
    func getLowerProtocolLayer() -> ProtocolLayer {
        return mLowerProtocolLayer
    }

    /**
    * Gets the protocol layer version of the message received
    * @return version of the protocol
    */
    func getVersion () -> UInt8 {
        return mVersion
    }

    /**
    * Method to decode the received message
    * @param pDataReceived buffer containing data related to this protocol layer
    * @return message received
    * @see ProtocolLayer#transmitt()
    */
    func receive ( pDataReceived : [UInt8] ) -> AnyObject!{
        return nil;
    }

    /**
    * Method to encode and so transmit a message
    * @return message encoded
    * @see ProtocolLayer#receive(byte[])
    */
    func transmitt ( ){}

}
你能帮忙吗?我坚持这个classe。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我找到了答案,只需要使用协议

protocol Layer {

    func receive ( pDataReceived : [UInt8] ) -> AnyObject!
    func transmitt () -> [UInt8]?
}