📜  Java中使用静态块的构造函数重载

📅  最后修改于: 2022-05-13 01:55:24.925000             🧑  作者: Mango

Java中使用静态块的构造函数重载

在Java中,构造函数是类似于用于初始化对象状态的方法的代码块。在创建对象或实例时调用构造函数。每次使用new()关键字创建对象时,都会调用至少一个构造函数(可能是默认构造函数)来为数据成员分配初始值 属于同一类。

Java
// A Java program to demonstrates Constructor
class Emp {
    int id;
    String name;
 
    // this would be invoked while an object
    // of that class is created.
    public Emp() { System.out.println("GeeksforGeeks"); }
}
 
// Driver Code
public class GFG {
    public static void main(String[] args)
    {
        // this would invoke default constructor.
        Emp obj = new Emp();
    }
}


Java
// Java program to demonstrates
// Constructor Overloading
class Emp {
   
    int id;
   
    // Default Constructor
    public Emp() // Constructor1
    {
        System.out.println("Default Constructor");
    }
 
    // Parameterized Constructor
    public Emp(int id)
    {
        System.out.println("Parameterized Constructor");
    }
}
 
// Driver Code
public class GFG {
   
    public static void main(String[] args)
    {
        // Accessing parameterized constructor
        Emp obj = new Emp(10);
    }
}


Java
// Java program to demonstrates Constructor
// Overloading with static block
class Emp {
    int id, exp;
    String name;
    static String category;
 
    // Static block with category Developer
    static { category = "Developer"; }
   
    // Default Constructor
    public Emp()
    {
        System.out.println("-"
                           + "\t"
                           + "-"
                           + "\t"
                           + "-"
                           + "\t"
                           + "\t"
                           + "-");
    }
   
    // Parameterized Constructor with two arguments
    public Emp(int id, String name)
    {
        System.out.println(id + "\t" + name + "\t"
                           + category + "\t" + exp);
    }
   
    // Parameterized Constructor with three arguments
    public Emp(int id, String name, int exp)
    {
        System.out.println(id + "\t" + name + "\t"
                           + category + "\t" + exp);
    }
}
 
// Driver Code
public class GFG {
   
    public static void main(String[] args)
    {
        System.out.println("Id"
                           + "\t"
                           + "Name"
                           + "\t"
                           + "Category"
                           + "\t"
                           + "Exp");
       
        // Passing values to parameterized constructor with
        // two arguments
        Emp obj1 = new Emp(1863, "Kumar");
       
        // Passing values to parameterized constructor with
        // three arguments
        Emp obj2 = new Emp(1864, "ravi", 2);
       
        // Calling Default COnstructor
        Emp obj3 = new Emp();
    }
}


输出
GeeksforGeeks

构造函数重载:

像方法重载一样,我们也可以重载构造函数。在构造函数重载中,我们可以创建多个具有不同参数的构造函数,有时需要以不同的方式初始化一个对象。这可以使用构造函数重载来完成。

在此示例中,我们创建了两个具有相同类名的构造函数,每个构造函数的参数不同,并且要访问构造函数,我们通过对象类传递参数。当我们通过对象类传递值 10 时,将调用具有 int 参数的第二个构造函数,如果没有值通过对象类传递,则调用默认构造函数。

Java

// Java program to demonstrates
// Constructor Overloading
class Emp {
   
    int id;
   
    // Default Constructor
    public Emp() // Constructor1
    {
        System.out.println("Default Constructor");
    }
 
    // Parameterized Constructor
    public Emp(int id)
    {
        System.out.println("Parameterized Constructor");
    }
}
 
// Driver Code
public class GFG {
   
    public static void main(String[] args)
    {
        // Accessing parameterized constructor
        Emp obj = new Emp(10);
    }
}

输出:

Parameterized Constructor

使用静态块的构造函数重载

Java中的静态块用于类的静态初始化。静态块在类加载到内存时执行。静态块内的代码只执行一次。静态块还可以帮助减少代码行数。让我们了解为什么我们需要在Java中使用带有静态块的构造函数重载。假设一家公司雇佣了一些没有经验或两年经验的员工作为一个独特的角色开发人员。由于新聘用的候选人属于开发人员类别,我们可以使用静态块来执行此操作。

Java

// Java program to demonstrates Constructor
// Overloading with static block
class Emp {
    int id, exp;
    String name;
    static String category;
 
    // Static block with category Developer
    static { category = "Developer"; }
   
    // Default Constructor
    public Emp()
    {
        System.out.println("-"
                           + "\t"
                           + "-"
                           + "\t"
                           + "-"
                           + "\t"
                           + "\t"
                           + "-");
    }
   
    // Parameterized Constructor with two arguments
    public Emp(int id, String name)
    {
        System.out.println(id + "\t" + name + "\t"
                           + category + "\t" + exp);
    }
   
    // Parameterized Constructor with three arguments
    public Emp(int id, String name, int exp)
    {
        System.out.println(id + "\t" + name + "\t"
                           + category + "\t" + exp);
    }
}
 
// Driver Code
public class GFG {
   
    public static void main(String[] args)
    {
        System.out.println("Id"
                           + "\t"
                           + "Name"
                           + "\t"
                           + "Category"
                           + "\t"
                           + "Exp");
       
        // Passing values to parameterized constructor with
        // two arguments
        Emp obj1 = new Emp(1863, "Kumar");
       
        // Passing values to parameterized constructor with
        // three arguments
        Emp obj2 = new Emp(1864, "ravi", 2);
       
        // Calling Default COnstructor
        Emp obj3 = new Emp();
    }
}
输出
Id    Name    Category    Exp
1863    Kumar    Developer    0
1864    ravi    Developer    2
-    -    -        -

在上面的例子中,在带有两个参数的参数化构造函数中,我们从对象中传递了唯一的 id 和 name,在输出中,我们还可以看到 category 和 exp 也被打印出来,这是因为我们为如果没有向其传递任何值,类别 Developer 和Java会自动分配 0。在带有三个参数的参数化构造函数中,我们只传递了 id、name、exp 但类别也打印在输出中,这是因为类别位于静态块中。