提问人:Calcul4tor 提问时间:4/8/2023 最后编辑:marc_sCalcul4tor 更新时间:4/8/2023 访问量:102
为什么我的实例在当前上下文中不存在?[已结束]
Why does my instance not exist in the current context? [closed]
问:
我使用了 if 语句来确定实例应该使用哪个构造函数,但它声称它在当前上下文中不存在。我使用了应该获得全名、眼睛颜色和年龄的方法,但两者都是可选的。然后,它应根据用户输入确定应使用哪个构造函数。GetDetails
之后,它将调用函数 ,它只是自我介绍。此函数位于另一个名为 的类中,该类包含所有构造函数。代码尚未完全完成,仍然存在错误,但是我想了解此错误是什么以及如何修复它。IntroduceMyself
Human
这是主类:
class Program
{
static string fullName;
static string eyeColour;
static int age;
static void Main(string[] args)
{
GetDetails();
if (eyeColour == "")
{
Human person1 = new Human(fullName);
}
else if (eyeColour != "")
{
Human person1 = new Human(fullName, eyeColour);
}
else
{
Human person1 = new Human(fullName, eyeColour, age);
}
person1.IntroduceMyself(fullName, eyeColour, age);
}
public static void GetDetails()
{
Console.Write("Hello! To start, please enter your full name:");
fullName = Console.ReadLine();
Console.Write("Would you like to input your eye colour? y / n :");
if (Console.ReadLine() == "y")
{
Console.Write("Please enter your eye colour:");
eyeColour = Console.ReadLine();
}
Console.Write("Would you like to input your age? y / n :");
if (Console.ReadLine() == "y")
{
Console.Write("Please enter your age:");
while(!(Int32.TryParse(Console.ReadLine(), out age)))
{
Console.Write("There was an error! Please try again:");
}
}
}
这是人类类:
internal class Human
{
public string fullName;
public string eyeColour;
public int age;
public Human(string fullName)
{
this.fullName = fullName;
}
public Human(string fullName, string eyeColour)
{
this.fullName = fullName;
this.eyeColour = eyeColour;
}
public Human(string fullName, string eyeColour, int age)
{
this.fullName = fullName;
this.eyeColour = eyeColour;
this.age = age;
}
public static void IntroduceMyself(string fullName, string eyeColour, int age)
{
Console.WriteLine("Hello {0}, you have {1} eyes and you are {2} years old", fullName, eyeColour, age);
}
}
答:
3赞
Milly Stack
4/8/2023
#1
该变量在语句内部声明,不能在相应块的范围之外访问。因此,不能在这些块之外访问该变量以调用 IntroduceMyself 方法。
person1
if-else
if-else
person1
该函数被定义为静态函数,这意味着您可以使用类名后跟函数名来调用它,而不需要类 () 的实例。我建议删除静电。
IntroduceMyself
Human.IntroduceMyself(fullName, eyeColor, age)
下面是更新后的代码
// Human class
internal class Human
{
public string fullName;
public string eyeColour;
public int age;
public Human(string fullName)
{
this.fullName = fullName;
}
public Human(string fullName, string eyeColour)
{
this.fullName = fullName;
this.eyeColour = eyeColour;
}
public Human(string fullName, string eyeColour, int age)
{
this.fullName = fullName;
this.eyeColour = eyeColour;
this.age = age;
}
// Function no longer static. Also removed the parameters since they are apart of the object
public void IntroduceMyself()
{
Console.WriteLine("Hello {0}, you have {1} eyes and you are {2} years old", fullName, eyeColour, age);
}
}
using System;
class Program
{
static string fullName;
static string eyeColour;
static int age;
static void Main(string[] args)
{
GetDetails();
// defining the person
Human person1 = null;
if (eyeColour == "")
{
person1 = new Human(fullName);
}
else if (eyeColour != "")
{
person1 = new Human(fullName, eyeColour);
}
else
{
person1 = new Human(fullName, eyeColour, age);
}
person1.IntroduceMyself();
}
public static void GetDetails()
{
Console.Write("Hello! To start, please enter your full name:");
fullName = Console.ReadLine();
Console.Write("Would you like to input your eye colour? y / n :");
if (Console.ReadLine() == "y")
{
Console.Write("Please enter your eye colour:");
eyeColour = Console.ReadLine();
}
Console.Write("Would you like to input your age? y / n :");
if (Console.ReadLine() == "y")
{
Console.Write("Please enter your age:");
while(!(Int32.TryParse(Console.ReadLine(), out age)))
{
Console.Write("There was an error! Please try again:");
}
}
}
}
评论
0赞
Luuk
4/8/2023
请注意,如果 eyeColour 为 null:i.stack.imgur.com/bFn5x.png
评论
Human person1