java - How to make constructor that takes in 2 if there are 2 parameters, or 3 if there are 3 -


new java...

i have name class has:

private string firstname; private string middleinitial; private string lastname; 

as instance variables.

if had data had firstname , lastname, no middleinitial, how make constructor took 2 parameters instead of three?

you write constructor 2 parameters , constructor three

public yourclass(string firstname, string lastname) {    ... }  public yourclass(string firstname, string middleinitial, string lastname) {     ... } 

callers can choose use appropriate constructor based on needs.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -