Vb.net NET语言中支持属性的单一实现
使用VB.Net 我声明了如下属性:Vb.net NET语言中支持属性的单一实现,vb.net,Vb.net,使用VB.Net 我声明了如下属性: Private _name as String Private _address as String Public Property Name as String Get Return _name End Get Set(value as String) _name = value End Set End Property Public Property Address as String G
Private _name as String
Private _address as String
Public Property Name as String
Get
Return _name
End Get
Set(value as String)
_name = value
End Set
End Property
Public Property Address as String
Get
Return _address
End Get
Set(value as String)
_address= value
End Set
End Property
我希望最小化声明另一个将处理get和set的变量。此变量或函数将由所有属性使用,并将处理所有属性的所有get和set值
例如:
Public Property Address as String
Get
Return address /*Insert function here that can be use by all property*/
End Get
Set(value as String)
address = value /*Insert function here that can be use by all property*/
End Set
End Property
提前感谢。在c中,您可以用简短的格式定义属性
public string Name{get; set;}
编译器将为它创建备份字段,并扩展为简单的getter和setter
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
当然,您可以为getter和setter快捷方式提供或不提供访问修饰符:
public string Name{ internal get; protected set;}
是的,你可以拥有房产
- 无背场
- 不带
设置
(只读)
- 没有
(疯狂只写属性)get
- 使用不同的访问修饰符访问
和get
set
类属性
{
//只读,无支持字段
公共int答案{get{return 42;}
//仅写,无支持字段
公共bool销毁{set{if(value)destromething();}}
//没有具有某些工作入属性的支持字段:
int合并;
公共整数低{
获取{return combined&0xFF;}
集合{combined=combined&~0xFF |(value&0xFF);}
}
公共int高{
获取{return combined>>8;}
设置{combined=combined&0xFF |(值这与C#?@shigatsu.有什么关系?没有必要使用backing字段..试试看!!可能是重复的,很抱歉出错,是的,我想在VB.NET上使用它。我想问题是关于如何在VB.NET中实现这一点。(问题不是很清楚)
class Properties
{
// Read-only, no backing field
public int TheAnswer { get { return 42;}}
// Write-only, no backing field
public bool Destroyed { set { if (value) DestroyEverything();}}
// No backing fields with some work in properties:
int combined;
public int Low {
get { return combined & 0xFF; }
set { combined = combined & ~0xFF | (value & 0xFF); }
}
public int High {
get { return combined >> 8; }
set { combined = combined & 0xFF | (value << 8); }
}
void DestroyEverything(){}
}
Dim firstName, lastName As String
Property fullName() As String
Get
If lastName = "" Then
Return firstName
Else
Return firstName & " " & lastName
End If
End Get
Set(ByVal Value As String)
Dim space As Integer = Value.IndexOf(" ")
If space < 0 Then
firstName = Value
lastName = ""
Else
firstName = Value.Substring(0, space)
lastName = Value.Substring(space + 1)
End If
End Set
End Property