什么时候不使用 setter 直接在对象上设置属性值被认为是好的设计?

发布于 2024-12-27 14:08:53 字数 771 浏览 1 评论 0原文

这可能不是最适合 stackoverflow 的问题,但我只是在寻找一个最能描述为什么程序员有时不使用属性的 setter/getter 的答案,例如在属性的情况下注射(DI)。

考虑这个例子......

class Test
{
    public propertyA;
    protected propertyB;

    public function setPropertyB(val)
    {
        // do some logic to validate 'val'
        this.propertyB = val;
    }

    public function getPropertyB()
    {
        return this.propertyB;
    }
}

为什么你会选择直接设置propertyA的风格:

var Test = new Test();
Test.propertyA = 1;

propertyB的setter选项上:

var Test = new Test();
Test.setPropertyB(1);

我总是使用setter/getter方法,但是我有看到一些相当成熟的框架使用 propertyA 方法散布着 propertyB 方法。使用这种方法我们有什么好处?

This may not be the best kind of question suited to stackoverflow, but I'm only after an answer that best describes why programmers sometimes don't use setters/getters for properties, e.g. in the case of property injection (DI).

Consider this example...

class Test
{
    public propertyA;
    protected propertyB;

    public function setPropertyB(val)
    {
        // do some logic to validate 'val'
        this.propertyB = val;
    }

    public function getPropertyB()
    {
        return this.propertyB;
    }
}

Why would you choose the style of directly setting propertyA:

var Test = new Test();
Test.propertyA = 1;

Over the setter option for propertyB:

var Test = new Test();
Test.setPropertyB(1);

I always use the setter/getter approach, but I have seen some pretty established frameworks using the propertyA approach interspersed with the propertyB approach. What benefits do we have using this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

秋千易 2025-01-03 14:08:53

为什么您可能不关心封装:

  1. 您可能会在 15 分钟后扔掉该项目。

  2. 您可能发现 getter/setter 是 CPU 密集型代码的瓶颈,导致您优化性能而不是设计。

  3. 实例字段可能是不可变且只读的,因此公开它可能没有危险。

  4. 你太懒了,无法编写 getter/setter。

Why you might not care about encapsulation:

  1. You might be throwing away the project 15 minutes later.

  2. You might have found getters/setters to be bottlenecks for your CPU-bound code, causing you to optimize for performance instead of design.

  3. The instance field might be immutable and read-only, so there might be no danger in exposing it.

  4. You're too lazy to write getters/setters.

不再见 2025-01-03 14:08:53

您应该使用 getter 和 setter,因为它们允许您控制对象的接口。

例如,假设我在 Java 应用程序中有一个银行帐户类:

class BankAccount {
    private int balance;

    BankAccount() {
        balance = 0;
    }

    public void deposit(int amount) {
        balance = balance + amount;
    }

    public void withdraw(int amount) {
        balance = balance - amount;
    }
}

当我的软件需要通过存款和取款来更改银行帐户的余额时,它会调用适当的方法。

现在,出现了一些狡猾的人,他们设法通过告诉网上银行软件提取负金额来增加银行余额。我可以通过向提款方法添加前提条件来修复此错误,然后该错误就会消失。

如果余额字段是公共的,并且一大堆类只是任意操纵它的值,那么现在需要更改这些类。如果其中一些外部类是由第三方编写的,那么我们将面临很大的痛苦才能修复错误。

为什么要使用公共字段?在一般情况下,您可能不应该这样做。有些语言允许您将字段范围设置为公共,那么如果您稍后需要添加 getter/setter,则可以在不更改对象接口的情况下执行此操作(我相信 C# 会这样做,但如果我错了,请纠正我)。

You should use getters and setters because they allow you to control the interface to your objects.

For example, let's say I have a bank account class in a Java application:

class BankAccount {
    private int balance;

    BankAccount() {
        balance = 0;
    }

    public void deposit(int amount) {
        balance = balance + amount;
    }

    public void withdraw(int amount) {
        balance = balance - amount;
    }
}

When my software needs to alter a bank account's balance through deposits and withdrawals, it calls the appropriate methods.

Now, along comes some sneaky individual who manages to figure out that they can increase their bank balance by telling their internet banking software to withdraw negative amounts of money. I can fix this bug by adding a precondition to the withdraw method, and the bug goes away.

If the balance field was instead public, and a whole bunch of classes were just manipulating it's value arbitrarily, those classes would now need to be changed. If some of those external classes were written by third parties, then we're looking at a whole lot of pain to get the bug fixed.

Why would you use public fields? In the general case, you probably shouldn't. Some languages allow you to have a field scoped as public, then if you need to add a getter/setter later on you can do so without changing your object's interface (I believe C# does this, but correct me if I'm wrong).

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