day10-接口的静态方法的定义和使用

This commit is contained in:
2026-07-27 10:44:44 +08:00
parent b099333123
commit 1eb48c33bf
3 changed files with 44 additions and 0 deletions
@@ -0,0 +1,18 @@
package com.inmind.interface_static03;
/*
接口中的静态方法的使用:
只能通过接口名.静态方法(参数列表)直接调用
*/
public class Demo03 {
public static void main(String[] args) {
//调用静态方法(接口名.静态方法(参数列表)直接调用)
MyInterface.method1();
MyInterface.method2();
//能不能使用对象来强制调用???绝对不行,只能通过定义静态方法的接口调用!!!!
/*MyInterfaceImpl myInterface = new MyInterfaceImpl();
myInterface.method1();
MyInterfaceImpl.method1();*/
}
}
@@ -0,0 +1,20 @@
package com.inmind.interface_static03;
/*
jdk8接口中添加了静态方法
静态方法:封装接口相关的通用的功能,提供辅助工具的方法。
接口的静态方法定义:
public static void method(){
};
*/
public interface MyInterface {
//静态方法
public static void method1(){
System.out.println("接口中的静态方法method1");
}
static void method2(){
System.out.println("接口中的静态方法method2");
}
}
@@ -0,0 +1,6 @@
package com.inmind.interface_static03;
public class MyInterfaceImpl implements MyInterface{
//静态方法能重写吗?不能
}