day10-接口interface-静态方法的使用
This commit is contained in:
24
day10/src/com/inmind/interface_static03/Demo03.java
Normal file
24
day10/src/com/inmind/interface_static03/Demo03.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.interface_static03;
|
||||||
|
/*
|
||||||
|
jdk8接口中添加了静态方法
|
||||||
|
静态方法:封装接口相关的通用的功能,提供辅助工具的方法。
|
||||||
|
|
||||||
|
接口的静态方法定义:
|
||||||
|
public static void method(){
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
------------------------------------------------
|
||||||
|
接口中的静态方法的使用:
|
||||||
|
只能通过接口名.静态方法(参数列表)直接调用
|
||||||
|
*/
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*MyInterfaceStaticImpl impl = new MyInterfaceStaticImpl();
|
||||||
|
impl.method();//错误,接口的静态方法不能通过实现类对象来调用或操作,由于接口的多实现导致的
|
||||||
|
*/
|
||||||
|
|
||||||
|
MyInterfaceStatic1.method();//只能通过接口名.静态方法(参数列表)直接调用
|
||||||
|
MyInterfaceStatic2.method();//只能通过接口名.静态方法(参数列表)直接调用
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.interface_static03;
|
||||||
|
|
||||||
|
public interface MyInterfaceStatic1 {
|
||||||
|
|
||||||
|
public static void method(){
|
||||||
|
System.out.println("接口1中的静态方法");
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.interface_static03;
|
||||||
|
|
||||||
|
public interface MyInterfaceStatic2 {
|
||||||
|
|
||||||
|
public static void method(){
|
||||||
|
System.out.println("接口2中的静态方法");
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.inmind.interface_static03;
|
||||||
|
|
||||||
|
public class MyInterfaceStaticImpl implements MyInterfaceStatic1,MyInterfaceStatic2{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user