Multiple Constructors with One Parameter

So, at school we had the task of making a car in OOP, so far it has been pretty easy and straightforward. But now I need to create four constructors, one with no parameters, two with one parameter and one with two parameters.

As far as I know how overloading works, it checks the number of parameters that you supply to it, and then checks which constructor it should use. Since the two constructors are the same, both accept ints, only one must change the number or gears, and the other must change the maximum speed.

Is there a way to do this without passing an extra parameter?

+6
source share
5 answers

As far as I know how overloading works, it checks the number of parameters that you supply to it, and then checks which constructor it should use.

No, overloading is not based solely on the number of parameters - it is also based on their types.

However:

Since two of the constructors are the same, both take strings

This is problem. You cannot declare two constructors as follows:

public Foo(string x) { } public Foo(string y) { } 

These signatures are facing overload.

I suggest having public static factory methods, so you can specify what you are trying to create:

 public static Foo FromGears(string gears) { return new Foo(...); } public static Foo FromMaximumSpeed(string maxSpeed) { return new Foo(...); } 

Then you can have one constructor that accepts both values, and a default value that is missing when calling the constructor from the factory method.

However, there are two other oddities in your description:

  • You use strings for two values ​​that sound like they should be numbers (or maybe one number and one number and one)
  • You talk about declaring constructors, but then use the word β€œmodify” as if they were going to modify an existing instance. This does not make sense - constructors are used to create new objects.

EDIT: Okay, now we know a little more, here is what I mean:

 public class Car { private const int DefaultGears = 5; private const int DefaultTopSpeed = 180; private readonly int gears; private readonly int topSpeed; public Car(int gears, int topSpeed) { this.gears = gears; this.topSpeed = topSpeed; } public static Car CreateWithGears(int gears) { return new Car(gears, DefaultTopSpeed); } public static Car CreateWithTopSpeed(int topSpeed) { return new Car(topSpeed, DefaultGears); } } 

Note that you can use optional parameters and named arguments for this too in C # 4:

 public class Car { public const int DefaultGears = 5; public const int DefaultTopSpeed = 180; private readonly int gears; private readonly int topSpeed; public Car(int gears = DefaultGears, int topSpeed = DefaultTopSpeed) { this.gears = gears; this.topSpeed = topSpeed; } } 

Then:

 Car car = new Car(gears: 4); Car car = new Car(topSpeed: 30); 

I would not recommend this at all, although, of course, you are not yet relatively new to this language. There are various subtleties around optional parameters.

+12
source

Overloaded methods (and constructors) must differ in their signature. This means that they must have a different number of parameters or parameters having a different type (or both). Parameter names are not taken into account. This means that you cannot have two constructors having one string parameter.

Constructors cannot change anything but initialize the object. If you want to change the value, consider adding a method, such as ChangeGears(string gears) .

+1
source

In short, no. Constructors depend not only on the number of parameters, but also on their types.

 public Car(string gears) { } public Car(string maxSpeed) { } 

You cannot call it because they are essentially the same constructor, think about it when you pass a string, this is the value you pass, so the compiler has no idea which constructor to use: if you wanted to set the number of transfers at 6 or maximum speed up to 6, the call will be new Car("6") , as if ambiguous, is not it?

It cannot be said in the home spec that you have two constructors that take strings, since this is not possible. In fact, the only sensible way to achieve this would be to use static factory methods or, as you said, enter another parameter.

Looking at what you commented in other answers makes me believe that something like this might work for you, but it's hard to know exactly what your homework wants.

 public Car() { ... // Do your setting of your default values here (# of gears, max speed) } public Car(int n) { if( n > 10 ) { // Use n to set your max speed } else { // Use n to set your # of gears } } 

It seems unlikely that the car will have more than 10 gears, or its maximum speed will be less than 10, so this way you can use one constructor to set both your # gears and maximum speed. (Keep in mind that you asked that this probably won’t answer your homework, but it would be enough for IMO to use a constructor that can set the maximum speed and number of transfers without adding another parameter, assuming you have some kind of guarantee that the maximum speed of the car is> = 10, and the number of gears is <10)

+1
source

Each method (including constructors) has a signature. The signature consists of parameter types and their order. You cannot have two methods that have the same name and signature.

I assume the requirement that the parameters are strings is a misunderstanding. In this case, I would suggest that the number of transmissions is always an integer - best represented as an integer. And the maximum speed can be a fractional value - perhaps double or floating.

 public class Car { public Car( int gears ) {} public Car( float maxSpeed ) {} } 
+1
source
 public Car(string param, bool isGear) { if(isGear) gears = param; else maxSpeed = param; } 
0
source

Source: https://habr.com/ru/post/909855/


All Articles