day10--接口的静态方法的定义和使用
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.inmind.interface_static03;
|
||||
/*
|
||||
jdk8提供的接口中的静态方法
|
||||
接口中的静态方法的作用:封装接口相关的通用的功能,给相关实现类作为辅助工具的方法
|
||||
|
||||
类中的静态:只跟类有关,跟对象无关
|
||||
类中的静态调用:
|
||||
1.对象名.静态内容(不推荐)
|
||||
2.类名.静态内容(推荐)
|
||||
|
||||
接口的静态方法
|
||||
1.是否通过对象调用静态方法的形式来操作???不能
|
||||
2.只能通过接口名.静态方法来调用
|
||||
接口的静态方法不能通过实现类对象调用或操作,由于接口的多继承导致
|
||||
*/
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
//接口名.静态方法来调用
|
||||
MyInterface.method();
|
||||
//实现类对象调用或操作接口的静态方法(不可以)
|
||||
/*MyInterfaceImpl impl = new MyInterfaceImpl();
|
||||
impl.method();*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.inmind.interface_static03;
|
||||
/*
|
||||
定义静态方法格式
|
||||
public static 返回值类型 方法名(参数列表){
|
||||
方法体
|
||||
};
|
||||
|
||||
*/
|
||||
public interface MyInterface {
|
||||
//接口中静态方法
|
||||
public static void method(){
|
||||
System.out.println("接口MyInterface的静态方法method");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.inmind.interface_static03;
|
||||
|
||||
public class MyInterfaceImpl implements MyInterface{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user