公共变量坏实践vs Getters和Setters函数? [英] Public variables bad practice vs Getters and Setters functions?

查看:141
本文介绍了公共变量坏实践vs Getters和Setters函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在他的流中我遇到了这个问题,从那时起,这个问题就像一个疼痛的拇指。我想也许如果我保存了视频,并回到它在未来,当我更精通我会理解它,但它只是不断地麻烦只是离开它。这是视频...

I came across this during his stream, and this stuck out to me like a sore thumb since. I thought maybe if I saved the video and come back to it in the future when I'm more proficient I'll understand it, but it just kept on bothering to just leave it be. Here's the video...

它会自动从1:13:00开始。

It automatically starts at 1:13:00 for you.

https://youtu.be/uHSLHvWFkto?t=4380

作为一个新的程序员,C / C ++听说这完全扭曲了我的思维方式。他是一个专业,所有我应该采纳的建议,但我需要一些清晰。从网站,视频,到书籍,我已经读到,使用公共变量是一个坏的做法,但从我从这个视频中得到的其他说法。在视频中,他使用一个结构,它默认情况下具有public的访问修饰符vs一个默认访问为private的类。有没有什么我不能理解这里。

As a new programmer to C/C++ after hearing this its completely warped my way of thinking. With him being a professional and all I should take the advice, but I need some clarity. From sites, videos, to books, I've read that the use of public variables is a bad practice, but from what I'm getting from this video its saying otherwise. In the video he's uses a struct which by default has a access modifier of "public" vs a class which has a default access of "private". Is there something I'm not comprehending properly here.

我不知道该怎么办。如果我让我的变量公开我不会冒险含糊吗?他的方式他说,他会自动开火的人编码格式是得到我哈哈!我应该真正使用哪一个?何时和为什么?

I don't know what to do. If I make my variables public won't I risk ambiguity ? The way he's saying that he'll automatically fire someone for coding in format is getting to me haha! Which one should I truly use ? When and Why ?

推荐答案

首先, struct 相当于 class ,但默认成员访问为 public ,而不是 private

First of all, a struct is completely equivalent to a class, but with the default member access being public rather than private.

现在,在面向对象编程(OOP)中,不是 > public 数据成员(变量),因为这会使全部代码取决于的内部从而打破了OOP的原始原则,也就是...

Now, in Object Oriented Programming (OOP), it's not considered good practice to have public data members (variables), because that makes all your code dependent on the internals of the class, and thus breaking a primordial principle of OOP, and that is...

封装这种编码哲学声明一个类应该包含数据和在单个紧密实体中管理它的代码。也就是说,您不会访问数据定向,而是使用中的方法 。这有几个设计优点,例如,你会知道,除了类中的一个代码之外没有代码可能包含操纵这样的信息的错误。

Encapsulation is the coding philosophy that states that a class should englobe both data and the code that manages it in a single tight entity. That is, you don't access data directy, but rather you use methods from the class to manipulate such data. This has several design advantages, such as that you'll know that no code except the one inside the class may incorporate bugs with respect to the manipulation of such information.

现在, get() ers和 set() ers,也称为存取器 完整谎言!随着访问器,你自己欺骗自己认为你尊重封装,当你是打破它!它增加了膨胀,必要的冗长,错误和除封装之外的一切。而不是具有类Person with unsigned getAge() void setAge(unsigned),使用 unsigned getAge() a void incrementAge()

Now, get()ers and set()ers, otherwise known as accessors, are a complete lie! With accessors, you're tricking yourself into thinking that you're respecting encapsulation, when you're rather breaking it! It adds bloat, innecessary verbosity, bugs, and everything but encapsulation. Instead of having a class Person with unsigned getAge() and void setAge(unsigned), have it with a unsigned getAge() and a void incrementAge() or however you want to call it.

现在,对你的问题的核心...

Now, to your question's core...

不希望始终进行封装。虽然你应该(通常)在头文件(至少对于一些封装)上这样做,你可以创建静态纯旧 struct 对于单个翻译单元是私有的。我的建议是让他们甚至比他们已经老。

Encapsulation is not always desired. Although you should (usually) not do this on header files (again, for at least some bit of encapsulation), you may create static plain old structs that are private to a single translation unit. My recommendation is to make them even "older" than they already are, i.e...


  • 所有数据成员

  • 没有方法。

  • 没有构造函数(隐式属性除外)。

  • 继承总是公开的,只允许从其他简单的 struct


  • All data members are public.
  • No methods.
  • No constructors (except implicit ones).
  • Inheritance is always public, and only allowed from other plain old structs.
  • I repeat, don't put them on header files!

现在,另一个用于纯旧 struct code> s是(讽刺的是)元程序导出 constexpr 数据和类型,也称为现代核心 - 模板 - 元编程 - public -everywhere,例如...

Now, another use for plain old structs is (ironically) metaprogrammatic exporting of constexpr data and types, otherwise known as modern-hardcore-template-metaprogramming-without-having-to-type-public-everywhere, for example...

template<bool B, typename T>
struct EnableIf {};

template<typename T>
struct EnableIf<true, T> {
    typedef T type;
};

template<bool B, typename T>
using SFINAE = typename EnableIf<B, T>::Type;

这篇关于公共变量坏实践vs Getters和Setters函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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