day10-java的单继承多实现的操作特点
This commit is contained in:
13
day10/src/com/inmind/extends_interfaces06/Demo06.java
Normal file
13
day10/src/com/inmind/extends_interfaces06/Demo06.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind.extends_interfaces06;
|
||||||
|
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//调用单继承多实现的类的对象,调用各种方法
|
||||||
|
Zi zi = new Zi();
|
||||||
|
zi.methodFu();
|
||||||
|
zi.method1();
|
||||||
|
zi.method2();
|
||||||
|
zi.method();
|
||||||
|
zi.sameMethod();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
day10/src/com/inmind/extends_interfaces06/Fu.java
Normal file
10
day10/src/com/inmind/extends_interfaces06/Fu.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.extends_interfaces06;
|
||||||
|
|
||||||
|
public abstract class Fu {
|
||||||
|
public abstract void methodFu();
|
||||||
|
public abstract void method();
|
||||||
|
|
||||||
|
public void sameMethod(){
|
||||||
|
System.out.println("这是父类的同名的普通方法sameMethod");
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.inmind.extends_interfaces06;
|
||||||
|
|
||||||
|
public interface MyInterface1 {
|
||||||
|
void method1();
|
||||||
|
void method();
|
||||||
|
void sameMethod();
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.inmind.extends_interfaces06;
|
||||||
|
|
||||||
|
public interface MyInterface2 {
|
||||||
|
void method2();
|
||||||
|
void method();
|
||||||
|
void sameMethod();
|
||||||
|
}
|
||||||
41
day10/src/com/inmind/extends_interfaces06/Zi.java
Normal file
41
day10/src/com/inmind/extends_interfaces06/Zi.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package com.inmind.extends_interfaces06;
|
||||||
|
/*
|
||||||
|
单继承父类并实现多个接口
|
||||||
|
注意事项:
|
||||||
|
1.非抽象的子类实现类必须重写所有的抽象方法
|
||||||
|
2.如果有抽象方法没有实现,那么子类实现类必须是抽象类
|
||||||
|
3.如果多个接口中有相同的抽象方法,那么子类实现类只要实现一次即可。
|
||||||
|
4.如果多个接口和抽象父类中有相同的抽象方法,那么子类实现类也只要实现一次即可。
|
||||||
|
5.如果多个接口有相同的抽象方法,并且在父类中有一个同名的普通方法,
|
||||||
|
那么子类实现类可以不重写接口中的抽象方法,也可以根据需求来重写,类的方法优先于接口
|
||||||
|
*/
|
||||||
|
public class Zi extends Fu implements MyInterface1,MyInterface2{
|
||||||
|
@Override
|
||||||
|
public void methodFu() {
|
||||||
|
System.out.println("子类实现了抽象父类的抽象方法methodFu");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method1() {
|
||||||
|
System.out.println("实现类Zi实现了接口1的抽象方法method1");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method() {
|
||||||
|
System.out.println("实现类Zi实现了父类Fu、接口1、接口2的同名抽象方法method");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method2() {
|
||||||
|
System.out.println("实现类Zi实现了接口2的抽象方法method2");
|
||||||
|
|
||||||
|
}
|
||||||
|
//ctrl+o
|
||||||
|
@Override
|
||||||
|
public void sameMethod() {
|
||||||
|
super.sameMethod();
|
||||||
|
System.out.println("zi类重写了fu类的普通方法sameMethod,同时也刚好实现了2个接口的抽象方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,18 @@
|
|||||||
package com.inmind.interface_constant05;
|
package com.inmind.interface_constant05;
|
||||||
|
/*
|
||||||
|
接口中的内容特点:
|
||||||
|
1.抽象方法(jdk7之前):制定规则,规范
|
||||||
|
2.默认方法(jdk8):优化解决接口升级的问题
|
||||||
|
3.静态方法(jdk8):定义一些可以直接通过接口名调用的方法
|
||||||
|
4.私有方法(jdk9):对默认方法和静态方法共有的代码进行抽取复用,并不让外界调用
|
||||||
|
5.常量(jdk7之前):定义一些可以通过接口名直接访问的值
|
||||||
|
|
||||||
|
抽象方法:定义实现类必须遵守的规范;
|
||||||
|
默认方法:为实现类提供可选的默认实现;
|
||||||
|
静态方法:为接口本身提供辅助工具功能
|
||||||
|
私有方法(jdk9):对默认方法和静态方法共有的代码进行抽取复用,并不让外界调用
|
||||||
|
接口常量的作用域是全局可见(public),且不可修改(final)适合定义"公共标准值"。为接口的行为规范提供配套的固定值
|
||||||
|
*/
|
||||||
public class Demo05 {
|
public class Demo05 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//调用接口的常量
|
//调用接口的常量
|
||||||
|
|||||||
Reference in New Issue
Block a user