day10-接口的抽象方法的定义和使用

This commit is contained in:
2026-07-27 10:00:18 +08:00
parent e10abf4e61
commit beb20cf752
3 changed files with 34 additions and 1 deletions
+14 -1
View File
@@ -1,4 +1,17 @@
package com.inmind.interface01;
/*
接口的抽象方法的使用步骤:
1.接口不能创建对象的,它没有构造方法
2.定义一个实现类,实现该接口,并实现接口中的所有的抽象方法
public class MyInterfaceImpl(实现类) implements MyInterface(接口){
3.创建该接口的实现类的对象,调用接口的方法
*/
public class Demo01 {
public static void main(String[] args) {
//MyInterface myInterface = new MyInterface();接口不能创建对象的
//创建该接口的实现类的对象,调用接口的方法
MyInterfaceImpl myInterface = new MyInterfaceImpl();
myInterface.method();
myInterface.method1();
}
}
@@ -8,6 +8,13 @@ public interface 接口名 {
jdk7之前:抽象方法,常量
jdk8:默认方法,静态方法
jdk9:私有方法
---------------------------------
接口中抽象方法定义:
public abstract 返回值类型 抽象方法名(参数列表);
但凡在接口中定义一个没有方法体{}的方法,就是public abstract修饰;暂时不省略
*/
public interface MyInterface {
public abstract void method();//抽象方法
void method1();//抽象方法,编译器会自动加上public abstract
}
@@ -0,0 +1,13 @@
package com.inmind.interface01;
public class MyInterfaceImpl implements MyInterface{
@Override
public void method() {
System.out.println("实现类中method方法");
}
@Override
public void method1() {
System.out.println("实现类中method1方法");
}
}