metadeck
10/5/2018 - 10:55 AM

Beginners Dart - Scope Getters and Setters

Beginners Dart - Scope Getters and Setters

class AnimalWithGettersAndSetters {

  String _name;
  int _age = 0;

  AnimalWithGettersAndSetters(String name, int age) {

    _name = name;
    _age = age * 7;

  }

  String get name => _name;
  void set name(String value) =>  _name = value;

  int get age => _age;
  void set age(int value) => _age = value * 7;

}

void main() {
 
  AnimalWithGettersAndSetters dog = new AnimalWithGettersAndSetters("Ringo", 7);
  dog.name = "Spike"; //Setter.
  print(dog.name); //Getter.

  print("Before setter ${dog.age}"); //Getter.
  dog.age = 4; //Setter.
  print("After setter ${dog.age}"); //Getter.
  
}