课程: Treating Go as an Object-Oriented Language
Object-oriented programming
- [Instructor] Let's start with a discussion as a refresher to object-oriented programming, or OOP. And since you're taking this class I'm assuming that you know all of this, but it's worth at least talking through quickly. So let's begin with the core of what OOP is all about. The primary focus of object-oriented programming as a paradigm is that you focus on objects and not procedures. Data is stored in those objects in the form of attributes. Procedures then attach to the objects and can, and most often do, interact with the attributes of the object, even if those attributes are protected or private from the public world. There are really four pillars of the object-oriented paradigm for programming. The first is abstraction. Abstraction is essentially making the complex simple. Abstraction allows the developer to hide the complexity of a procedure, usually many steps in a very specific order, behind a single abstracted procedure itself. The next is encapsulation. Data stored in classes or objects can be hidden from any callable procedure. This allows the programmer to protect data, allowing it to only be accessed or modified using very specific code paths that they control. Objects also have the concept of inheritance. Each object can inherit the properties and non-final methods of a parent object. In fact, there is always a base object that all other objects inherit from, and this actually can cause a problem in object-oriented programming. Now, one of the most powerful features is polymorphism. With polymorphism, different objects that share a common set of methods, either through an interface or through super classes, can exhibit different behavior based on the object itself, not some external procedure, as they are unique types. Now, this is so powerful that patterns have actually developed around polymorphism. Things like the factory pattern where you can dynamically get an object and then just call its methods, and the behavior will change based on the type that was dynamically created. This allows the developer to do a lot of things with very simple code paths, and it's often one of the least utilized but most important aspects of object-oriented programming.