day10-接口的静态方法的定义和使用
This commit is contained in:
25
day10/src/com/inmind/interface_static03/Demo03.java
Normal file
25
day10/src/com/inmind/interface_static03/Demo03.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.inmind.interface_static03;
|
||||
/*
|
||||
jdk8提供的接口中的静态方法
|
||||
接口中的静态方法的作用:封装接口相关的通用的功能,给相关实现类作为辅助工具的方法
|
||||
|
||||
类中的静态:只跟类有关,跟对象无关
|
||||
类中的静态调用:
|
||||
1.对象名.静态内容(不推荐)
|
||||
2.类名.静态内容(推荐)
|
||||
|
||||
接口的静态方法
|
||||
1.是否通过对象调用静态方法的形式来操作???不能
|
||||
2.只能通过接口名.静态方法来调用
|
||||
接口的静态方法不能通过实现类对象调用或操作,由于接口的多继承(接口与接口之间的关系)导致
|
||||
*/
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
//接口中静态方法的调用---正确的做法
|
||||
MyInterface.staticMethod();
|
||||
|
||||
//使用接口的实现类来调用接口的静态方法-------错误做法,与之前的类中静态操作不同
|
||||
/*MyInterfaceImpl myInterface = new MyInterfaceImpl();
|
||||
myInterface.staticMethod();*/
|
||||
}
|
||||
}
|
||||
15
day10/src/com/inmind/interface_static03/MyInterface.java
Normal file
15
day10/src/com/inmind/interface_static03/MyInterface.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.inmind.interface_static03;
|
||||
/*
|
||||
定义静态方法格式
|
||||
public static 返回值类型 方法名(参数列表){
|
||||
方法体
|
||||
};
|
||||
|
||||
*/
|
||||
public interface MyInterface {
|
||||
// void method();//抽象方法
|
||||
//静态方法
|
||||
public static void staticMethod(){
|
||||
System.out.println("接口中的静态方法");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.inmind.interface_static03;
|
||||
|
||||
public class MyInterfaceImpl implements MyInterface{
|
||||
//重写快捷键:ctrl+o
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user