class CreditCardPayment : PaymentMethod, PaymentType
例如:
interface PaymentMethod {
fun initiatePayment(amount: Double): String
}
interface PaymentType {
val paymentType: String
}
class CreditCardPayment(val cardNumber: String, val cardHolderName: String, val expiryDate: String) : PaymentMethod,
PaymentType {
override fun initiatePayment(amount: Double): String {
// 模拟使用信用卡处理支付
return "Payment of $$amount initiated using Credit Card ending in ${cardNumber.takeLast(4)}."
}
override val paymentType: String = "Credit Card"
}
fun main() {
val paymentMethod = CreditCardPayment("1234 5678 9012 3456", "John Doe", "12/25")
println(paymentMethod.initiatePayment(100.0))
// 输出结果为: Payment of $100.0 initiated using Credit Card ending in 3456.
println("Payment is by ${paymentMethod.paymentType}")
// 输出结果为: Payment is by Credit Card
}
interface DrawingTool {
val color: String
fun draw(shape: String)
fun erase(area: String)
fun getToolInfo(): String
}
你创建了一个类 PenTool, 实现 DrawingTool 接口, 为它的所有成员提供实现:
class PenTool : DrawingTool {
override val color: String = "black"
override fun draw(shape: String) {
println("Drawing $shape using a pen in $color")
}
override fun erase(area: String) {
println("Erasing $area with pen tool")
}
override fun getToolInfo(): String {
return "PenTool(color=$color)"
}
}
你想要创建与 PenTool 类似的类, 保持相同的行为, 只是 color 值不同. 一种方案是创建一个新的类, 接受一个实现了 DrawingTool 接口的对象作为参数, 例如一个 PenTool 类实例. 然后, 在这个类之内, 你可以覆盖 color 属性.
但在这种情况下, 你需要为 DrawingTool 接口的每个成员添加实现:
interface DrawingTool {
val color: String
fun draw(shape: String)
fun erase(area: String)
fun getToolInfo(): String
}
class PenTool : DrawingTool {
override val color: String = "black"
override fun draw(shape: String) {
println("Drawing $shape using a pen in $color")
}
override fun erase(area: String) {
println("Erasing $area with pen tool")
}
override fun getToolInfo(): String {
return "PenTool(color=$color)"
}
}
//sampleStart
class CanvasSession(val tool: DrawingTool) : DrawingTool {
override val color: String = "blue"
override fun draw(shape: String) {
tool.draw(shape)
}
override fun erase(area: String) {
tool.erase(area)
}
override fun getToolInfo(): String {
return tool.getToolInfo()
}
}
//sampleEnd
fun main() {
val pen = PenTool()
val session = CanvasSession(pen)
println("Pen color: ${pen.color}")
// 输出结果为: Pen color: black
println("Session color: ${session.color}")
// 输出结果为: Session color: blue
session.draw("circle")
// 输出结果为: Drawing circle with pen in black
session.erase("top-left corner")
// 输出结果为: Erasing top-left corner with pen tool
println(session.getToolInfo())
// 输出结果为: PenTool(color=black)
}
abstract class // 请在这里编写你的代码
class SmartLight(name: String) : SmartDevice(name) {
override fun turnOn() {
println("$name is now ON.")
}
override fun turnOff() {
println("$name is now OFF.")
}
fun adjustBrightness(level: Int) {
println("Adjusting $name brightness to $level%.")
}
}
class SmartThermostat // 请在这里编写你的代码
fun main() {
val livingRoomLight = SmartLight("Living Room Light")
val bedroomThermostat = SmartThermostat("Bedroom Thermostat")
livingRoomLight.turnOn()
// 输出结果为: Living Room Light is now ON.
livingRoomLight.adjustBrightness(10)
// 输出结果为: Adjusting Living Room Light brightness to 10%.
livingRoomLight.turnOff()
// 输出结果为: Living Room Light is now OFF.
bedroomThermostat.turnOn()
// 输出结果为: Bedroom Thermostat thermostat is now heating.
bedroomThermostat.adjustTemperature(5)
// 输出结果为: Bedroom Thermostat thermostat set to 5°C.
bedroomThermostat.turnOff()
// 输出结果为: Bedroom Thermostat thermostat is now off.
}
abstract class SmartDevice(val name: String) {
abstract fun turnOn()
abstract fun turnOff()
}
class SmartLight(name: String) : SmartDevice(name) {
override fun turnOn() {
println("$name is now ON.")
}
override fun turnOff() {
println("$name is now OFF.")
}
fun adjustBrightness(level: Int) {
println("Adjusting $name brightness to $level%.")
}
}
class SmartThermostat(name: String) : SmartDevice(name) {
override fun turnOn() {
println("$name thermostat is now heating.")
}
override fun turnOff() {
println("$name thermostat is now off.")
}
fun adjustTemperature(temperature: Int) {
println("$name thermostat set to $temperature°C.")
}
}
fun main() {
val livingRoomLight = SmartLight("Living Room Light")
val bedroomThermostat = SmartThermostat("Bedroom Thermostat")
livingRoomLight.turnOn()
// 输出结果为: Living Room Light is now ON.
livingRoomLight.adjustBrightness(10)
// 输出结果为: Adjusting Living Room Light brightness to 10%.
livingRoomLight.turnOff()
// 输出结果为: Living Room Light is now OFF.
bedroomThermostat.turnOn()
// 输出结果为: Bedroom Thermostat thermostat is now heating.
bedroomThermostat.adjustTemperature(5)
// 输出结果为: Bedroom Thermostat thermostat set to 5°C.
bedroomThermostat.turnOff()
// 输出结果为: Bedroom Thermostat thermostat is now off.
}
然后, 创建一个类 Audio, 实现 Media 接口. Audio 类必须在构造器中使用 title 属性, 而且必须有一个额外的属性 composer, 类型 为String. 在这个类中, 实现 play() 函数, 打印输出: "Playing audio: $title, composed by $composer".
提示
你可以在类的头部使用 override 关键字, 在构造器中实现来自接口的属性.
interface // 请在这里编写你的代码
class // 请在这里编写你的代码
fun main() {
val audio = Audio("Symphony No. 5", "Beethoven")
audio.play()
// 输出结果为: Playing audio: Symphony No. 5, composed by Beethoven
}
interface Media {
val title: String
fun play()
}
class Audio(override val title: String, val composer: String) : Media {
override fun play() {
println("Playing audio: $title, composed by $composer")
}
}
fun main() {
val audio = Audio("Symphony No. 5", "Beethoven")
audio.play()
// 输出结果为: Playing audio: Symphony No. 5, composed by Beethoven
}