我遇到以下问题,想看看是否有人可以帮助我。详细信息在代码中,但我试图重载构造函数并且参数名称给了我一个错误:
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();
}
}
}
现在我知道有很多东西不必写在这段代码中,但这就是我应该这样做的方式,我只是在参数方面遇到了麻烦,请帮忙?