C# 施工人员的使用和当地派遣有什么区别?
大家好,我正在学习构造函数的用法,我想问一下下面两个代码片段之间的主要区别是什么 我正在将_fristnameinstance vairable分配给我在Human construction中创建的本地变量C# 施工人员的使用和当地派遣有什么区别?,c#,C#,大家好,我正在学习构造函数的用法,我想问一下下面两个代码片段之间的主要区别是什么 我正在将_fristnameinstance vairable分配给我在Human construction中创建的本地变量 public string _firstName; public Humans() { string name = ""; _firstName = name; } 我正在为实例_fristname分配局部变
public string _firstName;
public Humans()
{
string name = "";
_firstName = name;
}
我正在为实例_fristname分配局部变量名
public string _firstName;
public Humans()
{
string name;
name = _firstName;
}
为什么我使用公共访问修饰符将名称字符串化为公共
Humans()
{
public string name;
name = _firstName;
}
该类中的所有代码都有红色的错误行?为什么本地变量不能在构造函数中获得公共访问修饰符,还要从三个代码片段中指出哪些是否定的
我想知道除了创建实例到类类型对象的模板之外,构造函数的主要用途是什么。首先,问题的格式很难理解。如果您将其正确格式化,使缩进正确,并且代码不会与文本混合,这将有所帮助。但我想我明白你的意思了
// I'm not sure if you just left this out, or if you're missing it in your
// actual class, but need to declare the class.
public class Humans {
Humans() {
// The reason this doesn't work is because it's not a member of the class.
// you can only use access modifiers on a class member
public string name;
}
}
在上面的例子中,变量是在构造函数中声明的。如果它是在构造函数中声明的,那么它只存在于构造函数中。一旦离开构造函数,它就会消失
如果您想拥有一个属于类的公共成员名,可以在类中声明它
public class Humans {
public string name;
public Humans() {
name = "Some value here...";
}
}
在本例中,Humans对象具有一个属性名称,只要您的HumanInstance存在,就可以访问该名称
Humans human = new Humans();
string humanName = human.name;
因此,如果我们创建Humans对象的新实例,我们可以访问name属性。访问修饰符(如public、private等)允许您在构建对象后控制对这些属性的访问。所以它们只适用于属于类的值。在构造函数中声明变量时,该变量属于构造函数,而不是类。因此,当构造函数完成执行时,构造函数中声明的变量也完成了。但是,如果您在类本身的范围内声明变量,那么变量将与类保持相同的时间。因为访问该变量需要遍历类的实例,所以可以使用访问修饰符控制该变量的访问。private意味着只有在类中编写的代码才能访问它,public意味着任何可以访问类实例的东西都可以访问它
因此,作为访问修饰符如何工作的一个简单示例,下面是一个示例类Humans
public class Humans {
// These variables are accessible only by code within this class block.
private string firstName;
private string lastName;
// This constructor is public, so it could be accessed anywhere.
public Humans(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// This function is public, so can be called to get the full name.
public string getFullName() {
// the variable fullName only exists in the scope of this function.
// so there is no point in applying an access modifier.
string fullName = String.format("{} {}", this.firstName, this.lastName);
// When we return fullName, we return a copy of the value, and the
// original variable gets cleaned up.
return fullName;
}
// Lets say we want to allow updating the name, but we want to verify
// that the name is valid first. We can make public setters.
public void setFirstName(String firstName) {
if (!String.isNullOrWhiteSpace(firstName)) {
this.firstName = firstName;
} else {
throw new InvalidArgumentException("First name cannot be null or empty.")
}
}
}
将右侧的内容分配给左侧的内容:
\u firstName=name
。您将名称
指定为\u firstName
,而不是相反。查看访问修饰符适用于哪些成员。public/private/internal修饰符适用于类型和类型成员,而不是局部变量。您好,为什么要将右侧的赋值给左侧的赋值,以及为什么我需要假定实例成员var属于右侧或使其位于右侧。这就像说整数1;那么数字=4;在这种情况下,4代表构造函数中的用户定义值?我想我明白你的意思。谢谢你。你的解释真的帮助了我,它澄清了我想知道的。