Answers for "dart positional arguments vs named arguments"

0

dart positional arguments vs named arguments

void main(){

  var catBob = Cat('Bob', 'Grey');
  
  // display object with positional argument 
  print(catBob.displayCat());
  
  
  // display object with constructor and named arguments
  print(Dog(breed: 'Bulldog', name: 'Greg', color: 'Purple').displayDog());
}



// Cat class - with simple constructor and positional arguments
class Cat{

  // properties
  String name;
  String color;
  
  // simple constructor
  Cat(this.name, this.color);
  
  // method to display cat
  String displayCat() {
    return 'my cat is ${name} and he is ${color}';
  }

}

// Dog class - with constructor and named arguments
class Dog{
  String name;
  String breed;
  String color;
  
  // constructor with named arguments
  Dog({this.name, this.breed, this.color});
  
  // method to display cat
  String displayDog() {
    // interpolate 'breed, name, color' with ${ value }
    return 'my ${breed} is ${name} and he is ${color}';
  }
  
}
Posted by: Guest on February-12-2021

Code answers related to "dart positional arguments vs named arguments"

Code answers related to "Dart"

Browse Popular Code Answers by Language