需要一些C#的重载信息

时间:2013-10-08 19:29:42

标签: c# overloading

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

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


            }
        }
    }

现在我知道有很多东西不必写在这段代码中,但这就是iam应该怎么做的,我只是遇到参数有任何帮助吗?

2 个答案:

答案 0 :(得分:0)

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

会很高兴

很抱歉,我正在使用我的C ++帽子分析你的代码...而在C#中你做的一切都非常正确,除了将正确的值作为参数传递。您试图在定义/分配之前传递该值,这就是您收到错误的原因。 所以对于狗2你可能想要这样做。

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


            } 

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

答案 1 :(得分: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的形式传递它为jibberish。

您需要做的是声明(并在需要时实例化)一些将传递给构造函数的变量:

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的实例,因此您需要声明并实例化将用作{{1}的参数的变量在构造函数调用之前和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()); 的用户输入,然后将用户输入的值分配给关联的Console.ReadLine() Animal。例如:

property

以上假设已存在名为dog.name = Console.ReadLine(); 的{​​{1}}的有效实例。 Animal有一些公开的dog dogpropertiesnamerace左操作数age赋值运算符{{ 1}} = weight). The left side (右操作数) of the赋值运算符( Console.ReadLine()) 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左操作数`。这是你的任务。

另一个例子:

(as in this example), a mathematical calculation or a hard coded value etc.  However the