📜  Dart的构造函数

📅  最后修改于: 2021-09-02 05:46:06             🧑  作者: Mango

Dart还提供了构造函数的支持。构造函数是一种特殊的方法,用于在程序中创建对象时对其进行初始化。在面向对象编程中,当一个对象被创建时,它会自动调用构造函数。所有类都有自己的默认构造函数,它是在调用类时由编译器创建的,而且还可以定义自己的构造函数。但是,您必须注意,如果您这样做,则默认构造函数将不会被创建并且将被忽略。

Dart的构造函数构造函数与类名同名,没有任何返回类型。

class_name( [ parameters ] ){
    // Constructor Body
}

在上面的语法中:

  1. class_name是正在创建其构造函数的类的名称。
  2. 参数是可选特性,它们可以也不能为构造函数定义。默认构造函数中没有定义参数。
  3. 构造函数主体是构造函数的主体,在调用构造函数时即在创建对象时执行。
  4. 构造函数没有任何返回类型。

示例 1:在Dart创建构造函数

Dart
// Dart Program to create a constructor
  
// Creating Class named Gfg
class Gfg{
    
  // Creating Constructor
  Gfg() {
      
    // Whenever constructor is called
    // this statement will run
    print('Constructor is being created');
  }
    
  // Creating Field inside the class
  String geek1;
    
  // Creating Function inside class
  void geek(){
    print("Welcome to $geek1");
  }
}
  
void main() {
    
  // Creating Instance of class
  Gfg geek = new Gfg();
    
  // Calling field name geek1
  // and assigning value to it
  // using object of the class Gfg
  geek.geek1 = 'GeeksforGeeks';
    
  // Calling function name
  // geek using object
  // of the class Gfg
  geek.geek();
}


Dart
// Dart program to illustrate
// the Default constructor
  
// Creating Class named Gfg
class Gfg{
    
  // Creating Constructor
  Gfg() {
    print('This is the default constructor');
  }
}
  
void main() {
    
  // Creating Instance of class 
  Gfg geek = new Gfg();  
}


Dart
// Creating parameterized constructor in Dart
  
// Creating Class named Gfg
class Gfg{
    
  // Creating Parameterized Constructor
  Gfg(int a) {
      
    print('This is the parameterized constructor');
  }
}
  
void main() {
    
  // Creating Instance of class 
  Gfg geek = new Gfg(1);  
}


Dart
// Creating named constructor in Dart 
  
// Creating Class named Gfg
class Gfg{
    
  // Creating named and
  // parameterized Constructor
  // with one parameter
  Gfg.constructor1(int a) {
    print('This is the parameterized constructor with only one parameter');
  }
  // Creating named and
  // parameterized Constructor
  // with two parameter
  Gfg.constructor2(int a, int b) {
    print('This is the parameterized constructor with two parameters');
    print('Value of a + b is ${a + b}');
  }
}
  
void main() {
  // Creating Instance of class
  Gfg geek1 = new Gfg.constructor1(1); 
  Gfg geek2 = new Gfg.constructor2(2, 3); 
}


输出:

Constructor is being created
Welcome to GeeksforGeeks

Dart有三种类型的构造函数:

1. 默认构造函数:默认构造函数是那些没有任何参数的构造函数。因此,如果一个构造函数没有任何参数,那么它将是一种默认构造函数。

示例:在Dart创建默认构造函数

Dart

// Dart program to illustrate
// the Default constructor
  
// Creating Class named Gfg
class Gfg{
    
  // Creating Constructor
  Gfg() {
    print('This is the default constructor');
  }
}
  
void main() {
    
  // Creating Instance of class 
  Gfg geek = new Gfg();  
}

输出:

This is the default constructor

2. 参数化构造函数:在Dart,您还可以创建具有一些参数的构造函数。这些参数将决定调用哪个构造函数,不调用哪个构造函数。那些接受参数的构造函数称为参数化构造函数

例子:

Dart

// Creating parameterized constructor in Dart
  
// Creating Class named Gfg
class Gfg{
    
  // Creating Parameterized Constructor
  Gfg(int a) {
      
    print('This is the parameterized constructor');
  }
}
  
void main() {
    
  // Creating Instance of class 
  Gfg geek = new Gfg(1);  
}

输出:

This is the parameterized constructor

注意:您不能有两个同名的构造函数,尽管它们具有不同的参数。编译器会显示错误。

3.命名构造函数:由于不能定义多个同名的构造函数,所以这种类型的构造函数是解决问题的方法。它们允许用户使用不同的名称创建多个构造函数。

class_name.constructor_name ( parameters ){
   // Body of Constructor
}

例子:

Dart

// Creating named constructor in Dart 
  
// Creating Class named Gfg
class Gfg{
    
  // Creating named and
  // parameterized Constructor
  // with one parameter
  Gfg.constructor1(int a) {
    print('This is the parameterized constructor with only one parameter');
  }
  // Creating named and
  // parameterized Constructor
  // with two parameter
  Gfg.constructor2(int a, int b) {
    print('This is the parameterized constructor with two parameters');
    print('Value of a + b is ${a + b}');
  }
}
  
void main() {
  // Creating Instance of class
  Gfg geek1 = new Gfg.constructor1(1); 
  Gfg geek2 = new Gfg.constructor2(2, 3); 
}

输出:

This is the parameterized constructor with only one parameter
This is the parameterized constructor with two parameters
Value of a + b is 5