亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Java 菜鳥教程

Java 流程控制

Java 數(shù)組

Java 面向?qū)ο?I)

Java 面向?qū)ο?II)

Java 面向?qū)ο?III)

Java 異常處理

Java 列表(List)

Java Queue(隊(duì)列)

Java Map集合

Java Set集合

Java 輸入輸出(I/O)

Java Reader/Writer

Java 其他主題

Java 構(gòu)造函數(shù)

在本教程中,您將在示例的幫助下了解Java構(gòu)造函數(shù),如何創(chuàng)建和使用它們以及不同類型的構(gòu)造函數(shù)。

什么是構(gòu)造函數(shù)?

在Java中,每個(gè)類都有它的構(gòu)造函數(shù),當(dāng)類的對(duì)象被創(chuàng)建時(shí),該構(gòu)造函數(shù)將被自動(dòng)調(diào)用。構(gòu)造函數(shù)類似于方法,但實(shí)際上它不是方法。

一個(gè)Java方法和Java構(gòu)造函數(shù)以通過(guò)其名稱和返回類型進(jìn)行區(qū)分。構(gòu)造函數(shù)與類的構(gòu)造函數(shù)同名,并且不返回任何值。例如,

class Test {
    Test() {
        //構(gòu)造函數(shù)體
    }
}

在這里,Test()是一個(gè)構(gòu)造函數(shù)。它具有與該類相同的名稱,并且沒(méi)有返回類型。

class Test {
    void Test() {
        // 方法主體
    }
}

 這里,Test()與類的名稱相同。但是,它有一個(gè)返回類型void。因此,它是一個(gè)方法,而不是一個(gè)構(gòu)造函數(shù)。

示例:Java構(gòu)造函數(shù)

class Main {
   private int x;

   // 構(gòu)造函數(shù)體
   private Main(){
       System.out.println("構(gòu)造函數(shù)被調(diào)用");
       x = 5;
   }
   public static void main(String[] args){
       //創(chuàng)建對(duì)象時(shí)調(diào)用構(gòu)造函數(shù)
       Main obj = new Main();
       System.out.println("x 的值 = " + obj.x);
   }
}

輸出

構(gòu)造函數(shù)被調(diào)用
x 的值 = 5

 在上面的示例中,我們有一個(gè)名為Main()的私有構(gòu)造函數(shù)。在main方法中,我們正在創(chuàng)建一個(gè)名為obj的類對(duì)象。

Main obj = new Main();

 在此過(guò)程中,將調(diào)用構(gòu)造函數(shù)。因此,執(zhí)行print語(yǔ)句并初始化變量x。

構(gòu)造函數(shù)的類型

在Java中,構(gòu)造函數(shù)可以分為3種類型:

  • 無(wú)參數(shù)構(gòu)造函數(shù)

  • 默認(rèn)構(gòu)造函數(shù)

  • 參數(shù)構(gòu)造函數(shù)

無(wú)參數(shù)構(gòu)造函數(shù)

Java構(gòu)造函數(shù)可以具有或可以不具有任何參數(shù)。如果構(gòu)造函數(shù)不接受任何參數(shù),則稱為無(wú)參數(shù)構(gòu)造函數(shù)。例如,

private Constructor() {
   // 構(gòu)造函數(shù)體
}

無(wú)參數(shù)構(gòu)造函數(shù)的示例

class Main {

   int i;
   //沒(méi)有參數(shù)的構(gòu)造函數(shù)
   private Main(){
       i = 5;
       System.out.println("Object created and i = " + i);
   }

   public static void main(String[] args) {
       //不帶任何參數(shù)調(diào)用構(gòu)造函數(shù)
       Main obj = new Main();
   }
}

輸出

Object created and i = 5

在這里,構(gòu)造Main()函數(shù)不接受任何參數(shù)。

您是否注意到Main()構(gòu)造函數(shù)的訪問(wèn)修飾符是私有(private)的?

這是因?yàn)樵搶?duì)象是從同一類中實(shí)例化的。因此,它可以訪問(wèn)構(gòu)造函數(shù)。

但是,如果對(duì)象是在類外部創(chuàng)建的,則必須聲明構(gòu)造函數(shù)為public才能訪問(wèn)它。例如:

class Company {
    String domainName;

    // 公共構(gòu)造函數(shù)
    public Company(){
        domainName = "(cainiaoplus.com)";
    }
}

public class Main {
    
    public static void main(String[] args) {
        // 在另一個(gè)類中創(chuàng)建對(duì)象
        Company companyObj = new Company();
        System.out.println("Domain name = "+ companyObj.domainName);
    }
}

輸出

Domain name = (cainiaoplus.com)

相關(guān)閱讀: Java訪問(wèn)修飾符

默認(rèn)構(gòu)造函數(shù)

如果不創(chuàng)建任何構(gòu)造函數(shù),則Java編譯器將在運(yùn)行時(shí)自動(dòng)創(chuàng)建無(wú)參數(shù)的構(gòu)造函數(shù)。此構(gòu)造函數(shù)稱為默認(rèn)構(gòu)造函數(shù)。默認(rèn)構(gòu)造函數(shù)使用默認(rèn)值初始化所有未初始化的實(shí)例變量。

類型默認(rèn)值
booleanfalse
byte0
short0
int0
long0L
char\u0000
float0.0f
double0.0d
objectnull

示例:默認(rèn)構(gòu)造函數(shù)

class DefaultConstructor {

    int a;
    boolean b;

    public static void main(String[] args) {

        //調(diào)用默認(rèn)構(gòu)造函數(shù)
        DefaultConstructor obj = new DefaultConstructor();
        System.out.println("a = " + obj.a);
        System.out.println("b = " + obj.b);
    }
}

輸出

a = 0
b = false

在上面的程序中,我們還沒(méi)有初始化變量a和b的值。但是,當(dāng)我們創(chuàng)建類的對(duì)象時(shí),我們可以在輸出中看到這些值已使用某些值進(jìn)行了初始化。

 這是因?yàn)镴ava編譯器自動(dòng)創(chuàng)建了一個(gè)默認(rèn)構(gòu)造函數(shù)。構(gòu)造函數(shù)將使用默認(rèn)值0和false初始化變量a和b的值。

上面的程序等同于:

class DefaultConstructor {

    int a;
    boolean b;

    //私有構(gòu)造函數(shù) 
    private DefaultConstructor() {
        a = 0;
        b = false;
    }

    public static void main(String[] args) {
        //調(diào)用構(gòu)造函數(shù)
        DefaultConstructor obj = new DefaultConstructor();
        System.out.println("a = " + obj.a);
        System.out.println("b = " + obj.b);
    }
}

輸出

a = 0
b = false

參數(shù)化構(gòu)造函數(shù)

與方法類似,我們可以將參數(shù)傳遞給構(gòu)造函數(shù)。此類構(gòu)造函數(shù)稱為參數(shù)化構(gòu)造函數(shù)。例如,

private Constructor (arg1, arg2, ..., argn) {
    // 構(gòu)造函數(shù)體
}

示例:參數(shù)化構(gòu)造函數(shù)

class Vehicle {

    int wheels;

    //接受單個(gè)值的構(gòu)造函數(shù)
    private Vehicle(int wheels){
        this.wheels = wheels;
        System.out.println(wheels + " wheeler vehicle created.");
    }

    public static void main(String[] args) {
        //通過(guò)傳遞單個(gè)值來(lái)調(diào)用構(gòu)造函數(shù)
        Vehicle v1 = new Vehicle(2);
        Vehicle v2 = new Vehicle(3);
        Vehicle v3 = new Vehicle(4);
    }
}

輸出

2 wheeler vehicle created.
3 wheeler vehicle created.
4 wheeler vehicle created.

在上面的示例中,我們有一個(gè)名為Vehicle()的構(gòu)造函數(shù)。構(gòu)造函數(shù)接受一個(gè)名為wheels的參數(shù)。

在這里,當(dāng)創(chuàng)建對(duì)象時(shí),我們將參數(shù)傳遞給構(gòu)造函數(shù)。并且,基于參數(shù),它正在生成輸出。

Java中的構(gòu)造函數(shù)重載

 與方法重載類似,我們也可以在Java中重載構(gòu)造函數(shù)。如果您不熟悉方法重載,請(qǐng)?jiān)L問(wèn)Java方法重載

在構(gòu)造函數(shù)重載中,有兩個(gè)或多個(gè)具有不同參數(shù)的構(gòu)造函數(shù)。例如,

class Company {

    String domainName;

    //沒(méi)有參數(shù)的構(gòu)造函數(shù)
    public Company(){
        this.domainName = "default";
    }

    //具有單個(gè)參數(shù)的構(gòu)造函數(shù)
    public Company(String domainName){
        this.domainName = domainName;
    }

    public void getName(){
        System.out.println(this.domainName);
    }

    public static void main(String[] args) {
        //不帶參數(shù)調(diào)用構(gòu)造函數(shù)
        Company defaultObj = new Company();

        //使用單個(gè)參數(shù)調(diào)用構(gòu)造函數(shù)
        Company nhoooObj = new Company("(cainiaoplus.com)");
        defaultObj.getName();
        nhoooObj.getName();
    }
}

輸出

default
(cainiaoplus.com)

在上面的示例中,我們有兩個(gè)構(gòu)造函數(shù):public Company()和public Company(String domainName)。

在這里,兩個(gè)構(gòu)造函數(shù)都用不同的值初始化變量domainName。因此,根據(jù)我們需要的值,我們可以從main()方法調(diào)用構(gòu)造函數(shù)。

注意,我們使用this關(guān)鍵字來(lái)指定類的變量。要了解有關(guān)this關(guān)鍵字的更多信息,請(qǐng)?jiān)L問(wèn)Java this關(guān)鍵字。

重要筆記

  •  構(gòu)造函數(shù)在實(shí)例化對(duì)象時(shí)被隱式調(diào)用。

  • 創(chuàng)建構(gòu)造函數(shù)的兩個(gè)規(guī)則是:

    • 構(gòu)造函數(shù)的名稱應(yīng)與類的名稱相同。

    • Java構(gòu)造函數(shù)不得具有返回類型。

  • 如果類沒(méi)有構(gòu)造函數(shù),則Java編譯器會(huì)在運(yùn)行時(shí)自動(dòng)創(chuàng)建默認(rèn)構(gòu)造函數(shù)。默認(rèn)構(gòu)造函數(shù)使用默認(rèn)值初始化實(shí)例變量。例如,int變量將被初始化為0

  • 構(gòu)造函數(shù)類型:

    • 無(wú)參數(shù)構(gòu)造函數(shù)  - 不接受任何參數(shù)的構(gòu)造函數(shù)

    • 默認(rèn)構(gòu)造函數(shù)  - 如果沒(méi)有顯式定義,Java編譯器會(huì)自動(dòng)創(chuàng)建一個(gè)構(gòu)造函數(shù)。

    • 參數(shù)化構(gòu)造函數(shù)  - 接受參數(shù)的構(gòu)造函數(shù)

  • 構(gòu)造函數(shù)不能是抽象的abstract 、static或final。

  • 構(gòu)造函數(shù)可以重載,但不能被重寫。