Dart Multiple Constructors

后端 未结 7 1682
一向
一向 2021-02-03 16:51

Is it really not possible to create multiple constructors for a class in dart?

in my Player Class, If I have this constructor

Player(String name, int col         


        
相关标签:
7条回答
  • 2021-02-03 17:00

    If your class uses final parameters the accepted answer will not work. This does:

    class Player {
      final String name;
      final String color;
    
      Player(this.name, this.color);
    
      Player.fromPlayer(Player another) :
        color = another.color,
        name = another.name;
    }
    
    0 讨论(0)
  • 2021-02-03 17:03

    If you already used a constructor with params in the project and now you figured out that you need some no params default constructor you can add an empty constructor.

    class User{
    String name;
    
       User({this.name}); //This you already had before
       User.empty(); //Add this later 
    }
    
    0 讨论(0)
  • 2021-02-03 17:03

    Try the below code on DartPad

    class MyClass {
      //These two are private attributes
      int _age;
      String _name;
    
      //This is a public attribute
      String defaultName = "My Default Name!";
    
      //Default constructor
      MyClass() {
        _age = 0;
        _name = "Anonymous";
      }
    
      MyClass.copyContructor(MyClass fromMyClass) {
        this._age = fromMyClass._age;
        this._name = fromMyClass._name;
      }
    
      MyClass.overloadedContructor(String name, int age) {
        this._age = age;
        this._name = name;
      }
    
      MyClass.overloadedContructorNamedArguemnts({String name, int age}) {
        this._age = age;
        this._name = name;
      }
    
      //Overriding the toString() method
      String toString() {
        String retVal = "Name:: " + _name + " | " + "Age:: " + _age.toString();
        return retVal;
      }
    }
    
    //The execution starts from here..
    void main() {
      MyClass myClass1 = new MyClass();
    
      //Cannot access oprivate attributes
      //print(myClass1.name);
      //print(myClass1.age);
    
      //Can access the public attribute
      print("Default Name:: " + myClass1.defaultName);
    
      print(myClass1.toString());
    
      MyClass myClass2 = new MyClass.copyContructor(myClass1);
    
      print(myClass2.toString());
    
      MyClass myClass3 = new MyClass.overloadedContructor("Amit", 42);
    
      print(myClass3.toString());
    
      MyClass myClass4 =
          new MyClass.overloadedContructorNamedArguemnts(age: 42, name: "Amit");
    
      print(myClass4.toString());
    }
    
    0 讨论(0)
  • 2021-02-03 17:05

    i had found solution to solve this problem depend on checked the type of data you are passed it to function

    Try this Solution

    0 讨论(0)
  • 2021-02-03 17:12

    How about the case you want to have more than one constructor. For example, there are 2 constructors you desire to use:

    Customer(String name, int age, String location) {
      this.name = name;
      this.age = age;
      this.location = location;
    }
    
    Customer(this.name, this.age) {
      this.name = name;
      this.age = age;
    }
    

    But if you define both of them in a class, there will be a compiler error.

    Dart provides Named constructor that helps you implement multiple constructors with more clarity:

    class Customer {
      // ...
    
      Customer(String name, int age, String location) {
        this.name = name;
        this.age = age;
        this.location = location;
      }
    
      // Named constructor - for multiple constructors
      Customer.withoutLocation(this.name, this.age) {
        this.name = name;
        this.age = age;
      }
    
      Customer.empty() {
        name = "";
        age = 0;
        location = "";
      }
    
      @override
      String toString() {
        return "Customer [name=${this.name},age=${this.age},location=${this.location}]";
      }
    }
    

    You can write it more simply with Syntactic sugar:

    Customer(this.name, this.age, this.location);
    
    Customer.withoutLocation(this.name, this.age);
    
    Customer.empty() {
      name = "";
      age = 0;
      location = "";
    }
    

    Now we can create new Customer object by these methods.

    var customer = Customer("bezkoder", 26, "US");
    print(customer);
    // Customer [name=bezkoder,age=26,location=US]
    
    var customer1 = Customer.withoutLocation("zkoder", 26);
    print(customer1);
    // Customer [name=zkoder,age=26,location=null]
    
    var customer2 = Customer.empty();
    print(customer2);
    // Customer [name=,age=0,location=]
    

    So, is there any way to make Customer.empty() neat? And how to initialize an empty value for location field when calling Customer.withoutLocation() instead of null?

    From : Multiple Constructors

    0 讨论(0)
  • 2021-02-03 17:15

    You can only have one unnamed constructor, but you can have any number of additional named constructors

    class Player {
      Player(String name, int color) {
        this._color = color;
        this._name = name;
      }
    
      Player.fromPlayer(Player another) {
        this._color = another.getColor();
        this._name = another.getName();
      }  
    }
    
    new Player.fromPlayer(playerOne);
    

    This constructor can be simplified

      Player(String name, int color) {
        this._color = color;
        this._name = name;
      }
    

    to

      Player(this._name, this._color);
    

    Named constructors can also be private by starting the name with _

    class Player {
      Player._(this._name, this._color);
    
      Player._foo();
    }
    

    Constructors with final fields initializer list are necessary:

    class Player {
      final String name;
      final String color;
    
      Player(this.name, this.color);
    
      Player.fromPlayer(Player another) :
        color = another.color,
        name = another.name;
    }
    
    0 讨论(0)
提交回复
热议问题