Java构造函数使用详细指南

2021年3月15日09:07:32 发表评论 598 次浏览

构造函数用于初始化对象的状态。喜欢方法, 构造函数还包含陈述集(即说明)在创建对象时执行的操作。

构造需要

想想一个盒子。如果我们谈论盒子类, 那么它将有一些类变量(例如, 长度, 宽度和高度)。但是, 当要创建其对象时(例如, Box现在将存在于计算机的内存中), 那么可以存在一个没有为其尺寸定义值的盒子。答案是不。

因此, 在创建对象时, 可以使用构造函数将值分配给类变量, 这些值可以由程序员或由Java本身(默认构造函数)显式完成。

什么时候调用构造函数?

每次使用创建对象

new()

关键字至少调用一个构造函数(可以是默认构造函数)来为分配初始值

数据成员

同一class的

创建对象或实例时会调用构造函数。例如:

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();

编写构造函数的规则:

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

构造函数的类型

Java中有两种类型的构造函数:

参数构造函数:

没有参数的构造函数称为默认构造函数。如果我们没有在类中定义构造函数, 则编译器会创建

默认构造函数(无参数)

上课而且, 如果我们编写带有参数或无参数的构造函数, 则编译器不会创建默认的构造函数。

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

// 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

参数化构造函数:

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

// Java Program to illustrate calling of
// parameterized constructor.
import java.io.*;
  
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 GFG
{
     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"。

构造函数重载

像方法一样, 我们可以重载构造函数以不同方式创建对象。编译器根据参数数量, 参数类型和参数顺序来区分构造函数。

// 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

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

  • 构造函数的名称必须与它在其中定义的类的名称相同, 而对于Java中的方法则不必要。
  • 构造函数不返回任何类型, 而方法具有返回类型或虚空如果不返回任何值。
  • 构造函数在创建对象时仅被调用一次, 而方法可以被任意多次调用。

相关文章:

  • 构造器链接
  • 复制构造函数

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: