day10-继承父类并实现多个接口的注意事项
This commit is contained in:
8
day10/src/com/inmind/extends_impls06/Demo05.java
Normal file
8
day10/src/com/inmind/extends_impls06/Demo05.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.extends_impls06;
|
||||||
|
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Zi zi = new Zi();
|
||||||
|
zi.sameMethod();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
day10/src/com/inmind/extends_impls06/Fu.java
Normal file
10
day10/src/com/inmind/extends_impls06/Fu.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.extends_impls06;
|
||||||
|
|
||||||
|
//抽象父类
|
||||||
|
public abstract class Fu {
|
||||||
|
public abstract void methodFu();
|
||||||
|
//抽象父类中的普通方法
|
||||||
|
public void sameMethod(){
|
||||||
|
System.out.println("抽象父类中的方法sameMethod()");
|
||||||
|
};
|
||||||
|
}
|
||||||
6
day10/src/com/inmind/extends_impls06/MyInterface1.java
Normal file
6
day10/src/com/inmind/extends_impls06/MyInterface1.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.extends_impls06;
|
||||||
|
|
||||||
|
public interface MyInterface1 {
|
||||||
|
void method1();
|
||||||
|
void sameMethod();
|
||||||
|
}
|
||||||
6
day10/src/com/inmind/extends_impls06/MyInterface2.java
Normal file
6
day10/src/com/inmind/extends_impls06/MyInterface2.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.extends_impls06;
|
||||||
|
|
||||||
|
public interface MyInterface2 {
|
||||||
|
void method2();
|
||||||
|
void sameMethod();
|
||||||
|
}
|
||||||
33
day10/src/com/inmind/extends_impls06/Zi.java
Normal file
33
day10/src/com/inmind/extends_impls06/Zi.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.inmind.extends_impls06;
|
||||||
|
/*
|
||||||
|
单继承多实现的注意事项:
|
||||||
|
1.非抽象的子类实现类必须重写所有的抽象方法
|
||||||
|
2.如果有抽象方法未实现,那么子类实现类必须是抽象类
|
||||||
|
3.如果多个接口中有相同的抽象方法,那么非抽象的子类只需要实现一次即可
|
||||||
|
4.如果多个接口和抽象父类中有相同的抽象方法,那么非抽象的子类也只需要实现一次即可
|
||||||
|
5.如果抽象父类中拥有一个与多个接口中抽象方法,相同的普通方法,那么子类实现类,
|
||||||
|
就可以选择性地重写或不重写该抽象方法,类的方法优先于接口的
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Zi extends Fu implements MyInterface1,MyInterface2{
|
||||||
|
@Override
|
||||||
|
public void methodFu() {
|
||||||
|
System.out.println("子类重写抽象父类中的抽象方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method1() {
|
||||||
|
System.out.println("实现类实现接口MyInterface1中的抽象方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sameMethod() {
|
||||||
|
System.out.println("实现类实现接口MyInterface1和MyInterface2中的同名抽象方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method2() {
|
||||||
|
System.out.println("实现类实现接口MyInterface2中的抽象方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user