day10-接口的默认方法的定义和使用
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.inmind.interface_default02;
|
||||
/*
|
||||
JDK8时提供,接口的默认方法
|
||||
|
||||
接口的默认方法的使用步骤:
|
||||
1.接口不能创建对象的,它没有构造方法
|
||||
2.定义一个实现类,实现该接口,并实现接口中的所有的抽象方法
|
||||
public class MyInterfaceImpl(实现类) implements MyInterface(接口){
|
||||
3.创建该接口的实现类的对象,调用接口的抽象方法和默认方法
|
||||
4.在接口的实现类中,可以根据对应的需求,选择性重写,或不重写接口中的默认方法,也可以沿用功能(接口.super.默认方法名)(重点)
|
||||
*/
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//创建接口的实现类的对象1
|
||||
MyInterfaceImpl1 myInterface1 = new MyInterfaceImpl1();
|
||||
myInterface1.method1();
|
||||
myInterface1.method2();
|
||||
myInterface1.method3();
|
||||
//创建接口的实现类的对象2
|
||||
MyInterfaceImpl2 myInterface2 = new MyInterfaceImpl2();
|
||||
myInterface2.method1();
|
||||
myInterface2.method2();
|
||||
myInterface2.method3();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user