51 - Orientação Objetos - Métodos pt 08 - Referência this
Introduction
The video begins with a greeting and introduction to the topic of references in programming.
Understanding References
- When executing code, it is necessary to call the reference variable to access data located within an object.
- By using the reference variable, we can assign values and access attributes within the object.
- The reference variable acts as a control remote, allowing us to activate buttons such as name, sex, age, and print.
Implementing Reference Variables
The instructor demonstrates how to implement reference variables in code.
Using Reference Variables
- To utilize a reference variable, we need to declare it within our object.
- We can then call methods or attributes using the reference variable.
- This allows us to access all available attributes without creating additional variables.
Accessing Object Attributes
The instructor explains how to refer to object attributes using reserved keywords.
Referring to Object Attributes
- To refer to an attribute within an object, we use the reserved keyword "this".
- By using "this", we can easily access attributes such as name, city, and sex.
- We can directly print these attributes by calling them through the reference variable.
Initializing Objects with Reference Variables
The instructor demonstrates how to initialize objects with reference variables.
Initializing Objects
- Two objects are initialized using the same class.
- Each object has its own set of attribute values.
- Both objects act as control remotes for accessing their respective attribute values.
Executing Methods with Reference Variables
The instructor explains how methods are executed using reference variables.
Executing Methods
- When calling a method through a reference variable, the object associated with that variable executes the method.
- By calling the print method on different objects, we can access their respective attribute values.
Multiple Reference Variables
The instructor discusses how multiple reference variables can control the same object.
Controlling Objects with Multiple Reference Variables
- Multiple reference variables can control the same object.
- These variables act as separate control remotes for accessing and manipulating attributes of the shared object.
Benefits of Using References
The instructor explains the benefits of using references in programming.
Benefits of Using References
- Using references allows for better code organization and cohesion.
- It eliminates the need for separate classes solely responsible for printing data.
- References provide a clear distinction between objects and their associated methods or attributes.
Maintaining High Cohesion
The instructor emphasizes maintaining high cohesion by separating responsibilities in code design.
Separating Responsibilities
- It is important to separate responsibilities within code design to maintain high cohesion.
- Printing data should be handled separately from other functionalities within an object-oriented system.