淺析Java語言中接口的特點(diǎn)和繼承相關(guān)知識(shí)
前面幾篇文章用Java帶大家一起了解Java的部分基礎(chǔ)知識(shí),感興趣的小伙伴們可以去學(xué)習(xí)下了,一篇文章帶你了解Java類的設(shè)計(jì)和封裝及類成員的訪問控制、一篇文章帶你了解this關(guān)鍵字和單例模式、Java基礎(chǔ)入門篇——面向?qū)ο蠛皖惖亩x,這篇文章帶大家一起學(xué)習(xí)下Java知識(shí)中的接口及其繼承知識(shí),一起來看看吧。
一、接口的繼承
1.定義一個(gè)接口使用extends關(guān)鍵字來繼承另一個(gè)接口,這就是接口的繼承。
2.語法:
[修飾符] interface 接口名 extends 父類接口1, 父類接口2, …{
定義常量0個(gè)或多個(gè);
定義抽象方法0個(gè)或多個(gè);
}
3.如何實(shí)現(xiàn)接口的繼承關(guān)系例子
//定義Printer接口
interface Printer{
int max_line=20;//定義全局常量
void printRun();//定義抽象方法pirntRun()
void printData();//定義抽象方法printData()
}
//定義了Autopriner接口,然后繼承Printer接口
interface Autoprinter extends Printer{//接口繼承
void autoPrint();//定義抽象方法autoPrint()方法
}
//Colorprinter類實(shí)現(xiàn)Printer接口
class Colorprinter implements Autoprinter{
//實(shí)現(xiàn)printRun()方法
public void printRun(){
System.out.println("彩色打印機(jī)正在啟動(dòng)");
}
//實(shí)現(xiàn)printData()方法
public void printData(){
System.out.println("彩色打印機(jī)正在打印中");
}
//實(shí)現(xiàn)autoPrint()方法
public void autoPrint(){
System.out.println("彩色打印機(jī)自動(dòng)打印");
}
}
public class p27 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Colorprinter c=new Colorprinter();//創(chuàng)建Colorprinter類的實(shí)例對(duì)象
c.printRun();//調(diào)用Colorprinter類的printRun()方法
c.printData();//調(diào)用Colorprinter類的printData()方法
c.autoPrint();//調(diào)用Colorprinter類的autoPrint()方法
}
}
輸出的結(jié)果是:
彩色打印機(jī)正在啟動(dòng)
彩色打印機(jī)正在打印中
彩色打印機(jī)自動(dòng)打印
從上面的代碼中,首先是定義了Printer接口,定義全局常量和兩個(gè)方法分別是printRun()和printData(),然后再定義一個(gè)Autopriner接口來繼承Printer接口,再Colorprinter類實(shí)現(xiàn)Printer接口,此時(shí),Autopriner接口有三個(gè)抽象方法,通過Colorprinter類實(shí)例化對(duì)象然后調(diào)用對(duì)應(yīng)的方法。
二、接口的特點(diǎn)
1.接口里的方法是抽象的,不可以去實(shí)例化對(duì)象。
2.如果實(shí)現(xiàn)某接口的類是抽象類,那么可以不實(shí)現(xiàn)該接口的所有方法。對(duì)于這個(gè)抽象類任何一個(gè)不是抽象類的子類來說,就需要實(shí)現(xiàn)所有抽象方法。
3.一個(gè)類使用implements關(guān)鍵字實(shí)現(xiàn)多個(gè)接口
例如:
//定義A接口
interface A{
int MAX=1;//定義全局常量
void testA();//定義抽象方法testA()
}
//定義B接口
interface B{
int MAX=2;//定義全局常量
void testB();//定義抽象方法testB()
}
//定義一個(gè)類實(shí)現(xiàn)A,B接口
class C implements A,B{
int MAX=3;//定義全局常量
//實(shí)現(xiàn)testB()方法
public void testB() {
// TODO Auto-generated method stub
System.out.println("我是B");
}
//實(shí)現(xiàn)testA()方法
public void testA() {
// TODO Auto-generated method stub
System.out.println("我是A");
}
}
public class p28 {
public static void main(String[] args) {
// TODO Auto-generated method stub
C c=new C();//創(chuàng)建C類實(shí)例對(duì)象
c.testA();//調(diào)用C類的testA()方法
c.testB();//調(diào)用C類的testB()方法
}
}
運(yùn)行的結(jié)果是:
我是A
我是B
從上面代碼中,定義了A和B接口,在C類中使用implements關(guān)鍵字實(shí)現(xiàn)A和B的接口的方法,實(shí)例化對(duì)象并調(diào)用對(duì)應(yīng)的方法。
4.一個(gè)接口使用extends關(guān)鍵字實(shí)現(xiàn)繼承多個(gè)接口
例如:
//定義A接口
interface A{
int MAX=1;//定義全局常量
void testA();//定義抽象方法testA()
}
//定義B接口
interface B{
int MAX=2;//定義全局常量
void testB();//定義抽象方法testB()
}
//定義接口繼承A,B接口
interface C extends A,B{
int MAX=3;//定義全局常量
void testC();//定義testC()方法
}
public class p29 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(A.MAX);
System.out.println(B.MAX);
System.out.println(C.MAX);
}
}
運(yùn)行的結(jié)果是:
1
2
3
5.一個(gè)類在實(shí)現(xiàn)某接口的抽象方法,就需要使用一樣的方法頭。
6.如果實(shí)現(xiàn)某接口的類不是抽象類,那么類的定義部分需要實(shí)現(xiàn)指定接口的所有抽象方法。
三、總結(jié)
本文主要介紹了接口的繼承和接口的特點(diǎn)。
接口的繼承是定義一個(gè)接口使用extends關(guān)鍵字來繼承另一個(gè)接口,通過如何實(shí)現(xiàn)接口的繼承例子來幫助大家的理解這個(gè)用法。
一個(gè)類使用implements關(guān)鍵字實(shí)現(xiàn)多個(gè)接口,一個(gè)接口使用extends關(guān)鍵字實(shí)現(xiàn)繼承多個(gè)接口。
希望大家通過本文的學(xué)習(xí),對(duì)你有所幫助!