abe*_*nci 5 .net c# naming-conventions
在我们的.NET软件组件中,我们使用以下命名约定.当客户使用我们的VB.NET DLL时,编译器无法区分distance成员字段和Distance属性.你推荐什么解决方法?
谢谢.
public class Dimension : Text
{
private string _textPrefix;
protected double distance;
/// <summary>
/// Gets the real measured distance.
/// </summary>
public double Distance
{
get { return Math.Abs(distance); }
}
}
Run Code Online (Sandbox Code Playgroud)
Wic*_*ser 10
您不应使用受保护的字段,因为无法保护版本控制和访问.请参阅现场设计指南.将您的字段更改为属性,这也会强制您更改为名称(因为您不能有两个具有相同名称的属性).或者,如果可能,将受保护的字段设为私有.
要使您的属性的设置仅对继承类可访问,请使用受保护的setter:
public class Dimension : Text
{
private string _textPrefix;
private double _absoluteDistance;
/// <summary>
/// Gets the real measured distance.
/// </summary>
public double Distance
{
get { return _absoluteDistance }
protected set { _absoluteDistance = Math.Abs(distance);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这确实会导致get和set之间出现分歧,但功能并不相同.在这种情况下,也许一个单独的受保护方法会更好:
public class Dimension : Text
{
private string _textPrefix;
/// <summary>
/// Gets the real measured distance.
/// </summary>
public double Distance { get; private set; }
protected void SetAbsoluteDistance(double distance)
{
Distance = Math.Abs(distance);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5974 次 |
| 最近记录: |