我正在尝试扩展一个我可以选择传入类型的类。然而,更多时候这将是类型,int尽管在某些情况下这可能会改变,所以我想让它动态化。
但是,我想让它默认为整数类型,并且由于它扩展了另一个需要该类型的类,我不知道如何处理它。
我有一个 BaseEntity 设置,以便在创建/更新记录时允许填充 CreatedAt 和 UpdatedAt 列,这扩展了 JSON API 包中的 Identifiable 类。
class BaseEntity : Identifiable {
...
}
如果 id 的类型与指定的原始类型不同,则该类Identifiable可选地允许您将类型参数传递给扩展。
class BaseEntity : Identifiable<Guid> {
...
}
我想知道,如何扩展此类BaseEntity并可选择提供一个类型并将其传递给 Identifiable。这是可以实现的还是我每次都需要提供它。
class BaseEntity<T> : Identifiable<T> {
// <T> Should default to int if possible.
}
class AnotherEntity : BaseEntity<Guid> {
// Allow me to pass in GUID to override the default int type
}
class AnotherEntityAgain : BaseEntity {
// Would default to type <int> if nothing specified.
}