使用接口编程,提共所需的方法让类取实现。避免伴随类的生成。降低代码的耦合度
解决接口冲突
当两个接口同时提供了默认方法,必须覆盖这个方法来解决冲突
案例:
public class SameInterface implements Person,Named{
public static void main(String[] args) {
}
@Override
public String getName() {
// TODO Auto-generated method stub
return Named.super.getName();
}
}
//接口一
interface Person{
default String getName() {
return "";
}
}
interface Named{
default String getName() {
return ""+"s";
}
}