私人会员在课堂上的目的 [英] Purpose of private members in a class

查看:106
本文介绍了私人会员在课堂上的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在面向对象编程中拥有类/结构的私有/受保护成员的目的是什么?

What are the purposes of having private/protected members of a class/structure in object-oriented programming? What's the harm in having all members be public?

推荐答案

封装。也就是说隐藏你的类数据的实现。这允许您以后更改它,而不会破坏所有客户端代码。例如。如果你有

Encapsulation. I.e. hiding the implementation of your class data. This allows you to change it later, without breaking all client code. E.g. if you have

class MyClass {
    public int foo;
}

您的客户可能会写

MyClass bar = new MyClass();
bar.foo++;



是一个double而不是int,你改变它:

now if you realize that foo should actually be a double rather than int, you change it:

class MyClass {
    public double foo;
}

且客户端代码无法编译: - (

and the client code fails to compile :-(

通过设计良好的界面,内部(私有部分)的更改甚至可以包括将成员变量转换为计算,反之亦然:

With a well designed interface, the change of the internals (private parts) may even include turning a member variable into a calculation or vice versa:

class Person {
    public String getName();
    public String getStreetAddress();
    public String getZipCode();
    public String getCountryCode();
    public int hashCode();
}

使用String属性为了简单起见 - 在现实世界的设计中,其中一些可能应该有自己的类型。)

(using String properties for the sake of simplicity - in a real world design some of these would probably deserve to have their own type.)

使用这个设计,例如在内部引入地址属性,其中包含街道地址,邮政编码和国家代码,并重写访问者以使用此私人成员的字段,而无需客户注意任何东西。

With this design, you are free to e.g. introduce an Address property internally, which would contain street address, zip code and country code, and rewrite your accessors to use the fields of this private member instead, without your clients noticing anything.

您还可以自由决定是否每次计算哈希码,或将其缓存到私有变量以提高性能。如果该缓存字段是公共的,任何人都可以更改它,这可能会破坏哈希映射行为和引入微妙的错误。因此,封装是保证对象的内部状态的一致性的关键。例如。在上面的示例中,您的setter可以轻松验证邮政编码和国家/地区代码,以防止设置无效值。您甚至可以确保邮政编码格式对实际国家/地区有效,即确保跨多个属性的有效性条件。通过精心设计的界面,您可以通过例如只提供一个setter来同时设置属性:

You could also decide freely whether to calculate the hash code every time, or to cache it into a private variable in order to improve performance. If that cache field was public, however, anyone could change it, which could ruin hash map behaviour and introduce subtle bugs. So encapsulation is key in guaranteeing the consistency of the your object's internal state. E.g. in the above example, your setters can easily validate the zip code and country code, to prevent setting invalid values. You can even ensure that the zip code format is valid for the actual country, that is, ensure a validity criteria spanning multiple properties. With a well designed interface, you can enforce this binding by e.g. providing only a setter to set both properties at the same time:

    public void setCountryCodeAndZip(String countryCode, String zipCode);

但是,对于公共字段,你根本没有这些选择。

However, with public fields you simply don't have these choices.

私有字段的一个特殊用例是不可变对象;这在例如Java,例如 String BigDecimal 。这些类根本没有公共设置器,这保证了它们的对象一旦被创建,就不会改变它们的状态。这使得许多性能优化,以及使它们更容易使用在例如。多线程程序,ORM等。

A special use case for private fields is immutable objects; this is very common in e.g. Java, examples are String and BigDecimal. These classes have no public setters at all, which guarantees that their objects, once created, will not change their state. This enables a lot of performance optimizations, as well as makes them easier to use in e.g. multithreaded programs, ORM etc.

这篇关于私人会员在课堂上的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆