Kent Beck 的测试驱动开发示例
我正在阅读测试驱动开发:通过示例和其中一个示例打扰我了。 第 3 章(人人平等)中,作者在 Dollar
类中创建了一个 equals
函数来比较两个 Dollar
对象:
public boolean equals(Object object)
{
Dollar dollar= (Dollar) object;
return amount == dollar.amount;
}
在 在接下来的章节(4:隐私)中,他将 amount 设为 Dollar 类的私有成员。
private int amount;
并且测试通过了。 这是否会导致 equals
方法中出现编译器错误,因为虽然该对象可以访问自己的 amount
成员,但因为它无法访问其他 Dollar
code> 对象的 amount
成员?
//shouldn't dollar.amount be no longer accessable?
return amount == dollar.amount
我是否从根本上误解了private
?
更新 我决定回去手动编写这本书的代码,当我进入下一部分(第 6 章 - 所有人平等,Redux)时,他们将 amount 推入父类并使其受到保护,我遇到了访问问题:
public class Money
{
protected int amount;
}
public class Dollar : Money
{
public Dollar(int amount)
{
this.amount = amount;
}
// override object.Equals
public override bool Equals(object obj)
{
Money dollar = (Money)obj;
//"error CS1540: Cannot access protected member 'Money.amount'
// via a qualifier of type 'Money'; the qualifier must be of
// type 'Dollar' (or derived from it)" on the next line:
return amount == dollar.amount;
}
}
这是否意味着 C# 中的 protected
是基于实例的?
I'm reading through Test Driven Development: By Example and one of the examples is bugging me. In chapter 3 (Equality for all), the author creates an equals
function in the Dollar
class to compare two Dollar
objects:
public boolean equals(Object object)
{
Dollar dollar= (Dollar) object;
return amount == dollar.amount;
}
Then, in the following chapter (4: Privacy), he makes amount a private member of the dollar class.
private int amount;
and the tests pass. Shouldn't this cause a compiler error in the equals
method because while the object can access its own amount
member as it is restricted from accessing the other Dollar
object's amount
member?
//shouldn't dollar.amount be no longer accessable?
return amount == dollar.amount
Am I fundamentally misunderstanding private
?
UPDATE
I decided to go back and code along with the book manually and when I got to the next part (chapter 6 - Equality For All, Redux) where they push amount into a parent class and make it protected, I'm getting access problems:
public class Money
{
protected int amount;
}
public class Dollar : Money
{
public Dollar(int amount)
{
this.amount = amount;
}
// override object.Equals
public override bool Equals(object obj)
{
Money dollar = (Money)obj;
//"error CS1540: Cannot access protected member 'Money.amount'
// via a qualifier of type 'Money'; the qualifier must be of
// type 'Dollar' (or derived from it)" on the next line:
return amount == dollar.amount;
}
}
Does this mean that protected
IS instance-based in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,你从根本上误解了私人。 隐私是特定于类的,而不是特定于实例的。
Yep, you're fundamentally misunderstanding private. Privacy is class-specific, not instance-specific.
从根本上误解了 private,Dollar 可以访问任何 Dollar 私有方法(如果它们是同一类)。
Fundamentally misunderstanding private, Dollar can access any Dollar private method if they are the same class.
修饰符 private 是类私有,而不是对象私有。
Modifier private is class-private, not object-private.
在 Java 中,
private
表示类私有。 在类中,您可以在类的所有实例中访问该字段。在 Scala 中,还有一个对象私有作用域,写作
private[this]
。 另外,在其他方面,Scala 的范围更加灵活(请参阅本文 了解更多信息)。但在 Java 中没有对象私有作用域。
In Java,
private
means class-private. Within the class, you can access that field in all instances of the class.In Scala there is also an object-private scope which is written
private[this]
. Also in other respects Scala's scopes are more flexible (see this article for more information).But in Java there is no object-private scope.
在 C++ 系列语言(C++、Java、C#)中,访问控制仅在类级别。 因此
private
允许访问该类的任何实例。Smalltalk 隐私中的 IIRC 的行为正如您所期望的那样。
In languages of the C++ family (C++,Java,C#), access control is only at the class level. So
private
allows access to any instance of that class.IIRC in Smalltalk privacy behaves as you expect.