我的类里有三个构造:
public Employee(String n,double s,int year,int month,int day)
{
  name=n;
  salary=s;
  GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
  hireDay=calendar.getTime();
}
public Employee()
{}
第三个类要调用第一个类同时增加一个参数int x  请问这个类怎么写??谢谢!

解决方案 »

  1.   

    (更正)我的类里有三个构造器:
    public Employee(String n,double s,int year,int month,int day)
    {
      name=n;
      salary=s;
      GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
      hireDay=calendar.getTime();
    }
    public Employee()
    {}
    第三个构造器要调用第一个构造器同时增加一个参数int x  请问这个构造器怎么写??谢谢!
      

  2.   

    public Employee(... int x)

     this(n,s,year,month,day);
     处理x
    }
      

  3.   

    public Employee(String n,double s,int year,int month,int day,int x)
    {
      this(n,s,year,month,day);
      对x的处理...
    }
      

  4.   

    上面二位说的是对的,但有一点要注意
    this(....)这句必须放在该构造器的第一行
      

  5.   

    《Thinking In Java》:Calling constructors from constructorsWhen you write several constructors for a class, there are times when you’d like to call one constructor from another to avoid duplicating code. You can make such a call by by using the this keyword. Feedback
    Normally, when you say this, it is in the sense of “this object” or “the current object,” and by itself it produces the reference to the current object. In a constructor, the this keyword takes on a different meaning when you give it an argument list. It makes an explicit call to the constructor that matches that argument list. Thus you have a straightforward way to call other constructors: Feedback
    //: c04:Flower.java
    // Calling constructors with "this."
    import com.bruceeckel.simpletest.*;public class Flower {
      static Test monitor = new Test();
      int petalCount = 0;
      String s = new String("null");
      Flower(int petals) {
        petalCount = petals;
        System.out.println(
          "Constructor w/ int arg only, petalCount= "
          + petalCount);
      }
      Flower(String ss) {
        System.out.println(
          "Constructor w/ String arg only, s=" + ss);
        s = ss;
      }
      Flower(String s, int petals) {
        this(petals);
    //!    this(s); // Can't call two!
        this.s = s; // Another use of "this"
        System.out.println("String & int args");
      }
      Flower() {
        this("hi", 47);
        System.out.println("default constructor (no args)");
      }
      void print() {
    //! this(11); // Not inside non-constructor!
        System.out.println(
          "petalCount = " + petalCount + " s = "+ s);
      }
      public static void main(String[] args) {
        Flower x = new Flower();
        x.print();
        monitor.expect(new String[] {
          "Constructor w/ int arg only, petalCount= 47",
          "String & int args",
          "default constructor (no args)",
          "petalCount = 47 s = hi"
        });
      }
    } ///:~The constructor Flower(String s, int petals) shows that, while you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you’ll get a compiler error message.