简体   繁体   English

C#简单构造函数重载

[英]C# simple constructor overload

I am working on a very simple overloaded constructor exercise, but am running into a no overload taking for parameters found. 我正在做一个非常简单的重载构造函数练习,但是遇到了没有重载的发现参数的情况。 I thought I have done this correctly, but maybe I am doing something wrong? 我以为我做对了,但是也许我做错了什么?

   namespace A3_Date_StudentWork
    {
        class MyDate
        {      


            private int newMonth;
            private int newDay;
            private int currentMonth;
            private int currentDay;

            public MyDate(int month, int day)
            {
               newMonth = month;
               newDay = day;
            }

            public MyDate(int month, int day, int cmonth, int cday)
            {
                newMonth = month;
                newDay = day;
                currentMonth = cmonth;
                currentDay = cday;
            }
        }
    }

Here is what my new objects look like from main 这是我的主要新对象的样子

    MyDate today = new MyDate(todayMonth, todayDay);

    MyDate bday = new MyDate(bdayMonth, bdayDay);

    MyDate combine = new MyDate(bdayMonth, bdayDay, todayMonth, todayDay);

It is pretty simple program, all of my variables are int. 这是一个非常简单的程序,我所有的变量都是int。 My entire Main: 我整个主:

    static void Main(string[] args)
    {

        int todayMonth = 0;
        int todayDay = 0;
        int bdayMonth = 0;
        int bdayDay = 0;


        Console.WriteLine("What is today's month?");
        Int32.TryParse(Console.ReadLine(), out todayMonth);
       // Console.WriteLine(todayMonth);

        Console.WriteLine("What is today's date?");
        Int32.TryParse(Console.ReadLine(), out todayDay);
       // Console.WriteLine(todayDay);

        Console.WriteLine("What is your birth month?");
        Int32.TryParse(Console.ReadLine(), out bdayMonth);
       // Console.WriteLine(bdayMonth);

        Console.WriteLine("What is your birth date?");
        Int32.TryParse(Console.ReadLine(), out bdayDay);
       // Console.WriteLine(bdayDay);

        MyDate today = new MyDate(todayMonth, todayDay);
        MyDate bday = new MyDate(bdayMonth, bdayDay);
        MyDate combine = new MyDate(bdayMonth, bdayDay, todayMonth, todayDay);

And here is the base class with the constructors 这是带有构造函数的基类

    {
class MyDate
{      


    private int newMonth;
    private int newDay;
    private int currentMonth;
    private int currentDay;

    public MyDate(int month, int day)
    {
       newMonth = month;
       newDay = day;
    }

    public MyDate(int month, int day, int cmonth, int cday)
    {
        newMonth = month;
        newDay = day;
        currentMonth = cmonth;
        currentDay = cday;
    }

The base class is literally the class MyDate 基类实际上是MyDate类

Just a piece of advice here in terms of technical. 这里只是技术方面的建议。 When you have multiple constructors you should from one constructor call the other one if possible by passing the values. 当您有多个构造函数时,应尽可能通过传递值从一个构造函数中调用另一个。 This is also known as 'Constructor Chaining'. 这也称为“构造函数链接”。

Here's an example based on your code: 这是一个基于您的代码的示例:

class MyDate
{
    private int newMonth;
    private int newDay;
    private int currentMonth;
    private int currentDay;

    public MyDate(int month, int day)
       : this(month, day, 0, 0)
    { }

    public MyDate(int month, int day, int cmonth, int cday)
    {
        newMonth = month;
        newDay = day;
        currentMonth = cmonth;
        currentDay = cday;
    }
}

In this simple case, when you call the constructor with 2 parameters (month and day) the other parameters will be 0 by default. 在这种简单情况下,当您使用2个参数(月和日)调用构造函数时,其他参数默认为0。 You can also make parameters nullable so you can assign a null value to it. 您还可以使参数可为空,以便可以为其分配空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM