day10--接口之间的多继承
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public interface MyInterface1 {
|
||||
public static final int a = 10;//放入常量池
|
||||
|
||||
void method1();
|
||||
void method();
|
||||
|
||||
public static void staticMethod1(){
|
||||
System.out.println("接口1的静态方法");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public interface MyInterface2 {
|
||||
int b = 20;
|
||||
|
||||
void method2();
|
||||
void method();
|
||||
public static void staticMethod2(){
|
||||
System.out.println("接口2的静态方法");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.inmind.interface_extends07;
|
||||
/*
|
||||
类与类:单继承
|
||||
类与接口:多实现
|
||||
接口与接口:多继承
|
||||
|
||||
类的继承:单继承,一个类永远只有一个父类
|
||||
|
||||
接口的继承:接口是多继承的,接口可以有多个直接的父接口
|
||||
接口的多继承的注意事项:
|
||||
1.如果是继承,子接口继承所有的父接口的抽象方法,子接口的实现类必须实现所有的抽象方法
|
||||
2.如果是继承的父接口中有同名的抽象方法,子接口只会继承一个抽象方法,子接口的实现类也只需要实现一次同名的抽象方法
|
||||
3.接口的静态方法不能通过对象调用
|
||||
4.接口的静态方法只跟本接口相关,只能通过本接口名.静态方法来调用
|
||||
5.常量也类似
|
||||
*/
|
||||
public interface MyInterfaceZi extends MyInterface1,MyInterface2 {
|
||||
void methodZi();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public class MyInterfaceZiImpl implements MyInterfaceZi{
|
||||
@Override
|
||||
public void methodZi() {
|
||||
MyInterface1.staticMethod1();
|
||||
MyInterface2.staticMethod2();
|
||||
System.out.println(MyInterface1.a);
|
||||
System.out.println(MyInterface2.b);
|
||||
System.out.println(MyInterfaceZi.b);//可以访问,原因是final,将值放入了常量池
|
||||
System.out.println(MyInterfaceZi.a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method1() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method2() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user