Dart Constructor

class Person {

  String name="";
  int age=0;
  
  Person(String inputName, int inputAge){
    this.name = inputName;
    this.age = inputAge;
  }//Person()
}//Person

void main() {

  var p1 = Person("Rizwan", 34);
  var p2 = Person("Ali", 32);
  
  print(p1.name); //Rizwan
  print(p1.age);  //34
  
  print(p2.name); //Ali
  print(p2.age);  //32
}

In this constructor we are using positional arguments i.e, inputName and inputAge instead of named arguments.

Comments

comments

no responses for Dart Constructor

    Leave a Reply

    Your email address will not be published. Required fields are marked *