+ 1

Is there a way to create a constructor with default settings but with costume modifiers?

what I'm talking about is create a constructor with a default settings like you can see in the code "Red" "SUV" with the numbers of "00-000-00" when creating the object, but if I need to keep the default settings but just change the color? is there a shorter version of what I wrote? https://code.sololearn.com/cDSy9Eaof7Z6/?ref=app sorry for bad English I'm writing this half a sleep

31st Jul 2017, 5:10 AM
Sam Miles
Sam Miles - avatar
4 odpowiedzi
+ 2
I just got a new idea!! Maybe this will be the shortest Use "" for blank information public Vehicle(String c, String t, String n){ this.setColor(c.length() == 0 ? "Red" : c); this.setType(t.length() == 0 ? "SUV" : t); this.setNumber(n.length() == 0 ? "00-000-00" : n); }
31st Jul 2017, 5:30 AM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
+ 1
Do it like this if it is clear that color is set first and next is kind of vehicle: Vehicle(String c, String t, String n){ //Initialize variables } Vehicle(String c, String t){ this(c, t, "00-000-00"); } Vehicle(String c){ this(c, "SUV", "00-000-00"); } Vehicle(){ this("red", "SUV", "00-000-00"); }
31st Jul 2017, 5:21 AM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
+ 1
what if it just the number changes?
31st Jul 2017, 5:27 AM
Sam Miles
Sam Miles - avatar
+ 1
@Orb_H you are a geniuse
31st Jul 2017, 7:48 AM
Sam Miles
Sam Miles - avatar