day10-接口之间的多继承
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package com.inmind.interface_interface07;
|
||||||
|
/*
|
||||||
|
类的继承:单继承,一个类永远只有一个父类
|
||||||
|
|
||||||
|
接口的继承:接口是多继承的,接口可以有多个直接的父接口
|
||||||
|
接口的多继承的注意事项:
|
||||||
|
1.如果是继承,子接口继承所有的父接口的抽象方法,子接口的实现类必须实现所有的抽象方法
|
||||||
|
2.如果是继承的父接口中有同名的抽象方法,子接口只会继承一个抽象方法,子接口的实现类也只需要实现一次同名的抽象方法
|
||||||
|
3.接口的静态方法不能通过对象调用
|
||||||
|
4.接口的静态方法只跟本接口相关,只能通过本接口名.静态方法来调用
|
||||||
|
5.常量也类似
|
||||||
|
|
||||||
|
总结:
|
||||||
|
类与类:单继承,多层继承
|
||||||
|
类与接口:多实现
|
||||||
|
接口与接口:多继承
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NewInterfaceImpl newInterface = new NewInterfaceImpl();
|
||||||
|
//newInterface.smethod1();//父接口的静态方法,子接口的对象无法调用
|
||||||
|
//NewInterface.smethod1();//父接口的静态方法,子接口也无法调用
|
||||||
|
MyInterface1.smethod1();
|
||||||
|
MyInterface2.smethod2();
|
||||||
|
System.out.println(MyInterface1.i1);
|
||||||
|
System.out.println(MyInterface2.i2);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.interface_interface07;
|
||||||
|
|
||||||
|
public interface MyInterface1 {
|
||||||
|
int i1 = 20;
|
||||||
|
|
||||||
|
void method1();
|
||||||
|
void method();
|
||||||
|
static void smethod1(){
|
||||||
|
System.out.println("接口1的静态方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.interface_interface07;
|
||||||
|
|
||||||
|
public interface MyInterface2 {
|
||||||
|
int i2 = 10;
|
||||||
|
|
||||||
|
void method2();
|
||||||
|
void method();
|
||||||
|
static void smethod2(){
|
||||||
|
System.out.println("接口2的静态方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.inmind.interface_interface07;
|
||||||
|
//接口与接口之间是多继承的
|
||||||
|
public interface NewInterface extends MyInterface1, MyInterface2{
|
||||||
|
void newMethod();
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.interface_interface07;
|
||||||
|
|
||||||
|
public class NewInterfaceImpl implements NewInterface{
|
||||||
|
@Override
|
||||||
|
public void newMethod() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method1() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user