Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们可以在servlet中创建非参数化构造函数吗?_Java_Servlets_Constructor - Fatal编程技术网

Java 我们可以在servlet中创建非参数化构造函数吗?

Java 我们可以在servlet中创建非参数化构造函数吗?,java,servlets,constructor,Java,Servlets,Constructor,我现在知道的: Servlet的实例首先由容器通过反射创建,并使用无参数构造函数 然后调用参数化init方法 还有人建议,我们不应该在servlet类中创建构造函数,因为它没有任何用处。我同意这一点 比方说,我在servlet类中创建了一个无参数构造函数,并从中调用了一个参数化构造函数。我的问题是,集装箱会叫它吗 public class DemoServlet extends HttpServlet{ public DemoServlet() { this(1);

我现在知道的:

  • Servlet的实例首先由容器通过反射创建,并使用
    无参数构造函数
  • 然后调用
    参数化init
    方法
还有人建议,我们不应该在servlet类中创建构造函数,因为它没有任何用处。我同意这一点

比方说,我在servlet类中创建了一个无参数构造函数,并从中调用了一个参数化构造函数。我的问题是,集装箱会叫它吗

public class DemoServlet extends HttpServlet{  
   public DemoServlet() {
      this(1);
   }
   public DemoServlet(int someParam) {
      //Do something with parameter
   }
}
容器是否会调用
DemoServlet()
,如果我们在其中放入一些初始化内容,它将被执行?我的猜测是肯定的,但这只是基于我的理解的猜测

出于好奇,我这样问可能很没用。

DemoServlet()
将被调用(因为您正在覆盖HttpServlet中定义的无参数构造函数(这是一个无操作构造函数)

但是,将不会调用另一个
DemoServlet(int arg)

DemoServlet()
将被调用(因为您正在覆盖HttpServlet中定义的无arg构造函数(这是一个无操作构造函数)

但是,将不会调用另一个
DemoServlet(int arg)

DemoServlet()
将被调用(因为您正在覆盖HttpServlet中定义的无arg构造函数(这是一个无操作构造函数)

但是,将不会调用另一个
DemoServlet(int arg)

DemoServlet()
将被调用(因为您正在覆盖HttpServlet中定义的无arg构造函数(这是一个无操作构造函数)


但是,将不会调用另一个
DemoServlet(int arg)

您的猜测是正确的。DemoServlet()将被容器调用,其中的任何初始化代码都将被执行——即使初始化是通过构造函数链接完成的,事实上,这是一种进行依赖项注入并创建线程安全servlet的好方法,该servlet是可测试的

public class DemoServlet extends HttpServlet
{
   private final someParam; //someParam is final once set cannot be changed

   //default constructor called by the runtime.
   public DemoServlet()
   {
       //constructor-chained to the paramaterized constructor
       this(1);
   }

   //observe carefully that this paramaterized constructor has only
  //package-level visibility. This is useful for being invoked through your
  //  unit and functional tests which would typically reside within the same 
  //package. Would also allow your test code to inject required values to 
 //verify behavior while testing.
   DemoServlet(int someParam)
   {
      this.param = param
   }

   //... Other class code...
}

您的猜测是正确的将被容器调用,其中的任何初始化代码都将被执行——即使初始化是通过构造函数链接完成的,事实上,这是一种进行依赖项注入并创建线程安全servlet的好方法,该servlet是可测试的

public class DemoServlet extends HttpServlet
{
   private final someParam; //someParam is final once set cannot be changed

   //default constructor called by the runtime.
   public DemoServlet()
   {
       //constructor-chained to the paramaterized constructor
       this(1);
   }

   //observe carefully that this paramaterized constructor has only
  //package-level visibility. This is useful for being invoked through your
  //  unit and functional tests which would typically reside within the same 
  //package. Would also allow your test code to inject required values to 
 //verify behavior while testing.
   DemoServlet(int someParam)
   {
      this.param = param
   }

   //... Other class code...
}

您的猜测是正确的将被容器调用,其中的任何初始化代码都将被执行——即使初始化是通过构造函数链接完成的,事实上,这是一种进行依赖项注入并创建线程安全servlet的好方法,该servlet是可测试的

public class DemoServlet extends HttpServlet
{
   private final someParam; //someParam is final once set cannot be changed

   //default constructor called by the runtime.
   public DemoServlet()
   {
       //constructor-chained to the paramaterized constructor
       this(1);
   }

   //observe carefully that this paramaterized constructor has only
  //package-level visibility. This is useful for being invoked through your
  //  unit and functional tests which would typically reside within the same 
  //package. Would also allow your test code to inject required values to 
 //verify behavior while testing.
   DemoServlet(int someParam)
   {
      this.param = param
   }

   //... Other class code...
}

您的猜测是正确的将被容器调用,其中的任何初始化代码都将被执行——即使初始化是通过构造函数链接完成的,事实上,这是一种进行依赖项注入并创建线程安全servlet的好方法,该servlet是可测试的

public class DemoServlet extends HttpServlet
{
   private final someParam; //someParam is final once set cannot be changed

   //default constructor called by the runtime.
   public DemoServlet()
   {
       //constructor-chained to the paramaterized constructor
       this(1);
   }

   //observe carefully that this paramaterized constructor has only
  //package-level visibility. This is useful for being invoked through your
  //  unit and functional tests which would typically reside within the same 
  //package. Would also allow your test code to inject required values to 
 //verify behavior while testing.
   DemoServlet(int someParam)
   {
      this.param = param
   }

   //... Other class code...
}


您是否尝试过测试您的代码?是的,您定义的默认构造函数将被执行。如果您没有定义,编译器将为您生成一个空的默认构造函数。在这两种情况下,默认构造函数都会被调用。我想在询问之前,这里没有做过任何测试(很遗憾,我现在没有下载任何IDE。我正在下载它。我以后肯定会测试它。您是否努力测试代码?是的,您定义的默认构造函数将被执行。如果您没有定义,编译器将为您生成一个空的默认构造函数。在任何情况下,都会调用默认构造函数。).我想在询问之前,这里没有做过任何测试(很遗憾,我现在没有下载任何IDE。我正在下载它。我以后肯定会测试它。您是否努力测试代码?是的,您定义的默认构造函数将被执行。如果您没有定义,编译器将为您生成一个空的默认构造函数。在任何情况下,都会调用默认构造函数。).我想在询问之前,这里没有做过任何测试(很遗憾,我现在没有下载任何IDE。我正在下载它。我以后肯定会测试它。您是否努力测试代码?是的,您定义的默认构造函数将被执行。如果您没有定义,编译器将为您生成一个空的默认构造函数。在任何情况下,都会调用默认构造函数。).我想在询问之前,这里没有做过任何测试(很遗憾,我现在没有下载任何IDE。我正在下载它。我以后肯定会测试它。谢谢你的输入!谢谢你的输入!谢谢你的输入!谢谢你的输入!谢谢你的输入!谢谢你的输入!谢谢你的输入!谢谢你的输入!谢谢你的输入!