面试题答案
一键面试1. 明确方法优先级
- 类优先原则:如果一个类实现了多个接口,且这些接口定义了同名默认方法,在类中直接重写该方法。这样类自己的实现会覆盖接口的默认实现,明确了具体行为。例如:
interface InterfaceA {
fun commonMethod() = "From InterfaceA"
}
interface InterfaceB {
fun commonMethod() = "From InterfaceB"
}
class MyClass : InterfaceA, InterfaceB {
override fun commonMethod(): String {
return "Custom implementation in MyClass"
}
}
- 接口层次优先级:通过定义接口层次结构,让更具体的接口的默认方法具有更高优先级。例如,如果有一个基础接口
BaseInterface
和一个扩展接口ExtendedInterface
都有同名默认方法,让ExtendedInterface
的实现覆盖BaseInterface
的实现。
interface BaseInterface {
fun commonMethod() = "From BaseInterface"
}
interface ExtendedInterface : BaseInterface {
override fun commonMethod() = "From ExtendedInterface"
}
class MyOtherClass : ExtendedInterface
2. 使用委托设计模式
- 委托类:创建一个委托类来处理默认方法的逻辑,然后在实现接口的类中委托给这个委托类。这样可以将复杂的默认方法逻辑分离出来,提高代码的可维护性。例如:
class CommonMethodDelegate {
fun commonMethod() = "Delegate implementation"
}
interface InterfaceC {
fun commonMethod()
}
class MyDelegateClass : InterfaceC {
private val delegate = CommonMethodDelegate()
override fun commonMethod() = delegate.commonMethod()
}
3. 接口隔离原则
- 拆分接口:将包含过多功能的接口拆分成多个更细粒度的接口,避免多个接口定义同名默认方法。这样每个接口专注于单一职责,降低冲突的可能性。例如:
interface FileReader {
fun readFile()
}
interface FileWriter {
fun writeFile()
}
class FileManager : FileReader, FileWriter {
override fun readFile() {
// 读取文件逻辑
}
override fun writeFile() {
// 写入文件逻辑
}
}
4. 策略模式
- 定义策略接口和实现:对于同名默认方法的不同行为,定义一个策略接口,并为每种行为创建具体的策略实现类。然后在实现接口的类中根据需要选择合适的策略。例如:
interface CommonMethodStrategy {
fun executeCommonMethod(): String
}
class StrategyA : CommonMethodStrategy {
override fun executeCommonMethod(): String = "Strategy A implementation"
}
class StrategyB : CommonMethodStrategy {
override fun executeCommonMethod(): String = "Strategy B implementation"
}
interface InterfaceD {
fun commonMethod()
}
class MyStrategyClass : InterfaceD {
private val strategy = StrategyA()
override fun commonMethod() = strategy.executeCommonMethod()
}
通过这些设计层面的方法,可以有效解决接口同名默认方法冲突问题,同时提高代码的可读性、可维护性和扩展性。