Created
November 23, 2021 02:30
-
-
Save felipecastrosales/a5df36cc51f795440a9a284527b434e0 to your computer and use it in GitHub Desktop.
POO - Getters e Setters
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
void main() { | |
var pessoa = Pessoa(); | |
pessoa.altura = 1.85; // ok | |
pessoa.altura = 5.00; // > 2.5 → 'Altura inválida' | |
} | |
class Pessoa { | |
double? _altura; | |
double? get altura => _altura; | |
set altura(double? altura) { | |
if (altura != null && altura > 0 && altura < 2.5) { | |
_altura = altura; | |
} else { | |
print('Altura inválida'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment