dcsimg
Confusion with Constructors-Please respond
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   madhoo_g
Posted On:   Saturday, May 6, 2006 08:35 PM

Please look at the following code, i have to call two different constructors in ClassTwo,is there any better approach so that i can avoid passing dummyVariable in the other constructor.or is the current approach correct? public class ClassOne{ protected Vector items; protected Vector otherItems; public ClassOne(Vector items) { this.items = items; } /* here boolean dummyVariable is not needed but i'm passing as to differntiate these two constructors as per req,items and otherItems have to be in two different constructors*/ public ClassOne(Vector otherItems,boolean dummyVariable) { this.otherItems = otherItems; } } pu   More>>

Please look at the following code, i have to call two different constructors in ClassTwo,is there any better approach so that i can avoid passing dummyVariable in the other constructor.or is the current approach correct?


public class ClassOne{

protected Vector items;
protected Vector otherItems;
public ClassOne(Vector items) {
this.items = items;
}

/* here boolean dummyVariable is not needed but i'm passing as to differntiate these two constructors
as per req,items and otherItems have to be in two different constructors*/

public ClassOne(Vector otherItems,boolean dummyVariable)
{
this.otherItems = otherItems;
}

}


public class ClassTwo{
ClassOne co = new ClassOne(items)
ClassOne co = new ClassOne(otherItems,true);
}

Thanks for the any response.
-GoM

   <<Less

Re: Confusion with Constructors-Please respond

Posted By:   Guillaume_Masserey  
Posted On:   Sunday, May 7, 2006 02:40 PM

Hi,


The answer of WanderJan is correct. It seems that you are asking the soluce to a problem that you partially defined, you should have mentioned the use of ClassOne class for somebody to answer precisely.


In more of the answer of WanderJan, I propose you 4 other soluces; you will have to choose by yourself with your fully constrained problem.

* 1 array required:


  • use Vector class instead of ClassOne
  • use ClassOne with only constructor ClassOne( Vector items ) // soluce of WanderJan
  • use ClassOne with only constructor ClassOne( Vector items, boolean type )
    {

    if( type ) mOther = items; else mNotOther = items;

    mType = type;

    }


* 2 arrays required:


  • use ClassOne with only constructor ClassOne( Vector items, Vector itemsOther )
  • use ClassOne with only constructor ClassOne( Vector items, boolean other )
    {

    if( other ) mOther = items; else mNotOther = items;

    }



Hope it will help!


Bye

Re: Confusion with Constructors-Please respond

Posted By:   WarnerJan_Veldhuis  
Posted On:   Sunday, May 7, 2006 04:45 AM

If you are creating two objects "co" anyway, why do you have two vectors in your class?? One would be enough. Each object will have his own Vector.
About | Sitemap | Contact