我刚修改过它。另一个问题是,如果我想从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与协议参与者的冗余一致性。
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 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
}
答案 0 :(得分:4)
您错过了协议中列出的getTime()
函数的实现。另外,你应该在Piazza上发布这样的问题。 :P
[更新以回答重写问题]
BaseParticipant
类已经采用Participant
协议,因此CyclingParticipant
子类不应声明它也采用它,这会导致冗余一致性错误。由于BaseParticipant
已经是Participant
,因此BaseParticipant
的任何子类也都是Participant
。
变化:
class CyclingParticipant: BaseParticipant, Participant
为:
class CyclingParticipant: BaseParticipant
答案 1 :(得分:1)
默认情况下,需要Swift协议中的所有声明方法。
var db = database.GetCollection<BsonDocument>("cars");
FieldDefinition<BsonDocument, string> field = "car.manufacture";
FilterDefinition<BsonDocument> filter = new BsonDocument();
var a = db.DistinctAsync(field, filter).GetAwaiter().GetResult().ToListAsync().GetAwaiter().GetResult();
未实施