课程: Treating Go as an Object-Oriented Language

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Encapsulation

Encapsulation

- [Instructor] Encapsulation is a primary pillar of object-oriented programming, and something that we get in Go as well. Encapsulation is a very important concept, especially when writing libraries. It allows you to provide protection to your internal data and logic. Think about our dog example, everything was exposed as we saw from the main method itself. We could get access to all of the data and all of the methods. Encapsulation is a way to protect that. Data contained in structs can be protected from access outside the package itself in the same way methods on a struct can be protected. It's important to note that interfaces which we will talk about later, can have protected, or hidden methods, which is different than you see in interfaces in some other languages, specifically Java. Another benefit and one that goes along with protection is that encapsulation allows you to control the flow of data, both the setting…

内容