简体   繁体   English

抽象是如何实际工作的? 如果开发人员可以浏览代码并查看所有内容,那么“隐藏”在现实世界中是如何工作的?

[英]How does abstraction work practically? If a developer can navigate the code and see everything, then how does 'hiding' work in the real world?

I get confused when I read 'you are hiding concrete classes by using an interface'.当我读到“您正在使用接口隐藏具体类”时,我感到困惑。 Since when the app is used by another guy, he can see the entire code (unless I expose web services only...that is a different aspect, not to mix here)?因为当应用程序被另一个人使用时,他可以看到整个代码(除非我只公开 web 服务......这是一个不同的方面,不要在这里混合)?

I agree that interfaces and abstract classes help to nicely design code, but from what I understand, that's it.我同意接口和抽象类有助于很好地设计代码,但据我了解,仅此而已。 I can't understand how practically in real world we are hiding stuff from others or adding more security etc.我无法理解在现实世界中我们如何向他人隐藏东西或增加更多安全性等。

You have a misconception that the reason for "hiding" something is security.你有一个误解,认为“隐藏”某事的原因是安全。 That is not at all what "hiding" means in this context.在这种情况下,这根本不是“隐藏”的意思。 An interface "hides" the implementation details of a class in the same way a car's engine is "hidden" under the hood.接口“隐藏”了 class 的实现细节,就像汽车引擎“隐藏”在引擎盖下一样。 Of course this doesn't stop you from looking at the engine if you want to see the car's internal workings, but the important points are that当然,如果您想了解汽车的内部运作,这并不会阻止您查看发动机,但重要的一点是

  • You don't have to look at the engine or understand it in order to use the car, and您无需查看或了解发动机即可使用汽车,并且
  • A car manufacturer doesn't have to make the engine work a particular way in order for the car to work as expected.汽车制造商不必为了让汽车按预期工作而使发动机以特定方式工作。

Likewise, by hiding the internal workings of a class, users of the class only need to understand how the class is meant to be used (ie its interface), and the developers of the class are free to change internal details of the class without annoying the users or breaking their code. Likewise, by hiding the internal workings of a class, users of the class only need to understand how the class is meant to be used (ie its interface), and the developers of the class are free to change internal details of the class without annoying用户或破坏他们的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM