需要一些 C# 的重载信息

本文关键字:重载 信息 | 更新日期: 2023-09-27 18:34:23

im 在以下方面遇到问题,想看看是否有人可以帮助我。 详细信息在代码中,但是我试图重载构造函数并且参数名称给了我一个错误:

using system
    //blah, blah blah
        class Animal
        {
            private string name;
            private string race;
            private double _weight;
            private double _age;

        public Animal(string n,string r, double a, double w)
        {
            name = n;
            race = r;
            age = a;
            weight = w;
        }
            public double age
            {
                get
                {
                    return _age;
                }
                set
                {
                    if (value > 0 && value <= 25)
                    {
                        _age = value;
                    }
                    else
                    {
                        do
                        {
                            Console.WriteLine("Invalid Age!'nDog's age: " + value);
                            Console.Write("Please enter dog's age: ");
                            age = double.Parse(Console.ReadLine());
                        } while (_age < 0 && age > 25);
                    }
                }
            }
            public double weight
            {
                get
                {
                    return _weight;
                }
                set
                {
                    if (value > 0 && value <= 60)
                    {
                        _weight = value;
                    }
                    else
                    {
                        do
                        {
                            Console.WriteLine("Invalid Weight!'nDog's weight: " + value);
                            Console.Write("Please enter dog's weight: ");
                            weight = double.Parse(Console.ReadLine());
                        } while (_weight < 0 && _weight >= 60);
                    }
                }
            }
            public Animal()
            {
                Console.WriteLine("Invalid Age!" + "'nInvalid Weight!");
                Console.Write("'nInformation for the 1st dog : ");
                getdetails();
            }
            public void info()
            {
                Console.Write("'nInformation for the 2nd dog: ");
                Console.WriteLine("'nName: " + name + ", Race: " + race
                    + ", Age: " + age + ", Weight: " + weight);
                Console.ReadLine();
            }
            public void getdetails()
            {
                _age = 1;
                _weight = 1;
                Console.WriteLine("'nName: No Name, Race: No Race" + ", Age: " + _age + ", Weight: "
                    + _weight);
                Console.WriteLine();
            }


            public static void Main(String[] args)
            {
                Animal dog = new Animal();
                Animal dog2 = new Animal(name,race,age,weight);***//HERE'S THE ERROR!
//SO HERE ARE WHERE MY TROUBLES LIE. I CREATED A NEW OBJECT AND I AM TRYING TO PASS
THE ENTERED INFORMATION FROM THE USER TO ONE OF MY CONSTRUCTORS BUT 
IM HAVING A TOUGH TIME DOING IT. COULD SOMEBODY HELP PLEASE?***
                Console.Write("Please enter your dog's name: ");
                dog.name = Console.ReadLine();
                Console.Write("Please enter your dog's race: ");
                dog.race = Console.ReadLine();
                Console.Write("Please enter your dog's age: ");
                dog.age = double.Parse(Console.ReadLine());
                Console.Write("Please enter your dog's weight: ");
                dog.weight = double.Parse(Console.ReadLine());
                dog.info(); 

            }
        }
    }

现在我知道有很多东西不必写在这个代码中,但这就是我应该这样做的方式,我只是在参数方面遇到问题,请帮忙吗?

需要一些 C# 的重载信息

您似乎混淆了构造函数调用和函数调用 -> animal(name, race ...)只是代码中的一个函数,因此您无法直接调用它。 看看如何重载构造函数。 顺便说一句,很高兴知道您使用的是哪种语言^^

抱歉,我正在用我的C++帽子分析您的代码...在 C# 中,除了将正确的值作为参数传递之外,您所做的一切都是正确的。您正在尝试在定义/分配它们之前传递值,这就是您收到错误的原因。因此,对于 dog2,您可能希望这样做。

   public static void Main(String[] args)
            {
                //variable declation
                string name, race;
                double age, weight;
                //variable assigned user inputs
                Console.Write("Please enter your dog's name: ");
                name = Console.ReadLine();
                Console.Write("Please enter your dog's race: ");
                race = Console.ReadLine();
                Console.Write("Please enter your dog's age: ");
                age = double.Parse(Console.ReadLine());
                Console.Write("Please enter your dog's weight: ");
                weight = double.Parse(Console.ReadLine());
                // call of the overloaded constructor with the right parameters
                Animal dog2 = new Animal(name,race,age,weight);
                dog2.info(); 

            } 

我希望这有帮助,祝你好运:)

您的示例有两个Constructors,一个默认的无参数ConstructorAnimal()和一个需要四个参数Animal(string n, string r, double a, double w)ConstructorConstructor的签名会通知您预期parameter(s)的数量和type

Animal dog = new Animal();

上述方法毫无怨言地工作,因为您使用的是默认的无参数构造函数。

Animal dog2 = new Animal(name, race, age, weight);

上述方法不起作用,因为您尝试使用参数化构造函数,但是,您以 unresolved symbols 的形式乱码传递它。

你需要做的是声明(如果需要,实例化(一些将传递给构造函数的变量:

public static void Main(string[] args)
{
    string name = "SomeName";
    string race = "SomeRace";
    double age = 12;
    double weight = 9001;
    Animal dog2 = new Animal(name, race, age, weight);
}

如果您需要/希望用户定义传递给参数化构造函数的参数值,则需要执行类似于示例中进一步收集用户输入的方式(使用 Console.ReadLine()(。

*编辑:*

@"那你到底是什么意思,因为我确实声明了字符串和双变量来传递它们,但它不起作用。

您的那段代码似乎被省略或遗漏了。

@"你说我必须在类动物的构造函数中要求用户输入?或者创建一个接受输入以更改传递的变量值的方法?

您正在尝试使用 Main 函数内的参数化构造函数创建 Animal 的实例,因此您需要在进行构造函数调用之前和 Main 函数内部声明并实例化将用作Animal构造函数参数的变量。

@"就像我说的,我

确实声明了字符串和双精度,但我没有为它们分配值,因为就像你说的那样,我试图让用户定义我的参数,但我不确定如何准确做到这一点。">

您的代码已经有一个示例,说明如何获取用户输入并将其分配给变量:

// You wrote this [:
Console.Write("Please enter your dog's name: ");
dog.name = Console.ReadLine();
Console.Write("Please enter your dog's race: ");
dog.race = Console.ReadLine();
Console.Write("Please enter your dog's age: ");
dog.age = double.Parse(Console.ReadLine());
Console.Write("Please enter your dog's weight: ");
dog.weight = double.Parse(Console.ReadLine());

上述请求使用Console.ReadLine()的用户输入,然后将用户输入的值分配给关联的Animal property。 所以例如:

dog.name = Console.ReadLine();

以上假设已经有一个名为 dogAnimal的有效实例。 dog有许多公共properties(nameraceageweight). The left side (左操作数) of the赋值运算符( = ) is the object property that you want to assign a value to. The right side (右操作数) of the赋值运算符is where that value is derived. This could be Console.ReadLine(( (as in this example), a mathematical calculation or a hard coded value etc. However the右操作数is derived, it is to the左操作数'赋值。 这是你的任务。

再比如:

private static void Main(string[] args)
{
    // Obtain parameter values from user
    Console.WriteLine("Enter a name");
    string name = Console.ReadLine();
    Console.WriteLine("Enter a race");
    string race = Console.ReadLine();
    Console.WriteLine("Enter an age");
    // Console.ReadLine() reads values in as strings
    // Therefore you will need to convert the values you want as doubles
    double age;
    while (!double.TryParse(Console.ReadLine(), out age))
    {
        Console.WriteLine("The value you entered is not a valid age, please enter a valid age");
    }
    Console.WriteLine("Enter a weight");
    double weight;
    while (!double.TryParse(Console.ReadLine(), out weight))
    {
        Console.WriteLine("The value you entered is not a valid weight, please enter a valid weight");
    }
    // Create an instance of Animal
    Animal dog2 = new Animal(name, race, age, weight);
}