gpt4 book ai didi

objective-c - 我可以在子类中实现协议(protocol)的一个功能吗?

转载 作者:行者123 更新时间:2023-11-28 09:58:38 24 4
gpt4 key购买 nike

我刚刚修改了它。另一个问题是,如果我想从 BaseParticipant 继承一个子类,我可以在子类中重新实现 func performEvent 吗?例如:

class CyclingParticipant: BaseParticipant, Participant
{
init(name: String)
{
super.init(name: name, preferredEvent: Event.CYCLING)

}

func performEvent(event: Event, distance: Distance) throws
{

}

}

但是编译器说“CyclingParticipant 与协议(protocol) Participant 的冗余一致性。

class BaseParticipant: Participant
{
var name: String
var preferredEvent: Event
var raceTime: Int
var couldNotFinish: Bool
//var performedEvent: Event
// in swift, the class accepts protocol must impletment all funcs inside protocol

init(name: String, preferredEvent: Event)
{
self.name = name
self.preferredEvent = preferredEvent
self.raceTime = 0
self.couldNotFinish = false
}

func getName() -> String
{
return self.name
}

func getPreferredEvent() -> Event
{
return self.preferredEvent
}

func isDisqualified() -> Bool
{
return self.couldNotFinish
}

func addTime(addtionalRaceTime:Int) -> Void
{
self.raceTime += addtionalRaceTime
}

func setCouldNotFinish() -> Void
{
self.couldNotFinish = true
}
func performEvent(event: Event, distance: Distance) throws -> Int
{
return 1
}
func getTime() throws
{

}

}

协议(protocol)参与者代码:

protocol Participant
{
func getName() -> String
func getPreferredEvent() -> Event
func isDisqualified() -> Bool
func performEvent(event: Event,distance: Distance) throws ->Int
func addTime(addtionalRaceTime: Int)
func setCouldNotFinish()
func getTime() throws

}

最佳答案

您缺少协议(protocol)中列出的 getTime() 函数的实现。另外,您应该在 Piazza 上发布此类问题。 :P

[更新以回答改写的问题]

BaseParticipant 类已经采用了 Participant 协议(protocol),所以 CyclingParticipant 子类不应该声明它也采用它,这会导致冗余一致性错误。因为 BaseParticipant 已经是一个 Participant,所以 BaseParticipant 的任何子类也将是一个 Participant

改变:

class CyclingParticipant: BaseParticipant, Participant

到:

class CyclingParticipant: BaseParticipant

关于objective-c - 我可以在子类中实现协议(protocol)的一个功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com