Feature request - simpler constructors

Derek Parnell derek at psych.ward
Fri Sep 21 05:03:35 PDT 2007


On Fri, 21 Sep 2007 08:43:41 +0100, Janice Caron wrote:

> I tried to look at tango source code, but I can't seem to connect to
> dsource.org right now. Hopefully that's a transient problem.
> 
> I know that Sun's official Java tutorial (see
> http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html)
> tells you to do exactly what I've been talking about. I quote:
> 
>  public Bicycle(int startCadence, int startSpeed, int startGear) {
>      gear = startGear;
>      cadence = startCadence;
>      speed = startSpeed;
>  }

It's just that I'm more likely to do something like ...
 
  this(int startCadence, int startSpeed, int startGear) {
      if (startGear > 0 && startGear <= Bicycle.maxGear)
          this.gear = startGear;
      else
          this.gear = Bicycle.defaultGear; // or exception

      if (startCadence > 0 && startCadence <= Bicycle.maxCadence)
          this.Cadence = startCadence;
      else
          this.Cadence = Bicycle.defaultCadence; // or exception

      if (startSpeed > 0 && 
             startSpeed <= Bicycle.maxSpeed(this.gear, this.Cadence))
          this.Speed = startSpeed;
      else
          this.Speed = Bicycle.calcSpeed(this.gear, this.Cadence);

  }

than just a simple set of assignments.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list