Kent Beck 的测试驱动开发示例

发布于 2024-07-19 03:53:13 字数 1496 浏览 4 评论 0原文

我正在阅读测试驱动开发:通过示例和其中一个示例打扰我了。 第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高速公鹿 2024-07-26 03:53:13

是的,你从根本上误解了私人。 隐私是特定于类的,而不是特定于实例的。

Yep, you're fundamentally misunderstanding private. Privacy is class-specific, not instance-specific.

是你 2024-07-26 03:53:13

从根本上误解了 private,Dollar 可以访问任何 Dollar 私有方法(如果它们是同一类)。

Fundamentally misunderstanding private, Dollar can access any Dollar private method if they are the same class.

丑丑阿 2024-07-26 03:53:13

修饰符 private 是类私有,而不是对象私有。

Modifier private is class-private, not object-private.

尐籹人 2024-07-26 03:53:13

在 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.

逆蝶 2024-07-26 03:53:13

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文