-
Notifications
You must be signed in to change notification settings - Fork 111
docs: adiciona conteúdos de polimorfismo e encapsulamento #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Washington83
wants to merge
4
commits into
danielhe4rt:master
Choose a base branch
from
Washington83:feature/featureBraba
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # 4.6 - Encapsulamento | ||
|
|
||
| ## O que é? | ||
|
|
||
| Encapsulamento é o conceito de **proteger os dados de uma classe**, controlando como eles são acessados e modificados. | ||
|
|
||
| Ou seja: | ||
| Você **esconde os atributos** e permite acesso apenas através de métodos. | ||
|
|
||
| --- | ||
|
|
||
| ## Por que usar? | ||
|
|
||
| Para garantir que os dados: | ||
|
|
||
| * Não sejam alterados de forma errada | ||
| * Tenham regras de validação | ||
| * Fiquem mais seguros | ||
|
|
||
| --- | ||
|
|
||
| ## Ideia simples | ||
|
|
||
| Pensa em uma conta bancária: | ||
|
|
||
| * Você não altera o saldo direto ❌ | ||
| * Você usa métodos como `depositar()` ou `sacar()` ✔️ | ||
|
|
||
| > Isso é encapsulamento. | ||
|
|
||
| --- | ||
|
|
||
| ## Exemplo sem encapsulamento (errado) | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| class ContaBancaria { | ||
| public $saldo = 1000; | ||
| } | ||
|
|
||
| $conta = new ContaBancaria(); | ||
| $conta->saldo = -5000; // ❌ valor inválido | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Exemplo com encapsulamento (correto) | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| class ContaBancaria { | ||
|
|
||
| private $saldo = 1000; | ||
|
|
||
| public function depositar($valor) | ||
| { | ||
| if ($valor > 0) { | ||
| $this->saldo += $valor; | ||
| } | ||
| } | ||
|
|
||
| public function sacar($valor) | ||
| { | ||
| if ($valor > 0 && $valor <= $this->saldo) { | ||
| $this->saldo -= $valor; | ||
| } | ||
| } | ||
|
|
||
| public function getSaldo() | ||
| { | ||
| return $this->saldo; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| $conta = new ContaBancaria(); | ||
| $conta->depositar(500); | ||
| $conta->sacar(200); | ||
|
|
||
| echo $conta->getSaldo(); // 1300 | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## O que está acontecendo? | ||
|
|
||
| * `saldo` é **private** → não pode ser acessado diretamente | ||
| * Só pode ser alterado por métodos controlados | ||
| * Existem regras (`if`) para evitar erros | ||
|
|
||
| --- | ||
|
|
||
| ## Resumão | ||
|
|
||
| * Encapsular = **esconder os dados** | ||
| * Acesso só por métodos (getters/setters) | ||
| * Mais segurança e controle | ||
|
|
||
| --- | ||
|
|
||
| ## 🧩 Frase pra guardar | ||
|
|
||
| > **Encapsulamento é proteger os dados e controlar como eles são acessados.** | ||
|
|
||
| --- | ||
|
|
||
| ## 🔗 Relação com outros conceitos | ||
|
|
||
| * Usa `private` e `protected` (modificadores de acesso) | ||
| * Trabalha junto com getters e setters | ||
| * Base para código seguro e profissional | ||
|
|
||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| # 4.5 - Polimorfismo | ||
|
|
||
| ## O que é? | ||
|
|
||
| Polimorfismo é a capacidade de um mesmo método ter **comportamentos diferentes**, dependendo da classe que o implementa. | ||
|
|
||
| > Ou seja: | ||
| Você chama **o mesmo método**, mas o resultado muda. | ||
|
|
||
| --- | ||
|
|
||
| ## Por que usar? | ||
|
|
||
| Para evitar vários `if/else` e deixar o código: | ||
|
|
||
| * Mais organizado | ||
| * Mais fácil de manter | ||
| * Mais profissional | ||
|
|
||
| --- | ||
|
|
||
| ## Ideia simples ! | ||
|
|
||
| Você tem um método chamado `emitirSom()`: | ||
|
|
||
| * Cachorro → **Au au** | ||
| * Gato → **Miau** | ||
|
|
||
| > Mesmo método, comportamentos diferentes. | ||
|
|
||
| --- | ||
|
|
||
| ## Exemplo prático | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| class Animal { | ||
|
|
||
| public function emitirSom() | ||
| { | ||
| echo "Som genérico"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Cachorro extends Animal { | ||
|
|
||
| public function emitirSom() | ||
| { | ||
| echo "Au au"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Gato extends Animal { | ||
|
|
||
| public function emitirSom() | ||
| { | ||
| echo "Miau"; | ||
| } | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Utilizando | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| $animal1 = new Cachorro(); | ||
| $animal2 = new Gato(); | ||
|
|
||
| echo $animal1->emitirSom() . PHP_EOL; | ||
| echo $animal2->emitirSom() . PHP_EOL; | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Outro exemplo (mais real) | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| class Pagamento { | ||
|
|
||
| public function pagar($valor) | ||
| { | ||
| echo "Pagamento genérico de R$ $valor"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Pix extends Pagamento { | ||
|
|
||
| public function pagar($valor) | ||
| { | ||
| echo "Pagamento via PIX de R$ $valor"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Cartao extends Pagamento { | ||
|
|
||
| public function pagar($valor) | ||
| { | ||
| echo "Pagamento no cartão de R$ $valor"; | ||
| } | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
| ### Usando: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| $pagamentos = [ | ||
| new Pix(), | ||
| new Cartao() | ||
| ]; | ||
|
|
||
| foreach ($pagamentos as $pagamento) { | ||
| $pagamento->pagar(100) . PHP_EOL; | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Sem polimorfismo | ||
|
|
||
| ```php | ||
| if ($tipo == 'pix') { | ||
| echo "Pagamento via PIX"; | ||
| } elseif ($tipo == 'cartao') { | ||
| echo "Pagamento no cartão"; | ||
| } | ||
| ``` | ||
|
|
||
| ❌ Difícil de manter | ||
| ❌ Cresce rápido demais | ||
|
|
||
| --- | ||
|
|
||
| ## Com polimorfismo | ||
|
|
||
| ```php | ||
| $pagamento->pagar(100); | ||
| ``` | ||
|
|
||
| ✔️ Limpo | ||
| ✔️ Reutilizável | ||
| ✔️ Escalável | ||
|
|
||
| --- | ||
|
|
||
| ## Resumão !!! | ||
|
|
||
| * Mesmo método (`pagar`, `emitirSom`) | ||
| * Classes diferentes | ||
| * Resultados diferentes | ||
|
|
||
| --- | ||
|
|
||
| ## 🧩 Frase pra guardar | ||
|
|
||
| > **Polimorfismo é usar o mesmo método com comportamentos diferentes em objetos diferentes.** | ||
|
|
||
| --- | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Padronize os headings ATX para remover espaço duplo após
##.As linhas 12, 33 e 82 violam a regra MD019 (
no-multiple-space-atx).💡 Ajuste sugerido
Also applies to: 33-33, 82-82
🧰 Tools
🪛 markdownlint-cli2 (0.21.0)
[warning] 12-12: Multiple spaces after hash on atx style heading
(MD019, no-multiple-space-atx)
🤖 Prompt for AI Agents