When/why to make function private in class?

后端 未结 7 792
攒了一身酷
攒了一身酷 2020-12-28 15:50

When should i make a function private and why is it good idea?

7条回答
  •  既然无缘
    2020-12-28 16:28

    One of the founding principals of OOP is encapsulation. This is where functionality for how an object works is kept internal to that object. The idea is that it's easier for code to use an object if it doesn't need to know how it works. Kind of like buying a microwave--you just need to know how to use it and not how it works.

    The same approach should be taken with OOP. Keep everything needed to maintain the object private. Make only what is needed to fully use the object public.

提交回复
热议问题