Java中的构造函数

Java构造函数或Java中的构造函数是一个术语,用于在我们的程序中构造某些东西。Java中的构造函数是 特殊方法 用于初始化对象的。在创建类的对象时调用构造函数。它可用于设置对象属性的初始值。

null

在Java中,构造函数是与方法类似的代码块。在创建类的实例时调用它。在调用构造函数时,对象的内存被分配到内存中。它是一种特殊类型的方法,用于初始化对象。每次使用new()关键字创建对象时,至少会调用一个构造函数。

注: 没有必要为类编写构造函数。这是因为如果你的类没有构造函数,java编译器会创建一个默认的构造函数。

构造函数与Java中的方法有何不同?

  • 构造函数必须与定义它的类具有相同的名称,而Java中的方法不需要该名称。
  • 构造函数不返回任何类型,而方法具有返回类型或 无效的 if不返回任何值。
  • 构造函数在对象创建时只被调用一次,而方法可以被调用任意次数。

现在,让我们给出在创建对象或实例时调用的构造函数的语法。

class Geek
{   
  .......

  // A Constructor
  new Geek() {}

  .......
}

// We can create an object of the above class
// using the below statement. This statement
// calls above constructor.
Geek obj = new Geek(); 

建造师的需求

想象一个盒子。如果我们谈论一个box类,那么它将有一些类变量(比如长度、宽度和高度)。但是,当涉及到创建它的对象时(即盒子现在将存在于计算机的内存中),那么盒子就可以在没有为其尺寸定义值的情况下存在。答案是否定的。 因此,构造函数用于在对象创建时为类变量赋值,要么由程序员显式完成,要么由Java本身(默认构造函数)完成。

什么时候调用构造函数?

每次使用 新的 关键字,至少调用一个构造函数(可以是默认构造函数)来为 数据成员 同班同学。

编写构造函数的规则如下:

  • 类的构造函数必须与它所在的类名同名。
  • Java中的构造函数不能是抽象的、最终的、静态的或同步的。
  • 访问修饰符可以在构造函数声明中使用,以控制其访问,即哪个类可以调用构造函数。

到目前为止,我们已经了解到构造函数用于初始化对象的状态。喜欢 方法 ,构造函数还包含一组在创建对象时执行的语句(即指令)。

Java中的构造函数类型

现在是讨论构造函数类型的正确时机,因此java中主要有两种类型的构造函数:

  • 无参数构造函数
  • 参数化构造函数

1.无参数构造函数

没有参数的构造函数称为默认构造函数。如果我们不在类中定义构造函数,那么编译器会创建 默认构造函数(不带参数) 为了班级。如果我们编写一个有参数或没有参数的构造函数,那么编译器不会创建默认构造函数。

注: 默认构造函数根据类型为对象提供默认值,如0、null等。

例子:

JAVA

// Java Program to illustrate calling a
// no-argument constructor
import java.io.*;
class Geek {
int num;
String name;
// this would be invoked while an object
// of that class is created.
Geek() { System.out.println( "Constructor called" ); }
}
class GFG {
public static void main(String[] args)
{
// this would invoke default constructor.
Geek geek1 = new Geek();
// Default constructor provides the default
// values to the object like 0, null
System.out.println(geek1.name);
System.out.println(geek1.num);
}
}


输出

Constructor called
null
0

2.参数化构造函数

具有参数的构造函数称为参数化构造函数。如果我们想用自己的值初始化类的字段,那么就使用参数化构造函数。

例子:

JAVA

// Java Program to Illustrate Working of
// Parameterized Constructor
// Importing required inputoutput class
import java.io.*;
// Class 1
class Geek {
// data members of the class.
String name;
int id;
// Constructor would initialize data members
// With the values of passed arguments while
// Object of that class created
Geek(String name, int id)
{
this .name = name;
this .id = id;
}
}
// Class 2
class GFG {
// main driver method
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek( "adam" , 1 );
System.out.println( "GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
}
}


输出

GeekName :adam and GeekId :1

记住:构造函数是否返回任何值?

构造函数中没有“return value”语句,但构造函数返回当前类实例。我们可以在构造函数中写入“return”。

现在,最重要的主题是将OOP与构造函数紧密结合起来,称为构造函数重载。就像方法一样,我们可以重载构造函数以不同的方式创建对象。编译器根据参数的数量、类型和顺序来区分构造函数。

例子:

JAVA

// Java Program to illustrate constructor overloading
// using same task (addition operation ) for different
// types of arguments.
import java.io.*;
class Geek
{
// constructor with one argument
Geek(String name)
{
System.out.println( "Constructor with one " +
"argument - String : " + name);
}
// constructor with two arguments
Geek(String name, int age)
{
System.out.println( "Constructor with two arguments : " +
" String and Integer : " + name + " " + age);
}
// Constructor with one argument but with different
// type than previous..
Geek( long id)
{
System.out.println( "Constructor with one argument : " +
"Long : " + id);
}
}
class GFG
{
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments
// Invoke the constructor with one argument of
// type 'String'.
Geek geek2 = new Geek( "Shikhar" );
// Invoke the constructor with two arguments
Geek geek3 = new Geek( "Dharmesh" , 26 );
// Invoke the constructor with one argument of
// type 'Long'.
Geek geek4 = new Geek( 325614567 );
}
}


输出

Constructor with one argument - String : Shikhar
Constructor with two arguments :  String and Integer : Dharmesh 26
Constructor with one argument : Long : 325614567

为了深入了解施工人员,有两个概念被广泛使用,如下所示:

本文由 尼茨海伦德拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享