Question

if i have group of classes let say class A,B,C and D and Class A initiate class B, class B initiate class C and class C initiate class D and their is arguments must be passed from A to D, what is the best way to passing??do i have to pass the arguments across all the classes i have ?? i tried this solution but i search for one easier.

class A
{
B b=new B(the_arguments);
}

class B
{
C c=new C(the_arguments);

}

class C
{
D d=new D(the_arguments);
}

thanks in advance .

Was it helpful?

Solution

Can you create a constructor in each successive class that takes a single argument of the previous class type? You'd need to make appropriate getters, or expose the arguments to the other classes (which wouldn't be too bad if they derived from each other).

class A
{
  B b = new B(this);
}

class B
{
  B(A a) { this.foo = a.foo; ... }  // Constructor
  C c = new C(this);
}

class C
{
  C(B b) { this.foo = b.foo; ... }  // Constructor
  D d = new D(this);
}

class D
{
  D(C c) { this.foo = c.foo; ... }  // Constructor
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top