问题
I was asked a question in an interview: if encapsulation is possible without OOP, e.g. in a procedural language?
回答1:
Bob Martin has stated that encapsulation is not only possible without OOP, it was better before OOP came along.
Here is an excerpt from a talk he gave in 2014 at Yale School of Management.
We had perfect encapsulation. In C, all you had to do was forward declare your functions and your data structures. You didn't have to implement them. You would forward declare them in a header file, and then you would implement them in a C file. Your users would
#include
your header file. They could see nothing of your implementation. Perfect encapaulation. There was no way any of your users could see any of your data values. All they could see was your function signatures. They could see the names of your data structures but none of the members inside your data structures. Absolutely perfect encapsulation.
来源:https://stackoverflow.com/questions/52645937/is-encapsulation-possible-without-oop