这是我不太一致的事情,并且总是对别人的行为感到好奇.
您如何访问内部属性(私有或公共)?
例如,你有这个属性:
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
在另一个函数中的同一个类中,您更喜欢哪一个?为什么?
_Name = "Johnny"
Run Code Online (Sandbox Code Playgroud)
要么
Name = "Johnny"
Run Code Online (Sandbox Code Playgroud)
忽略我使用Name而不是Me.Name的事实.
class Student
{
private string firstName;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value; // Possible logical checks may be implemented here in the future
}
}
public Student (firstName)
{
this.firstName = firstName; // Option 1
// Or
FirstName = firstName; // Option 2
}
}
Run Code Online (Sandbox Code Playgroud)
哪两条线更标准?
我们在构造函数中使用私有成员还是公共成员?