当前上下文中不存在的C#实例
我是C#新手,所以这可能很简单,但我试图制作一个程序,询问用户的性别,然后根据“男孩”或“女孩”的答案为类创建一个实例。我已经在类“男孩”和“女孩”中创建了方法“特征”。然而,问题出现在最后一行“真实的性别特征”()因为“真实的性”在“当前语境”中不存在显然,要用正则变量解决这一问题,您需要在if语句之前声明变量,但在实例中它的行为似乎有所不同。有人能帮忙吗?谢谢。这是一个范围问题。当您在代码块内定义变量时,它将不存在于该代码块之外。例如:当前上下文中不存在的C#实例,c#,C#,我是C#新手,所以这可能很简单,但我试图制作一个程序,询问用户的性别,然后根据“男孩”或“女孩”的答案为类创建一个实例。我已经在类“男孩”和“女孩”中创建了方法“特征”。然而,问题出现在最后一行“真实的性别特征”()因为“真实的性”在“当前语境”中不存在显然,要用正则变量解决这一问题,您需要在if语句之前声明变量,但在实例中它的行为似乎有所不同。有人能帮忙吗?谢谢。这是一个范围问题。当您在代码块内定义变量时,它将不存在于该代码块之外。例如: Console.WriteLine("
Console.WriteLine("Are you a boy or a girl?");
string sex = Console.ReadLine();
Console.WriteLine(sex);
while ((sex != ("boy")) && (sex != ("girl")))
{
Console.WriteLine("That is not a valid sex. Please answer the question again.");
sex = Console.ReadLine();
}
if (sex == "boy")
{
Console.WriteLine("You are a boy");
Boy real_sex = new Boy
{
Firstname = "George",
Secondname = "Smith"
};
}
else if (sex == "girl")
{
Console.WriteLine("You are a girl");
Girl real_sex = new Girl
{
Firstname = "Charlotte",
Secondname = "Smith"
};
}
real_sex.Characteristics()
将打印罚款,但在尝试打印B时会抛出错误,因为B是在print语句之前结束的代码块中定义的
解决方案是在与所需代码块相同(或更高)的代码块中定义所需变量。比较:
int a = 2;
{
int b = 3;
}
Console.WriteLine("A : " + a.ToString());
Console.WriteLine("B : " + b.ToString());
这将很好地工作,现在打印A:2和B:3
所以,改变
int a = 2;
int b = 0;
{
b = 3;
}
Console.WriteLine("A : " + a.ToString());
Console.WriteLine("B : " + b.ToString());
到
当然,您需要一个名为“Sex”的父类,从中派生男孩和女孩,这样您就可以将real_Sex设置为男孩或女孩。这是一个范围问题。当您在代码块中定义变量时,它将不存在于该代码块之外。例如:
Console.WriteLine("Are you a boy or a girl?");
string sex = Console.ReadLine();
Console.WriteLine(sex);
while ((sex != ("boy")) && (sex != ("girl")))
{
Console.WriteLine("That is not a valid sex. Please answer the question again.");
sex = Console.ReadLine();
}
if (sex == "boy")
{
Console.WriteLine("You are a boy");
Boy real_sex = new Boy
{
Firstname = "George",
Secondname = "Smith"
};
}
else if (sex == "girl")
{
Console.WriteLine("You are a girl");
Girl real_sex = new Girl
{
Firstname = "Charlotte",
Secondname = "Smith"
};
}
real_sex.Characteristics()
Sex real_sex = null;
if (sex == "boy")
{
Console.WriteLine("You are a boy");
real_sex = new Boy
{
Firstname = "George",
Secondname = "Smith"
};
}
else if (sex == "girl")
{
Console.WriteLine("You are a girl");
real_sex = new Girl
{
Firstname = "Charlotte",
Secondname = "Smith"
};
}
real_sex.Characteristics()
将打印罚款,但在尝试打印B时会抛出错误,因为B是在print语句之前结束的代码块中定义的
解决方案是在与所需代码块相同(或更高)的代码块中定义所需变量。比较:
int a = 2;
{
int b = 3;
}
Console.WriteLine("A : " + a.ToString());
Console.WriteLine("B : " + b.ToString());
这将很好地工作,现在打印A:2和B:3
所以,改变
int a = 2;
int b = 0;
{
b = 3;
}
Console.WriteLine("A : " + a.ToString());
Console.WriteLine("B : " + b.ToString());
到
当然,您需要一个名为“Sex”的父类,从中派生男孩和女孩,以便您可以将real_Sex设置为男孩或女孩。类
Boy
和Girl
是否都继承自一个基类或接口,该基类或接口上有列为方法的特征(){}.Variable real_sex不会在您想要访问它的位置退出。您正在嵌套的作用域中声明real_sex变量。您将无法在该作用域之外访问它。您提到了“常规变量”和“实例”(可能是值与引用类型)之间的区别。我认为这是您误解的结果,因为这对我来说毫无意义。在这个问题上,您认为两者有什么不同?发布完整的代码,您是如何创建对象的,以及什么是访问特性说明符()方法类Boy
和Girl
是否都继承自一个基类或接口,该基类或接口的Characteristics()
列为其上的方法?上下文由花括号{}定义.Variable real_sex不会在您想要访问它的位置退出。您正在嵌套范围中声明real_sex变量。您将无法在该范围之外访问它。您提到了“常规变量”和“实例”(可能是值与引用类型)之间的区别。我认为这是您误解的结果,因为这对我来说毫无意义。在这个问题上,您认为两者有什么不同?发布完整的代码,您是如何创建对象的,什么是特征()方法的访问说明符,或者两者都需要实现一个具有characteristics()的公共接口
在其中,您可以通过接口访问男孩和女孩……是的,最好的方法只是取决于具体情况(例如,如果两者之间有相同的函数,使用父类将减少代码与接口的重复),或者两者都需要实现具有特征的公共接口()
在其中,您可以通过接口访问男孩和女孩……是的,最佳方法仅取决于具体情况(例如,如果两者之间有相同的函数,使用父类将减少与接口相比的重复代码)
Sex real_sex = null;
if (sex == "boy")
{
Console.WriteLine("You are a boy");
real_sex = new Boy
{
Firstname = "George",
Secondname = "Smith"
};
}
else if (sex == "girl")
{
Console.WriteLine("You are a girl");
real_sex = new Girl
{
Firstname = "Charlotte",
Secondname = "Smith"
};
}
real_sex.Characteristics()