如何覆盖非班级成员?

时间:2019-04-05 08:01:03

标签: swift

我在尝试覆盖非类成员时遇到问题,但出现错误:“'override'只能在类成员上指定”。

我该如何解决?

override func ^(lhs: Int, rhs: Int) -> Int {
    return Int(pow(Double(lhs), Double(rhs)))
}

我以为可以,但是我没想到会出现错误!

1 个答案:

答案 0 :(得分:0)

您不能仅在文件范围内使用override。它在类范围内使用。

不要使用^符号来创建所需的新功能。以下是原始功能^的说明:

/// Returns the result of performing a bitwise XOR operation on the two given
/// values.
///
/// A bitwise XOR operation, also known as an exclusive OR operation, results
/// in a value that has each bit set to `1` where *one or the other but not
/// both* of its arguments had that bit set to `1`. For example:
///
///     let x: UInt8 = 5          // 0b00000101
///     let y: UInt8 = 14         // 0b00001110
///     let z = x ^ y             // 0b00001011
///     // z == 11
///
/// - Parameters:
///   - lhs: An integer value.
///   - rhs: Another integer value.
public static func ^ (lhs: Int, rhs: Int) -> Int