0

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

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(); 


            }
        }
    }

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

4

2 回答 2

0

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

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())。

*编辑:*

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

您的那部分代码似乎已被省略或遗漏。

@ “你说我必须在动物类的构造函数中要求用户输入?还是创建一个接受输入的方法来更改传递的变量的值?。”

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

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

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

// 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());

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

dog.name = Console.ReadLine();

以上假设已经存在一个有效的Animalnamed实例dogdog有许多公共properties( name, race,ageweight). 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 beConsole.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);
}
于 2013-10-08T20:04:44.343 回答
0

您似乎混淆了构造函数调用和函数调用 - >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(); 


            } 

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

于 2013-10-08T19:35:30.080 回答