using System;
using System.Windows.Forms;class compositionTest
{
    static void Main(string[] args)
    {
        Employee e = new Employee("Bob", "Jones", 7, 24, 1949, 3, 12, 1988);//这里是实例化一个对象吧
        MessageBox.Show(e.ToemployessString(),"Testing Class Employee");
    }
}public class Employee 
{
    private string firstName;
    private string lastName;
    private Date birthDate;
    private Date hireDate;    public Employee(string first,string last,
        int birthMonth,int birthDay,int birthYear,
        int hireMonth,int hireDay,int hireYear)
    {
        firstName = first;
        lastName = last;
//下面这两个也是实例化两个对象吗?如果是的话改成Date birthDate = new Date(birthMonth, birthDay, birthYear);,在下面的hireDate.ToDateString()就提示错误。为什么?而上面的则可以这样啊。。
        birthDate = new Date(birthMonth, birthDay, birthYear);
        hireDate = new Date(hireMonth, hireDay, hireYear);
    }    public string ToemployessString()
    {
        return lastName + ", " + firstName +
            " Hired: " + hireDate.ToDateString() +
            " BirthDay:" + birthDate.ToDateString();
    }
}public class Date
{
    private int Month;
    private int Day;
    private int Year;    public Date(int theMonth,int theDay,int theYear)
    {
        if (theMonth > 0 && theMonth <= 12)
            Month = theMonth;
        else
        {
            theMonth = 1;
            Console.WriteLine("Month {0} invalid.Set to month 1.", +theMonth);
        }
        Year=theYear;
        Day = checkDay(theDay);
    }    public int checkDay(int testDay)
    {
        int[] dayperMonth ={ 0,31,28,31,30,31,30,31,31,30,31,30,31};        if (testDay > 0 && testDay <= dayperMonth[Month])
            return testDay;
        
        if (Month == 2 && testDay == 29 &&
            (Year % 400 == 0 || Year % 4 == 0 && Year % 100 != 0))
            return testDay;        Console.WriteLine("Day (0) invalid.Set to day 1.",testDay);        return 1;
    }    public string ToDateString()
    {
        return Month + "/" + Day + "/" + Year;
    }
}help me~~
[email protected]

解决方案 »

  1.   

    Employee e = new Employee("Bob", "Jones", 7, 24, 1949, 3, 12, 1988);//这里是实例化一个对象吧
    对呀
      

  2.   

    //下面这两个也是实例化两个对象吗?
    是的如果是的话改成Date birthDate = new Date(birthMonth, birthDay, birthYear);,
    改成这样就定义了一个新的局部变量birthDate,当然会出错了
    在下面的hireDate.ToDateString()就提示错误。为什么?而上面的则可以这样啊。。
            birthDate = new Date(birthMonth, birthDay, birthYear);
            hireDate = new Date(hireMonth, hireDay, hireYear);
        }