Java教程

Java 构造函数

Java中有两种构造函数:
默认构造函数(无参数构造函数) 参数化构造函数 Java构造函数

Java默认构造函数

如果构造函数不存在,则称为"默认构造函数"没有任何参数。

默认构造函数的语法:

<class_name>(){}

默认构造函数示例

在此示例中,我们在Bike类中创建no-arg构造函数。将在创建对象时调用它。
//Java Program to create and call a default constructor
class Bike1{
    //creating a default
    constructorBike1(){
        System.out.println("Bike is created");
    }
    //main method
    public static void main(String args[]){
        //calling a default
        constructorBike1 b=new Bike1();
    }
}
输出:
Bike is created

注意: 规则: 如果类中没有构造函数,则编译器会自动创建一个默认构造函数。

Java默认构造函数

Q)的目的是什么

默认构造函数用于根据类型为对象提供默认值,例如0,null等。

示例显示默认值的默认构造函数

//Let us see another example of default constructor
//which displays the default values
class Student3{
    int id;
    String name;
    //method to display the value of id and namevoid
    display(){
        System.out.println(id+" "+name);
    }
    public static void main(String args[]){
        //creating objects
        Student3 s1=new Student3();
        Student3 s2=new Student3();
        //displaying values of the
        objects1.display();
        s2.display();
    }
}
输出:
0 null
0 null
说明: 在上面的类中,您没有创建任何构造函数,因此编译器为您提供了默认的构造函数。这里的默认构造函数提供了0和null值。

Java参数化构造函数

具有特定数量参数的构造函数称为参数化构造函数。

为什么使用参数化构造函数?

参数化构造函数用于为不同的对象提供不同的值。但是,您也可以提供相同的值。

参数化构造函数的示例

在此示例中,我们创建了具有两个参数的Student类的构造函数。构造函数中可以有任意数量的参数。
//Java Program to demonstrate the use of the parameterized constructor.class Student4{
    int id;
    String name;
    //creating a parameterized constructor Student4(int i,String n){
        id = i;
        name = n;
    }
    //method to display the values void display(){
        System.out.println(id+" "+name);
    }
    public static void main(String args[]){
        //creating objects and passing values Student4 s1 = new Student4(111,"Karan");
        Student4 s2 = new Student4(222,"Aryan");
        //calling method to display the values of object s1.display();
        s2.display();
    }
}
输出:
111 Karan
222 Aryan

Java中的构造函数重载

在Java中,构造函数就像方法一样,但是没有返回类型。它也可以像Java方法一样重载。
构造函数Java中的重载是一种具有多个具有不同参数列表的构造函数的技术。它们的排列方式使每个构造函数执行不同的任务。编译器通过列表中参数的数量及其类型来区分它们。

构造函数重载示例

//Java program to overload constructorsclass Student5{
    int id;
    String name;
    int age;
    //creating two arg constructor Student5(int i,String n){
        id = i;
        name = n;
    }
    //creating three arg constructor Student5(int i,String n,int a){
        id = i;
        name = n;
        age=a;
    }
    void display(){
        System.out.println(id+" "+name+" "+age);
    }
    public static void main(String args[]){
        Student5 s1 = new Student5(111,"Karan");
        Student5 s2 = new Student5(222,"Aryan",25);
        s1.display();
        s2.display();
    }
}
输出:
111 Karan 0
222 Aryan 25

Java中的构造函数与方法之间的差异

构造函数与方法之间存在许多差异。它们在下面给出。
Java构造函数 Java方法
构造函数用于初始化对象的状态。 一种方法用于揭示对象的行为。
构造函数不得具有返回类型。 方法必须具有返回类型。
构造函数被隐式调用。 该方法被显式调用。
如果类中没有任何构造函数,则Java编译器会提供默认构造函数。 在任何情况下编译器都不提供该方法。
构造函数名称必须与类名称相同。 方法名称可以与类名称相同或不相同。

Java构造函数与方法

Java复制构造函数

Java中没有复制构造函数。但是,我们可以将值从一个对象复制到另一个对象,例如C ++中的复制构造函数。
在Java中,有很多方法可以将一个对象的值复制到另一个对象。他们是:
通过构造函数 通过将一个对象的值分配给另一个对象 通过Object类的clone()方法
在此示例中,我们将使用Java构造函数将一个对象的值复制到另一个对象中。
//Java program to initialize the values from one object to another object.class Student6{
    int id;
    String name;
    //constructor to initialize integer and string Student6(int i,String n){
        id = i;
        name = n;
    }
    //constructor to initialize another object Student6(Student6 s){
        id = s.id;
        name =s.name;
    }
    void display(){
        System.out.println(id+" "+name);
    }
    public static void main(String args[]){
        Student6 s1 = new Student6(111,"Karan");
        Student6 s2 = new Student6(s1);
        s1.display();
        s2.display();
    }
}
输出:
111 Karan
111 Karan

在没有构造函数的情况下复制值

我们可以通过将对象的值分配给另一个对象来将一个对象的值复制到另一个对象中。在这种情况下,无需创建构造函数。
class Student7{
    int id;
    String name;
    Student7(int i,String n){
        id = i;
        name = n;
    }
    Student7(){
    }
    void display(){
        System.out.println(id+" "+name);
    }
    public static void main(String args[]){
        Student7 s1 = new Student7(111,"Karan");
        Student7 s2 = new Student7();
        s2.id=s1.id;
        s2.name=s1.name;
        s1.display();
        s2.display();
    }
}
输出:
111 Karan
111 Karan

Q)构造函数是否返回任何值?

是,它是当前的类实例(不能使用返回类型,但它返回一个值)。

构造函数能否执行其他任务而不是初始化?

是的,例如对象创建,启动线程,调用方法等。您可以像在方法中执行操作一样在构造函数中执行任何操作。

Java中是否有构造器类?

是。

构造函数类的用途是什么?

Java提供了一个构造函数类,可用于获取该类中构造函数的内部信息。可在java.lang.reflect包中找到它。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4