java构造函数顺序 [英] java constructor order

查看:187
本文介绍了java构造函数顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个java程序很容易和充满评论,所以你可以理解它fast.however,为什么在构造staff [1],程序首先去语句:

  this(Employee#+ nextId,s); 

然后转到对象初始化块,然后回到语句,如何confusion.why不它首先使用对象初始化块

  import java.util。*; 

public class ConstructorTest
{
public static void main(String [] args)
{
//用三个Employee对象填充staff数组
员工[] staff =新员工[3];

staff [0] = new Employee(Harry,40000);
staff [1] =新员工(60000);
staff [2] = new Employee();

//打印出所有Employee对象的信息
for(Employee e:staff)
System.out.println(name =+ e.getName()
+,id =+ e.getId()
+,salary =+ e.getSalary());
}
}

class Employee
{
//三个重载的构造函数
public Employee(String n,double s)
{
name = n;
salary = s;
}

public Employee(double)
{
//调用Employee(String,double)构造函数
this(Employee# nextId,s);
}

//默认构造函数
public Employee()
{
//名称初始化为 - 见下面
// salary not displayed set - 初始化为0
//在初始化块中初始化id
}

public String getName()
{
return名称;
}

public double getSalary()
{
return salary;
}

public int getId()
{
return id;
}

private static int nextId;

private int id;
private String name =; //实例字段初始化
private double salary;

//静态初始化块
static
{
Random generator = new Random();
//将nextId设置为0到9999之间的随机数
nextId = generator.nextInt(10000);
}

//对象初始化块
{
id = nextId;
nextId ++;
}
}


解决方案

this(Employee#+ nextId,s); 包括对超类构造函数的隐式调用,当然必须在子类的初始化程序块之前执行。 p>

使用实例初始化器通常是一个坏主意,因为它们不是众所周知的,不能做构造函数,混合两者会导致混乱。


This java program is easy and full of comment,so you can understand it fast.however,why in construct staff[1],the program first go to the statement:

this("Employee #" + nextId, s);

then go to the object initialization block,and then go back to the statement,how confusion.why not it first use the object initialization block

import java.util.*;

public class ConstructorTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];

      staff[0] = new Employee("Harry", 40000);
      staff[1] = new Employee(60000);
      staff[2] = new Employee();

      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName()
            + ",id=" + e.getId()
            + ",salary=" + e.getSalary());
   }
}

class Employee
{
   // three overloaded constructors
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }

   public Employee(double s)
   {
      // calls the Employee(String, double) constructor
      this("Employee #" + nextId, s);
   }

   // the default constructor
   public Employee()
   {
      // name initialized to ""--see below
      // salary not explicitly set--initialized to 0
      // id initialized in initialization block
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public int getId()
   {
      return id;
   }

   private static int nextId;

   private int id;
   private String name = ""; // instance field initialization
   private double salary;

   // static initialization block
   static
   {
      Random generator = new Random();
      // set nextId to a random number between 0 and 9999
      nextId = generator.nextInt(10000);
   }

   // object initialization block
   {
      id = nextId;
      nextId++;
   }
}

解决方案

Because this("Employee #" + nextId, s); includes an implicit call to the superclass constructor, which of course must be executed before the initializer block of the subclass.

Using instance initializers is generally a bad idea as they are not well known, cannot do anything more than constructors, and mixing both leads to confusion.

这篇关于java构造函数顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆