Java的。构造函数链接。我为什么要初始化this()中的参数 [英] Java. Constructor chaining. Why should I initialize a parameter in this()

查看:104
本文介绍了Java的。构造函数链接。我为什么要初始化this()中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a Java sample presented below. And I have a question: why should I initialize the third parameter in this(name, sal, "Gurgaon"); in the third constructor public Employee1(String name, int sal) ? I mean why i can't put "addr" instead of initializing the third parameter--> this(name, sal, addr); ?







public class Employee1 {
    public String name;
    public int salary;
    public String address;

    //default constructor of the class
    public Employee1()
    {
        //this will call the constructor with String param
        this("Chaitanya");
        System.out.println("Default");
    }

    public Employee1(String name)
    {
        //call the constructor with (String, int) param
        this(name, 120035);
        System.out.println(name);
    }
    public Employee1(String name, int sal)
    {
        //call the constructor with (String, int, String) param
        this(name, sal, "Gurgaon");
        System.out.println(name + " " + sal);
    }
    public Employee1(String name, int sal, String addr)
    {
        this.name=name;
        this.salary=sal;
        this.address=addr;
        System.out.println(name + " "+ sal + " " + addr);
    }

    public static void main(String[] args)
    {
        Employee1 obj = new Employee1();
    }
}





我的尝试:



我是Java的初学者。我用谷歌搜索,但找不到答案。



What I have tried:

I'm a beginner in Java. And I have Googled, but couldn't find an answer.

推荐答案

首先,使用调试器,Java提供了很好的调试选项。

如果您正在使用,任何编辑工具,如eclipse或其他编辑工具都支持调试。如果您想学习这些应用程序正在使用的java内部调试工具,请查看jdb。

使用调试工具,您可以检查每一行的行为并观察结果。



我已经添加了一些调试信息供您理解;运行代码逐个查看并查看结果。



First of all, use debugger, Java offers nice option for debugging.
If you are using, any editing tools, like, eclipse, or others there are support for debugging. If you want to learn java internal debugging tool that is being used by those application, take a look at jdb.
With debugging tool you can examine each line behavior and observe the result.

I have added some debug information for you to understand; Run the code go through one by one and see the result.

public class Employee1 {
  public String name;
  public int salary;
  public String address;

  //default constructor of the class
  public Employee1()
  {
      System.out.println("0 arguments");
      this("Chaitanya");
      System.out.println("Default");
  }

  public Employee1(String name)
  {
      //call the constructor with (String, int) param
      System.out.println("1 arguments");
      this(name, 120035);
      System.out.println(name);
  }
  public Employee1(String name, int sal)
  {
    System.out.println("2 arguments");
      //call the constructor with (String, int, String) param
      this(name, sal, "Gurgaon");
      System.out.println(name + " " + sal);
  }
  public Employee1(String name, int sal, String addr)
  {
      System.out.println("3 arguments");
      this.name=name;
      this.salary=sal;
      this.address=addr;
      System.out.println(name + " "+ sal + " " + addr);
  }

  public static void main(String[] args)
  {
      Employee1 obj = new Employee1();
  }
}





根据您当前的班级创建对象的其他几个例子



Few other example of creating object based on your current class

obj = new Employee1("Navin"); // you are giving a name and your constructor is setting up rest of the default values
obj = new Employee1("Navin", 3000); // name and salary, others are default
obj = new Employee1("Navin", 3000, "Australia"); // name, salary and location; no default value





您的问题



To your question

引用:

在第三个构造函数public Employee1(String name,int sal)?我的意思是为什么我不能把addr而不是初始化第三个参数 - >这(name,sal,addr); ?

in the third constructor public Employee1(String name, int sal) ? I mean why i can't put "addr" instead of initializing the third parameter--> this(name, sal, addr); ?





这是专为学习目的而设计的。一旦你明白了这一点,你将不再需要这样做。



This is designed for learning purpose. Once you understand the point of this, you won't have to deal this exactly this way anymore.


这篇关于Java的。构造函数链接。我为什么要初始化this()中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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