Z
Z
ZelibobA17062016-12-06 22:25:48
Java
ZelibobA1706, 2016-12-06 22:25:48

What's the best way to deal with a constructor?

What is the correct way to assign a new address through the add function to the variable second and then left, right, etc.?

public class First {
    Second second;
    public First() {
        second = null;
        add(second);
    }
    public void add(Second sec) {
        sec = new Second(11);
        sec.left = new Second(15);
    }
}
public class Second {
    public Second left;
    public Second right;
    public Integer value;
    public Second(Integer value) {
        left = null;
        right = null;
        this.value = value;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shockoway, 2016-12-07
@ZelibobA1706

public class First {
    Second second;
    public First() {
        second = add(second);
    }
    public Second add(Second sec) {
        sec = new Second(11);
        sec.left = new Second(15);
        return sec;
    }
}
class Second {
    public Second left;
    public Second right;
    public Integer value;
    public Second(Integer value) {
        this.value = value;
    }
}

And in general it is not absolutely clear to what so to complicate.
public class First {
    Second second;
    
    public First(int value, int left, int right) {
    	second = new Second(value, left, right);
    }
    public First(int value) {
    	second = new Second(value);
    }
}

class Second {
    public Second left;
    public Second right;
    public int value;
    
    public Second(int value, int left, int right) {
        this.value = value;
        this.left = new Second(left);
        this.right = new Second(right);
    }
    
    public Second(int value) {
    	this.value = value;
    }
    
}

A
aol-nnov, 2016-12-06
@aol-nnov

public class Second {
    public Second left;
    public Second right;
    public Integer value;
    public Second(Second left, Second right, Integer value) {
        this.left = left;
        this.right = right;
        this.value = value;
    }
    public Second(Integer value) {
        this(null, null, value);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question