简体   繁体   中英

How would one use this toString method for private variables via inheritance?

So typically, for toString methods I use, String.format ("%s, %s", variable, variable) and it works perfectly fine.

For a clearer example,

public class example1
{
    int number;
    String name;

    public void setup(String names, int numbers) {
        name = names;
        number = numbers;
    }

    public String toString() {
        return String.format("Name: %s\n Number: %s", name, number);
    }
}

And this works perfectly fine. However, I'm running into an issue when it comes to inheritance and private variables. Note the following example.

public class example1
{
    int number;
    private String name;

    public void setName(String names) {
        name = names;
    }

    public String getName() {
        return name;
    }
}

public class example2 extends example1
{
    public void setUP(int z) {
        number = z;
    }

    public String toString() {
        return String.format("Name: %s\n Number: %s", getName(), number);
    }
}

The number prints fine when using the toString method, however I only get a null for the name. I tried using the method name, but that wont work and I tried using n, but that wont work as well. I'd just like some clarification on how to go about doing this as it would be extremely helpful to know.

So, this is my main method

public class example3
{
    public static void main(String[] args) {
        example1 one = new example1();
        one.setName("Uri");
        example2 two = new example2();
        two.setUP(500);

        System.out.println(one.getName());
        System.out.println(two.toString());
    }
}

The number in the toString method prints just fine, its just that the name is null.

Apparent solution

public class example3
{
    public static void main(String[] args) {
        example1 one = new example1("Uri");
        test2 two = new test2(one.getName());
        two.setUP(500);

        System.out.println(one.getName());
        System.out.println(two.toString());
    }
}

This works well, however I'm not sure how elegant this is.

This has nothing to do with toString() . You haven't initialized the name member of your object, which is why it is printed as null.

I don't see any constructors in your code, so you should probably use the setName() method to initialize the name.

example2 ex = new example2();
ex.setUp(5);
ex.setName("John");
System.out.println(ex);

EDIT :

In the main you posted, you create two objects and set the name only in one of them. In the other, the same remains null. two.toString() doesn't print the name, since you only set the name in the one object.

Just add a constructor:

public class example1
{
    private String name;
    int number;

    public example1(String names) {
        name = names;
    }

    public void setName(String names) {
        name = names;
    }

    public String getName() {
        return name;
    }
}

public class example2 extends example1
{
    public example2(String names) {
        super(names);
    }

    public void setUP(int z) {
        number = z;
    }

    public String toString() {
        return String.format("Name: %s\n Number: %s", getName(), number);
    }
}

Doeing that, it calls the example1 constructor which initialize the name variable. Now, when calling getName() it returns the string passed through the constructor.

Now, in the main() :

public static void main (String [] args) {
    example1 one = new example1("Uri");// a name is required
    example2 two = new example2("Uri");// a name is required
    two.setUP(500);

    System.out.println(one.getName());
    System.out.println(two.toString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM