Last active
November 23, 2021 00:56
-
-
Save felipecastrosales/309e0d6843300c2d9fd66f9abeceaf2d to your computer and use it in GitHub Desktop.
POO - Construtores.dart
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
// Tipos de Construtores: | |
// 1. `Pessoa()`: construtor padrão (default constructor). | |
// Uso → `pessoa.nome` | `pessoa.idade`. | |
// 2. `Pessoa2(this.nome, this.idade)`: construtor parametrizado (parameterized constructor). | |
// Uso → `Pessoa('Felipe', 17)`. | |
// 3. `Pessoa({required this.nome, required this.idade})`: construtor nomeado (named constructor). | |
// Uso → `Pessoa(nome: 'Felipe', idade: 17)`. | |
void main() { | |
// 1. | |
var pessoa1 = Pessoa1(); | |
print(pessoa1.nome); | |
print(pessoa1.idade); | |
// 2. | |
var pessoa2 = Pessoa2('Felipe', 17); | |
print(pessoa2.nome); | |
print(pessoa2.idade); | |
// 3. | |
var pessoa3 = Pessoa3(nome: 'Felipe', idade: 17); | |
print(pessoa3.nome); | |
print(pessoa3.idade); | |
} | |
// 1. | |
class Pessoa1 { | |
String nome = 'Felipe'; | |
int idade = 17; | |
} | |
// 2. | |
class Pessoa2 { | |
String nome; | |
int idade; | |
Pessoa2(this.nome, this.idade); | |
} | |
// 3. | |
class Pessoa3 { | |
String nome; | |
int idade; | |
Pessoa3({required this.nome, required this.idade}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment