开发者

What does "this()" do in a constructor?

开发者 https://www.devze.com 2022-12-22 06:45 出处:网络
I have two questions about the following code. 1. How to constructor the third constructor without using setter?

I have two questions about the following code. 1. How to constructor the third constructor without using setter? 2. what does this() do in the last constructor.

public class Person {

    private String name;
    private String address;

    Person(){}
    Person(String name){
        this.name = name;
    }
    Person(String address){
        //Person(java.lang.String) is already defined.
    }

    Person(String name,String address){
        this();
        this.name = name;
        this.address = address;
    }
}

My solution for question is Person(Object address){ this.address = (String)address; } However, i am not sure about this.

and i think this(); in t开发者_如何学Gohe last constructor calls constructor Person(){}, but if it does, is it mean that two Person objects are created when i call

Person p = new Person("myName","myAddress");

Thanks!!!


The problem with Person(String name) and Person(String address) is that you can't have two constructors with the same parameters. The compiler will not know which one to call when you want to call something like this: new Person("Joe Blow");

You could do something like this instead:

Person(String name)
{
    this.name = name;
}
Person(String name, String address)
{
    this(name);
    this.address = address;
}

The "this()" in your last constructor is just telling that constructor to call the default constructor as part of the process of constructing the object. It does not create two objects, it just runs the code in the def. constructor, which in your case, does nothing.


Something to think about though - do you WANT a Person object created that doesn't have a name or address? If you don't, why offer those constructors (not that you can anyway - they have the same parameters, so the compiler can't differentiate them)? I would think every person would have a name. Maybe someone wouldn't have an address.

Think about your object before creating your constructors.

Another possibility is:

public class Person {

    private String name;
    private String address;

    Person(String name) {
        this(name, "");
    }

    Person(String name, String address) {
        this.name = name;
        this.address = address;

        // TODO - Other initializations.
    }
}


Use factory methods to construct this object

public class Person {

    private String name;
    private String address;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public static Person createHomelessPerson ( String name )
    {
        return new Person( name, null );
    }

    public static Person createNamelessPerson ( String address )
    {
        return new Person( null, address );
    }
}


  1. Sorry didn't understood the question
  2. Yes, this() calls first constructor. But don't worry, two instances will not be created! Actually creating new instance is done in two parts: memory allocation, calling constructor. So if you call internally another constructor it will just execute that code. constructor itself is just a special method, it doesn't create instance


You are right about the last statement, but two Person objects are not created. Internally, this is just like a method call. I don't understand what you're trying to achieve with this code. To invoke the last constructor, new Person("1", "2"), or internally this("1", "2") in a constructor. There are also no setters here. Setters are methods like setName(String), setAddress(String) and so on. Getters are methods like String getName(), String getAddress().

As another answer said, you also can't have two constructors with the same argument types. Just create one constructor with everything you need to set in it.


this calls the Person() but two objects are not created.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号