在我们的.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); }
}
}
答案 0 :(得分:8)
您不应使用受保护的字段,因为无法保护版本控制和访问。请参阅Field Design指南。将您的字段更改为属性,这也会强制您更改为名称(因为您不能有两个具有相同名称的属性)。或者,如果可能,请将受保护的字段设为私有。
要使属性的设置仅对继承类可访问,请使用受保护的setter:
if let user = FIRAuth.auth()?.currentUser {
// user is logged in
// *** I think this is the line with the issue ***
FIRDatabase.database().reference().child("users").child(user.uid).child("messages").observeEventType(.Value, withBlock: { (snapshot) in
messageArray = []
if let dictionaryOfMessages = snapshot.value as? [String: AnyObject] {
for messageKey in dictionaryOfMessages.keys {
messageArray.append(Message(json: JSON(dictionaryOfMessages[messageKey]!)))
// set the messageId
messageArray[messageArray.count - 1].messageId = messageKey
}
}
// return the data to the VC that called the function
success(messages: messageArray)
}) { (error) in
// Handle the Error
}
} else {
// return some generic messages about logging in etc.
}
虽然这确实导致get和set之间存在分歧,但功能并不相同。在这种情况下,也许一个单独的受保护方法会更好:
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);
}
}
答案 1 :(得分:2)
嗯,总结已经说过的话你可以这样做:
public class Dimension : Text
{
private string _textPrefix;
private double _rawDistance;
/// <summary>
/// Gets the real measured distance.
/// </summary>
public double AbsoluteDistance
{
get; private set;
}
/// <summary>
/// Gets the raw distance
/// </summary>
public double RawDistance
{
get { return _rawDistance; }
protected set { _rawDistance = value; AbsoluteDistance = Math.Abs(value); }
}
}
设置RawDistance
的值时,它还会设置AbsoluteDistance
的值,因此无需在“{1}}”{AbsoluteDistance“中调用Math.Abs()
。