Java Basics - object construction

1. Overload

  Some class has multiple constructors. For example, a configuration may be an empty StringBuilder object:

StringBuilder message = new StringBuilder();

  Alternatively, an initial character string can be specified:

StringBuilder message = new StringBuilder("Hello World!");

  This feature is called overloading (overloading). If a plurality of methods (for example, the StringBuilder constructor method) have the same name but different parameters, it will produce a heavy load. The compiler must be selected which method the concrete implementation, the method to select the corresponding value type is used to match it with a particular type of call with the parameter of each method of analysis method. If the compiler can not find matching parameters, a compile-time error because there is no match at all, or none (referred to as overload resolution (overloading resolution) this process) better than the other. It is noteworthy that, Java allows you to override any method, not just the constructor method.

2. Default Domain initialization

  If not explicitly given in the domain Initialization constructor, it will automatically be given a default value: the value is 0, the Boolean value is false, the application target is null. However, only a lack of programming experience people will do so. Indeed, it is not clear if the domain is initialized, it will affect the readability of the code. For example, the article on the Employee class, assumed not to initialize certain fields in the constructor, the default will be the name and sex field is initialized to null, the age field is initialized to 0. But this is not a good programming practice. If at this time, we call the getName method or getSex method, you will get a reference to a null value, this should not what we want results.

3. No argument constructor

  Many classes include a constructor with no arguments, the object is no argument constructor creates, its status is set to appropriate default values. If you do not write the constructor write a class, then the system will provide a no-argument constructor. This constructor all the strength of field is set to the default value.

  Note: If the class is provided at least one constructor, but did not provide a no-argument constructor, then when constructing an object if no parameters will be considered illegal. For example, it provides only one Employee class constructor:

package jackWe;

public class Employee {
    private String name;
    private int age;
    private String sex;

    public Employee(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public  int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    
    public void getOneHeight() {
        System.out.println("姓名:"+ this.name + ";");
        System.out.println("年龄:"+ this.age + ";");
        System.out.println("性别:"+ this.sex + ";");
        System.out.println("身高:"+ (int)(Math.random() * 200) + "cm。");
    }
}

  For this class, the default constructor of employees would be illegal. That is, call

The Employee = the Employee new new the Employee (); // create will generate an error

  It will generate an error.

  Warning: Remember that only if the class does not provide any constructor, the system will provide a default constructor. If the writing class, gives a constructor, even if it is very simple, to get the user of this class can construct an example in the following manner:

  new  className()

  You must provide a default constructor (i.e. the constructor with no arguments). Of course, if all the desired field is given a default value, the following format:

public the Employee () {
         // default constructor 
    }

4. The call another constructor

  Keyword this references the implicit method parameters. However, the keyword there is another meaning. If the first statement as the this constructor (...), then the constructor calls this constructor another in the same class, for example the following:

public the Employee () {
         the this ( "Jack", 18 is, "M");   // call another constructor 
    }

The initialization block

  As already mentioned two methods initialization data fields:

  ①, the constructor set value;

  ②, the assignment in a statement.

  In fact, Java there is a third mechanism, became initialization block. In a class declaration, it may comprise a plurality of code blocks. As long as the object class structure, these blocks will be executed.

  Because there are many ways to initialize the data field, so all paths listed construction process can be quite confusing. The following is a specific processing step of calling the constructor:

  ①, all data fields are initialized to default values ​​(0, false, null);

  ②, in order of appearance in the class declaration, all domains are sequentially executed initialization statements and initialization block;

  ③, a first row if the constructor calls a second configuration, a second body configured to execute;

  ④, the implementation body of this constructor.

Guess you like

Origin www.cnblogs.com/JackWeTa/p/11449541.html