如何在 Kotlin 中为具有默认值 true 或 false 或 null 的变量传递值
How to pass value for a variable with default value true or false or null in Kotlin
我已经在 Kotlin 中为“isOlListingEnabled”的不同值编写了以下代码,如 True、False 和 null。我在 PR 上收到的评论是“我们可以在方法 getFlightsOpinionLabModel 中传递 isOlListingEnabled 的值,默认值为 true 或 false 或 null,而不是创建新方法”。有人可以帮我编写下面的代码,以便我可以用相同的方法处理所有三个(真、假和空)值。
package com.abc.api.flights.unit.fixture
import com.abc.api.flights.models.domain.TripType
import com.abc.api.flights.models.ui.opinionlab.FlightsOpinionLabModel
import java.util.UUID
object FlightsOpinionLabModelFixture {
fun getFlightsOpinionLabModel(tripType: TripType): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = true
)
fun getFlightsOpinionLabModelWithoutListingEnabled(tripType: TripType): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = false
)
fun getFlightsOpinionLabModelWitListingEnabledNull(tripType: TripType): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = null
)
}
你的FlightsOpinionLabModelFixture
可以这样简化:
object FlightsOpinionLabModelFixture {
fun getFlightsOpinionLabModel(tripType: TripType, isOlListingEnabled: Boolean? = true): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = isOlListingEnabled
)
}
调用示例:
// FlightsOpinionLabModelFixture.getFlightsOpinionLabModelWithoutListingEnabled(type)
FlightsOpinionLabModelFixture.getFlightsOpinionLabModel(type, isOlListingEnabled = false)
我已经在 Kotlin 中为“isOlListingEnabled”的不同值编写了以下代码,如 True、False 和 null。我在 PR 上收到的评论是“我们可以在方法 getFlightsOpinionLabModel 中传递 isOlListingEnabled 的值,默认值为 true 或 false 或 null,而不是创建新方法”。有人可以帮我编写下面的代码,以便我可以用相同的方法处理所有三个(真、假和空)值。
package com.abc.api.flights.unit.fixture
import com.abc.api.flights.models.domain.TripType
import com.abc.api.flights.models.ui.opinionlab.FlightsOpinionLabModel
import java.util.UUID
object FlightsOpinionLabModelFixture {
fun getFlightsOpinionLabModel(tripType: TripType): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = true
)
fun getFlightsOpinionLabModelWithoutListingEnabled(tripType: TripType): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = false
)
fun getFlightsOpinionLabModelWitListingEnabledNull(tripType: TripType): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = null
)
}
你的FlightsOpinionLabModelFixture
可以这样简化:
object FlightsOpinionLabModelFixture {
fun getFlightsOpinionLabModel(tripType: TripType, isOlListingEnabled: Boolean? = true): FlightsOpinionLabModel = FlightsOpinionLabModel(
siteName = "www.abc.com",
langId = 1033,
duaid = UUID.randomUUID(),
expId = null,
tripType = tripType,
isOlListingEnabled = isOlListingEnabled
)
}
调用示例:
// FlightsOpinionLabModelFixture.getFlightsOpinionLabModelWithoutListingEnabled(type)
FlightsOpinionLabModelFixture.getFlightsOpinionLabModel(type, isOlListingEnabled = false)