在C#中创建和使用构造函数以及布尔方法

在C#中创建和使用构造函数以及布尔方法,c#,object,methods,constructor,boolean,C#,Object,Methods,Constructor,Boolean,我在C#作业中遇到了一些问题。下面的第一段代码包含一些分配说明。本质上,据我所知,我要创建两个构造函数,一个用于设置默认日期,另一个用于检查用户的日期。另外,还有一个SetDate方法似乎也在做同样的事情。这些似乎是多余的,但根据分配说明,两者都是必需的。我对面向对象编程非常陌生,所以我不知道如何将内容“传递”给构造函数,也不知道如何使用它并在主方法中调用它。第二段代码是我到目前为止所写的。所有的日期验证方法似乎都很好。但是,我不知道如何处理publicdate(intm,intd,inty)构

我在C#作业中遇到了一些问题。下面的第一段代码包含一些分配说明。本质上,据我所知,我要创建两个构造函数,一个用于设置默认日期,另一个用于检查用户的日期。另外,还有一个
SetDate
方法似乎也在做同样的事情。这些似乎是多余的,但根据分配说明,两者都是必需的。我对面向对象编程非常陌生,所以我不知道如何将内容“传递”给构造函数,也不知道如何使用它并在主方法中调用它。第二段代码是我到目前为止所写的。所有的日期验证方法似乎都很好。但是,我不知道如何处理
publicdate(intm,intd,inty)
构造函数和
SetDate
方法。每个人都应该做什么?还有,为什么我被要求使用整数变量M,D,Y,而我也被要求在上面声明月,日和年?任何有助于我理解如何使用此构造函数以及它与
SetDate
方法在功能上的联系和区别的见解都将不胜感激

 //Create a Date Class
 //This class holds:
private int Month;
private int Day;
private int Year;
    //Include the following constructors/methods. Include others/more if you 
    //need them.

    // Sets date to 1/1/1900
public Date()

    // Sets date to user’s input.
    // Checks to see the date is valid
    // If it isn’t valid, print message and set date to 1/1/1900
public Date(int M, int D, int Y)

     // Sets date to user’s input.
     // Checks to see the date is valid
     // If it isn’t valid, print message and set date to 1/1/1900
public Boolean SetDate(int M, int D, int Y)ere
//******************************************************************************

class Date
{
    private int Month;
    private int Day;
    private int Year;

    // Sets date to 1/1/1900
    public Date()
    {
        Month = 1;
        Day = 1;
        Year = 1900;
    }

    public Date(int M, int D, int Y)
    {
        Month = M;
        Day = D;
        Year = Y;
    }

    public Boolean SetDate(int M, int D, int Y)
    {

        int valDate = 0;

        Console.WriteLine("You will be prompted to enter three(3) numbers to represent a month, " +
            "day, and year. Only dates between 1/1/1900 and 12/31/2100 are valid.");

        Console.WriteLine("");

        while (valDate < 1)
        {
            Console.WriteLine("Enter the number for the month.");
            M = int.Parse(Console.ReadLine());
            Console.WriteLine("");

            Console.WriteLine("Enter the number for the day.");
            D = int.Parse(Console.ReadLine());
            Console.WriteLine("");

            Console.WriteLine("Enter the number for the year.");
            Y = int.Parse(Console.ReadLine());
            Console.WriteLine("");

            ValidateDate();

            if (ValidateDate())
            {
                DisplayDate();
                valDate++;
                return true;
            }
            else
            {

                Console.WriteLine("Please enter a valid date.");
                Console.WriteLine("");
                Month = 1;
                Day = 1;
                Year = 1900;
                return false;
            }


        }
        return false;
    }
        // Determines if date is valid.
    public Boolean ValidateDate()
    {
        ValidateMonth();
        ValidateDay();
        ValidateYear();

        if (ValidateMonth() && ValidateDay() && ValidateYear())
        {
            return true;
        }
        else
        {
            return false;
        }


    }
        // Determines if month is valid.
    public Boolean ValidateMonth()
    {
        if (Month >= 1 && Month <= 12)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        // Determines if year is valid.
    public Boolean ValidateYear()
    {
        if(Year >= 1900 && Year <= 2100)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        // Determines if day is valid
    public Boolean ValidateDay()
    {
        IsLeapYear();

        if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
        {
            if (Day >= 1 && Day <= 31)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
        {
            if (Day >= 1 && Day <= 30)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (Month == 2 && IsLeapYear())
        {
            if (Day >= 1 && Day <= 29)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (Month == 2 && !IsLeapYear())
        {
            if (Day >= 1 && Day <= 28)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    // Determine if year is a leap year
    public Boolean IsLeapYear()
    {
        if ((Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

        // Print date to screen in format M/D/Y
    public void DisplayDate()
    {
        Console.WriteLine(ShowDate());
    }

    public String ShowDate()
    {
        StringBuilder myStringBuilder = new StringBuilder();
        myStringBuilder.AppendFormat("{0} / {1} / {2}", Month, Day, Year);
        return (myStringBuilder.ToString());
    }

    static void Main(string[] args)
    {
        Date NewDate = new Date();
        NewDate.Date();

        Console.ReadLine();
    }
}
上课日期
{
私人整数月;
私人国际日;
私人国际年;
//将日期设置为1900年1月1日
公开日期()
{
月=1;
日=1;
年份=1900;
}
公开日期(整数M、整数D、整数Y)
{
月=M;
D=D;
年份=Y;
}
公共布尔设置日期(int M,int D,int Y)
{
int valDate=0;
Console.WriteLine(“系统将提示您输入三(3)个数字以表示一个月,”+
“日期和年份。只有1900年1月1日至2100年12月31日之间的日期才有效。”);
控制台。写线(“”);
while(valDate<1)
{
Console.WriteLine(“输入月份的数字”);
M=int.Parse(Console.ReadLine());
控制台。写线(“”);
Console.WriteLine(“输入当天的号码”);
D=int.Parse(Console.ReadLine());
控制台。写线(“”);
Console.WriteLine(“输入年份编号”);
Y=int.Parse(Console.ReadLine());
控制台。写线(“”);
ValidateDate();
if(ValidateDate())
{
显示日期();
valDate++;
返回true;
}
其他的
{
Console.WriteLine(“请输入有效日期”);
控制台。写线(“”);
月=1;
日=1;
年份=1900;
返回false;
}
}
返回false;
}
//确定日期是否有效。
公共布尔ValidateDate()
{
ValidateMonth();
ValidateDay();
验证eyear();
if(ValidateMonth()&&ValidateDay()&&ValidateYear())
{
返回true;
}
其他的
{
返回false;
}
}
//确定月份是否有效。
公共布尔ValidateMonth()
{

如果(月>=1&&Month=1900&&Year=1&&Day=1&&Day=1&&Day=1&&Day如果您想使用date创建一些实际的应用程序,那么我建议查看系统命名空间中的DateTime结构。该函数将返回true或false,无论输入是否有效

然而,看起来你正在做一些编程练习,所以在这种情况下,我的答案是:构造函数中的参数可能会导致无效对象——在你的例子中是无效日期——这不是很好。这是因为一旦调用构造函数,你将以某种方式拥有一个对象,除非你在构造函数中抛出异常然而,如果您仍然希望这样,那么您需要在类/结构中有一个名为“IsValid”的属性或公共变量,它告诉您是否使用构造函数给出了有效日期。 更好的选择是遵循DateTime方法-使用公共静态函数返回有效的日期对象。如下所示:

public bool TryParse(int m, int d, int y, out Date date)
{
   // validate

   // if valid then return Date object like that:
   date = new Date()
   {
      Month = m,
      Day = d,
      Year = y
   };
   return true;

   // Or like that:
   date = new Date(m, d, y);
   return true;

   // if not valid then return null (because have to return something)
   date = null;
   return false;
}

看起来您被要求创建一个方法和一个构造函数,它们做的是相同的事情。在这种情况下,简单的做法是让构造函数调用该方法

关于您的代码,我唯一要说的是,您显示的问题语句不需要在SetDate方法中收集输入。给定该语句,用户的输入似乎会在您的类之外收集

我不知道您对失败消息的要求是什么。这可能在它自己的方法中也是有意义的

以下是一个例子:

public class Date
{
    private int Month;
    private int Day;
    private int Year;

    public Date()
    {
        SetDefaultDate();
    }

    public Date(int M, int D, int Y)
    {
        SetDate(M, D, Y);
    }

    public void SetDate(int M, int D, int Y)
    {
        if (IsValidDate(M, D, Y))
        {
            Month = M;
            Day = D;
            Year = Y;
        }
        else
        {
            SetDefaultDate();
        }
    }

    private bool IsValidDate(int M, int D, int Y)
    {
        // validation logic.. return true if all parameters result in valid date
        // false if they do not.  If it is an invalid date print the failure message.

        return true;
    }

    private void SetDefaultDate()
    {
        Month = 1;
        Day = 1;
        Year = 1900;
    }
}

这是非常好的信息,谢谢。但是,对于我正在做的练习,我需要使用列出的构造函数或方法。我不确定在
公共日期(int M,int D,int Y)中放什么
构造函数,因为根据说明,在我看来,它与
SetDate
方法的作用完全相同。同样,这两个对象都是程序所必需的。这很有帮助。谢谢