package Demo15
;
public class DemoInterface {
public static void main(String
[] args
) {
MyInterfaceAbstractImplinl impl
= new MyInterfaceAbstractImplinl();
impl
.methodAbs();
impl
.methodAbs2();
}
}
package Demo15
;
public class MyInterfaceAbstractImplinl implements MyInterfaceAbstract {
@Override
public void methodAbs() {
System
.out
.println("这是第一个方法");
}
@Override
public void methodAbs2() {
System
.out
.println("这是二个方法");
}
@Override
public void methodAbs3() {
System
.out
.println("这是三个方法");
}
@Override
public void methodAbs4() {
System
.out
.println("这是四个方法");
}
}
package Demo15
;
public interface MyInterfaceAbstract {
public abstract void methodAbs();
abstract void methodAbs2();
public void methodAbs3();
void methodAbs4();
}
package Demo15
;
public interface MyInterfaceDefault {
public abstract void methodAbs();
public default void methodDefault(){
System
.out
.println("这是新添加的默认方法");
}
}
package Demo15
;
public class MyInterfaceDefaultA implements MyInterfaceDefault {
@Override
public void methodAbs() {
System
.out
.println("实现了抽象方法, AAA");
}
}
package Demo15
;
public class DemoInterface01 {
public static void main(String
[] args
) {
MyInterfaceDefaultA a
= new MyInterfaceDefaultA();
a
.methodAbs();
a
.methodDefault();
System
.out
.println("===================");
MyInterfaceDefaultB b
= new MyInterfaceDefaultB();
b
.methodAbs();
b
.methodDefault();
}
}
package Demo15
;
public class MyInterfaceDefaultB implements MyInterfaceDefault {
@Override
public void methodAbs() {
System
.out
.println("实现了抽象方法, BBB");
}
@Override
public void methodDefault(){
System
.out
.println("实行按类B类覆盖重写了接口的默认方法");
}
}
package Demo15
;
public interface MyInterfaceStaic {
public static void methodStatic(){
System
.out
.println("这是接口的静态方法!");
}
}
package Demo15
;
public class DemoInterface02 {
public static void main(String
[] args
) {
}
}
package Demo15
;
public class MyInterfaceStaticImpl implements MyInterfaceStaic{
public static void main(String
[] args
) {
MyInterfaceStaticImpl impl
= new MyInterfaceStaticImpl();
MyInterfaceStaic
.methodStatic();
}
}
package Demo15
;
java9的特性
public interface MyInterfacePrivateA {
public default void methodDefault1() {
System
.out
.println("默认方法1");
methodCommon();
}
public default void methodDefault2(){
System
.out
.println("默认方法2");
methodCommon();
}
public default void methodCommon(){
System
.out
.println("AAA");
System
.out
.println("BBB");
System
.out
.println("CCC");
}
}
package Demo15
;
public class DemoInterface05 {
}
package Demo15
;
public interface MyInterfaceA {
public abstract void methodA();
public abstract void methodAB();
public default void methodDefault(){
System
.out
.println("默认方法AAA");
}
}
package Demo15
;
public interface MyInterfaceB {
public abstract void methodB();
public abstract void methodAB();
public default void methodDefault(){
System
.out
.println("默认方法BBB");
}
}
package Demo15
;
public class MyInterfaceeImpl implements MyInterfaceA,MyInterfaceB
{
@Override
public void methodA
() {
System.out.println
("覆盖重写了A方法!");
}
@Override
public void methodAB
() {
System.out.println
("覆盖重写了AB接口都有的抽象方法");
}
@Override
public void methodB
() {
System.out.println
("覆盖重写了B方法!");
}
@Override
public void methodDefault
() {
System.out.println
("对多个接口当中冲突的默认方法进行了覆盖重写");
}
}
package Demo15
;
public class Fu
{
public void method
(){
System.out.println
("父类方法");
}
}
package Demo15
;
//这里父类和接口中有同样的方法,优先父类,所以不用重写
public class Zi extends Fu implements MyInterface
{
}
package Demo15
;
public interface MyInterface
{
public default void method
(){
System.out.println
("接口的默认方法");
}
}
package Demo15
;
/*
1、类与类之间是单继承的,直接父类只有一个。
2、类与接口之间是多实现的,一个类可以实现多个接口。
3、接口与接口之间是多继承的。
注意事项:
1、多个父接口当中的抽象方法如果重复,没关系
2、多个父接口当中的默认方法如果重复,那么子接口必须进行默认方法的重写,而且必须带着default关键字
*/
public class DemoRelations
{
}
package Demo15
;
public interface MyInterfaceA01
{
public abstract void methodA
();
public abstract void methodCommon
();
public default void method
(){
System.out.println
("默认AAA");
}
}
package Demo15
;
public interface MyInterfaceB01
{
public abstract void methodB
();
public abstract void methodCommon
();
public default void method
(){
System.out.println
("默认AAA");
}
}
package Demo15
;
//接口与接口之间是多继承的。
public interface MyyInterfaceAB extends MyInterfaceA01,MyInterfaceB01
{
public abstract void method
();
}
package Demo15
;
public interface MyInterfaceImplement extends MyInterfaceA01, MyInterfaceB01
{
@Override
default void methodA
() {
}
@Override
default void methodB
() {
}
//两个接口中的default有同样的默认方法,所以这里必须带着default
@Override
default void methodCommon
() {
}
@Override
default void method
() {
}
}