Home > Blockchain >  class object and property variation in dart
class object and property variation in dart

Time:10-29

so i recently started learning dart and I've found something kinda interesting.

why do we use constructors and getters/setters when we can achieve same results without them? (atleast when used for basic things).

class v1{
  var name;
  int age;
  
  v1(this.name, this.age);
  
  info(){
    print("my name is $name and i am $age");
  }
}

class v2{
  var name = "bash";
  int age = 100;
  
  info(){
    print("my name is $name and i am $age");
  }
}

class v3{
  var namee;
  int agee;
  
  String get name => namee;
  int get age => agee;
  
  set name(String name) => this.namee = name;
  set age(int age) => this.agee = age;
  
  info(){
    print("my name is $name and i am $age");
  }
}


void main(){
  var x = v1("bash", 100);
  x.info(); //my name is bash am i am 100
  
  var z = v2();
  var Z = v2();
  Z.name = "vert";
  Z.age = 20;
  z.info(); //my name is bash and i am 100
  Z.info(); //my name is vert and i am 100
  
  var y = v3();
  y.name = "rizz";
  y.age = 40;
  y.info(); //my name is rizz and i am 40
}

CodePudding user response:

Here's a more correct version of your class:

class User {
  final bool _isMale;
  String _name;
  int _age;

  User(this._isMale, this._name, this._age);

  bool isMale => _isMale;
  String get name => _name;
  int get age => _age;

  set name(String name) {
    // Sometimes you may want to update other properties here.
    // For example:
    // _isUpdated = true;
    _name = name;
  }

  set age(int age) {
    _age = age;
  }

  void info() {
    print("my name is $name and i am $age");
  }
}

Constructors are useful when you want to assign initial values to the class fields. They are essential if you need to assign final fields, as they are assignable only on class initialization (see _isMale field).

Setters are useful when you want to update other fields along with the field that's being modified.

Getters protect the internal state from being modified outside. In this example, nobody can change _isMale field.

CodePudding user response:

You don't need to use getters and setters unless you have to.

You use getters and setters if you need to store the data in a private field, or if you want to modify it when saving or returning the value.

class Abc {
    String _field;
    String _otherField;

    String anotherField; // No getters and setters required for this.

    String get field => _field;
    set field(String field) => _field = field;

    String get otherField => "The value of otherField is: "   _otherField;
    set otherField(String otherField) => _otherField = "[String] "   otherField;
}

As for constructors, you use them to initialize the object with custom values. When you need to work with immutable objects (which use final variables), you'll have to use constructors to set their initial value. You can also modify the incoming value according to your need before storing it,

class Def {
    final field; // Dart generates getters for this field, but it's value can't be modified once the object is instantiated.
    final _otherField; // No getters for this.
    Def(String field, String otherField) {
        this.field = "[String] $field"
        this._otherField = "[String] $otherField"
    }

    String describeMe() {
        return "[Def]: field: $field, _otherField: $_otherField"
    }
}
  • Related