類 Abstract 類和介面方法優先順序
類中的實現(包括抽象宣告)優先於所有介面預設值。
- 抽象類方法優先於介面預設方法 。
public interface Swim {
default void backStroke() {
System.out.println("Swim.backStroke");
}
}
public abstract class AbstractSwimmer implements Swim {
public void backStroke() {
System.out.println("AbstractSwimmer.backStroke");
}
}
public class FooSwimmer extends AbstractSwimmer {
}
以下宣告
new FooSwimmer().backStroke();
會生產
AbstractSwimmer.backStroke
- 類方法優先於介面預設方法
public interface Swim {
default void backStroke() {
System.out.println("Swim.backStroke");
}
}
public abstract class AbstractSwimmer implements Swim {
}
public class FooSwimmer extends AbstractSwimmer {
public void backStroke() {
System.out.println("FooSwimmer.backStroke");
}
}
以下宣告
new FooSwimmer().backStroke();
會生產
FooSwimmer.backStroke