fun membershipSelected(membershipSelected: List<MemberShipType>) = apply { this.membershipSelected= membershipSelected}
object GOLD: MemberShipType("Gold"), IRateOption{}
object BRONZE: MemberShipType("Bronze"){}
在Kotlin中,如何限制membershipSelected
仅是实现MembershipTypes
接口的IRateOption
?
例如,古铜色没有实现IRateOption
,但是GOLD
却实现了GOLD
,因此可以选择membershipType
答案 0 :(得分:1)
您需要使用带有Sub Main()
Call ColorCodeTextInCell(ActiveSheet, "[B]", RGB(20,100,255))
End Sub
Function ColorCodeTextInCell(WS As Worksheet, sText As String, Optional iColor As Long = vbRed)
Dim bNewInstanceFound As Boolean
Dim iStart As Integer
Dim cell As Range
bNewInstanceFound = True
For Each cell In WS.UsedRange.Cells
iStart = 1
Do While bNewInstanceFound = True
Set cell = WS.Range("A1")
iStart = InStr(iStart, cell.Value, sText)
If iStart > 0 Then
cell.Characters(iStart, Len(sText)).Font.Color = iColor
iStart = iStart + Len(sText)
Else
bNewInstanceFound = False
End If
Loop
Next cell
End Function
子句的泛型,例如documented in the Kotlin manual。对于您的用例,它看起来像这样:
where
或者,使用简写语法:
fun <T> membershipSelected(membershipSelected: List<T>): ReturnType
where T : MembershipType,
T : IRateOption {
// function body
}
编辑:仅当参数中的所有项目均为同一类型时,此解决方案才有效。这意味着您将无法使用两个单独的类/对象,它们是fun <T> membershipSelected(membershipSelected: List<T>) where T : MembershipType, T : IRateOption = // function body
和MembershipType
的子类型。如果您需要这种行为,建议您创建一个开放或抽象类,如下所示:
IRateOption