您好,歡迎來到賦能網(wǎng)!

java接口可以創(chuàng)建對(duì)象嗎?java接口如何實(shí)現(xiàn)?

賦能網(wǎng) 2023-05-09 89

java接口對(duì)于程序來說意義重大,它可以被理解為一種特殊的類,能夠保護(hù)代碼的安全和嚴(yán)密,有利于代碼的規(guī)范,那java接口可以創(chuàng)建對(duì)象嗎?下面來我們就來給大家講解一下。

java接口可以創(chuàng)建對(duì)象,可以使用使用匿名方式創(chuàng)建對(duì)象。

例如:

File driec = new File("G:/file/test");
File[] files = driec.listFiles(new FilenameFilter()
{ //FilenameFilter是接口,創(chuàng)建其匿名對(duì)象
    @Override
    public boolean accept(File dir, String name)
    {
        return new File(dir, name)
            .isFile() && name.endsWith(".java");
    }
});

java接口如何實(shí)現(xiàn)?

接口的主要用途就是被實(shí)現(xiàn)類實(shí)現(xiàn),一個(gè)類可以實(shí)現(xiàn)一個(gè)或多個(gè)接口,繼承使用 extends 關(guān)鍵字,實(shí)現(xiàn)則使用 implements 關(guān)鍵字。因?yàn)橐粋€(gè)類可以實(shí)現(xiàn)多個(gè)接口,這也是 Java 為單繼承靈活性不足所作的補(bǔ)充。類實(shí)現(xiàn)接口的語法格式如下:

class[extends superclass_name] [implements interface1_name[, interface2_name…]] {

// 主體

}

對(duì)以上語法的說明如下:

public:類的修飾符;

superclass_name:需要繼承的父類名稱;

interface1_name:要實(shí)現(xiàn)的接口名稱。

實(shí)現(xiàn)接口需要注意以下幾點(diǎn):

實(shí)現(xiàn)接口與繼承父類相似,一樣可以獲得所實(shí)現(xiàn)接口里定義的常量和方法。如果一個(gè)類需要實(shí)現(xiàn)多個(gè)接口,則多個(gè)接口之間以逗號(hào)分隔。

一個(gè)類可以繼承一個(gè)父類,并同時(shí)實(shí)現(xiàn)多個(gè)接口,implements 部分必須放在 extends 部分之后。

一個(gè)類實(shí)現(xiàn)了一個(gè)或多個(gè)接口之后,這個(gè)類必須完全實(shí)現(xiàn)這些接口里所定義的全部抽象方法(也就是重寫這些抽象方法);否則,該類將保留從父接口那里繼承到的抽象方法,該類也必須定義成抽象類。

在程序的開發(fā)中,需要完成兩個(gè)數(shù)的求和運(yùn)算和比較運(yùn)算功能的類非常多。那么可以定義一個(gè)接口來將類似的功能組織在一起。下面創(chuàng)建一個(gè)示例,具體介紹接口的實(shí)現(xiàn)方式。

1)創(chuàng)建一個(gè)名稱為 IMath 的接口,代碼如下:

public interface IMath
{
    public int sum(); // 完成兩個(gè)數(shù)的相加
    public int maxNum(int a, int b); // 獲取較大的數(shù)
}

2)定義一個(gè) MathClass 類并實(shí)現(xiàn) IMath 接口,MathClass 類實(shí)現(xiàn)代碼如下:

public class MathClass implements IMath
{
    private int num1; // 第 1 個(gè)操作數(shù)
    private int num2; // 第 2 個(gè)操作數(shù)
    public MathClass(int num1, int num2)
    {
        // 構(gòu)造方法
        this.num1 = num1;
        this.num2 = num2;
    }
    // 實(shí)現(xiàn)接口中的求和方法
    public int sum()
    {
        return num1 + num2;
    }
    // 實(shí)現(xiàn)接口中的獲取較大數(shù)的方法
    public int maxNum(int a, int b)
    {
        if (a >= b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
}

在實(shí)現(xiàn)類中,所有的方法都使用了 public 訪問修飾符聲明。無論何時(shí)實(shí)現(xiàn)一個(gè)由接口定義的方法,它都必須實(shí)現(xiàn)為 public,因?yàn)榻涌谥械乃谐蓡T都顯式聲明為 public。

3)最后創(chuàng)建測(cè)試類 NumTest,實(shí)例化接口的實(shí)現(xiàn)類 MathClass,調(diào)用該類中的方法并輸出結(jié)果。該類內(nèi)容如下:

public class NumTest
{
    public static void main(String[] args)
    {
        // 創(chuàng)建實(shí)現(xiàn)類的對(duì)象
        MathClass calc = new MathClass(100, 300);
        System.out.println("100 和 300 相加結(jié)果是:" + calc.sum());
        System.out.println("100 比較 300,哪個(gè)大:" + calc.maxNum(100, 300));
    }
}

程序運(yùn)行結(jié)果如下所示。

100 和 300 相加結(jié)果是:400
100 比較 300,哪個(gè)大:300

在該程序中,首先定義了一個(gè) IMath 的接口,在該接口中只聲明了兩個(gè)未實(shí)現(xiàn)的方法,這兩個(gè)方法需要在接口的實(shí)現(xiàn)類中實(shí)現(xiàn)。在實(shí)現(xiàn)類 MathClass 中定義了兩個(gè)私有的屬性,并賦予兩個(gè)屬性初始值,同時(shí)創(chuàng)建了該類的構(gòu)造方法。因?yàn)樵擃悓?shí)現(xiàn)了 MathClass 接口,因此必須實(shí)現(xiàn)接口中的方法。在最后的測(cè)試類中,需要?jiǎng)?chuàng)建實(shí)現(xiàn)類對(duì)象,然后通過實(shí)現(xiàn)類對(duì)象調(diào)用實(shí)現(xiàn)類中的方法。

這樣就java接口就可以實(shí)現(xiàn)了,接口實(shí)現(xiàn)之后無法被實(shí)例化,但是可以被實(shí)現(xiàn)。最后大家如果想要了解更多初識(shí)java知識(shí),敬請(qǐng)關(guān)注賦能網(wǎng)。


本文鏈接:

本文章“java接口可以創(chuàng)建對(duì)象嗎?java接口如何實(shí)現(xiàn)?”已幫助 89 人

免責(zé)聲明:本信息由用戶發(fā)布,本站不承擔(dān)本信息引起的任何交易及知識(shí)產(chǎn)權(quán)侵權(quán)的法律責(zé)任!

本文由賦能網(wǎng) 整理發(fā)布。了解更多培訓(xùn)機(jī)構(gòu)》培訓(xùn)課程》學(xué)習(xí)資訊》課程優(yōu)惠》課程開班》學(xué)校地址等機(jī)構(gòu)信息,可以留下您的聯(lián)系方式,讓課程老師跟你詳細(xì)解答:
咨詢熱線:4008-569-579

如果本頁不是您要找的課程,您也可以百度查找一下: