📜  Dart继承的概念

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

在Dart,一个类可以继承另一个类,即dart可以从现有类创建一个新类。我们使用扩展关键字来做到这一点。

术语:

  • 父类:它是其属性由子类继承的类。它也称为基类或超类。
  • 子类:它是继承其他类的属性的类。它也被称为被剥夺的类或子类。
class parent_class{
...
}

class child_class extends parent_class{
...
}

示例 1: dart的单继承示例。

Dart
// Dart program to show the single inheritance
  
// Creating parent class
class Gfg{
    
  // Creating a function
  void output(){
    print("Welcome to gfg!!\nYou are inside output function.");
  }
}
  
// Creating Child class
class GfgChild extends Gfg{
  // We are not defining
  // any thing inside it...
}
void main() {
  // Creating object of GfgChild class
  var geek = new GfgChild();
    
  // Calling function
  // inside Gfg(Parent class)
  geek.output();
}


Dart
// Dart program for multilevel interitance
  
// Creating parent class
class Gfg{
    
  // Creating a function
  void output1(){
    print("Welcome to gfg!!\nYou are inside the output function of Gfg class.");
  }
}
  
// Creating Child1 class
class GfgChild1 extends Gfg{
    
  // Creating a function
  void output2(){
    print("Welcome to gfg!!\nYou are inside the output function of GfgChild1 class.");
  }
}
  
// Creating Child2 class
class GfgChild2 extends GfgChild1{
  // We are not defining
  // any thing inside it...
}
  
void main() {
  // Creating object
  // of GfgChild class
  var geek = new GfgChild2();
    
  // Calling function
  // inside Gfg
  //(Parent class of Parent class)
  geek.output1();
    
  // Calling function
  // inside GfgChild
  // (Parent class)
  geek.output2();
}


Dart
// Dart program for Hierarchical inheritance
  
// Creating parent class
class Gfg{
    
  // Creating a function
  void output1(){
    print("Welcome to gfg!!\nYou are inside output function of Gfg class.");
  }
}
  
// Creating Child1 class
class GfgChild1 extends Gfg{
  // We are not defining
  // any thing inside it...
}
  
// Creating Child2 class
class GfgChild2 extends Gfg{
    
  // We are not defining
  // any thing inside it...
}
  
void main() {
  // Creating object
  // of GfgChild1 class
  var geek1 = new GfgChild1();
    
  // Calling function
  // inside Gfg(Parent class)
  geek1.output1();
    
  // Creating object of
  // GfgChild1 class
  var geek2 = new GfgChild2();
    
  // Calling function
  // inside Gfg(Parent class)
  geek2.output1();
}


输出:

Welcome to gfg!!
You are inside output function.

继承类型:

  1. 单一继承:当一个类继承一个单一的父类时,就会发生这种继承。
  2. 多重继承:当一个类继承多个父类时,就会发生这种继承。 Dart不支持这一点。
  3. 多级继承:当一个类继承另一个子类时,会发生这种继承。
  4. 分层继承:多个类具有相同的父类。

要点:

  1. 子类继承除父类的构造函数之外的所有属性和方法。
  2. 与Java不同, Dart也不支持多重继承。

示例 2:

Dart

// Dart program for multilevel interitance
  
// Creating parent class
class Gfg{
    
  // Creating a function
  void output1(){
    print("Welcome to gfg!!\nYou are inside the output function of Gfg class.");
  }
}
  
// Creating Child1 class
class GfgChild1 extends Gfg{
    
  // Creating a function
  void output2(){
    print("Welcome to gfg!!\nYou are inside the output function of GfgChild1 class.");
  }
}
  
// Creating Child2 class
class GfgChild2 extends GfgChild1{
  // We are not defining
  // any thing inside it...
}
  
void main() {
  // Creating object
  // of GfgChild class
  var geek = new GfgChild2();
    
  // Calling function
  // inside Gfg
  //(Parent class of Parent class)
  geek.output1();
    
  // Calling function
  // inside GfgChild
  // (Parent class)
  geek.output2();
}

输出:

Welcome to gfg!!
You are inside the output function of Gfg class.
Welcome to gfg!!
You are inside the output function of GfgChild1 class.

示例 3:分层继承。

Dart

// Dart program for Hierarchical inheritance
  
// Creating parent class
class Gfg{
    
  // Creating a function
  void output1(){
    print("Welcome to gfg!!\nYou are inside output function of Gfg class.");
  }
}
  
// Creating Child1 class
class GfgChild1 extends Gfg{
  // We are not defining
  // any thing inside it...
}
  
// Creating Child2 class
class GfgChild2 extends Gfg{
    
  // We are not defining
  // any thing inside it...
}
  
void main() {
  // Creating object
  // of GfgChild1 class
  var geek1 = new GfgChild1();
    
  // Calling function
  // inside Gfg(Parent class)
  geek1.output1();
    
  // Creating object of
  // GfgChild1 class
  var geek2 = new GfgChild2();
    
  // Calling function
  // inside Gfg(Parent class)
  geek2.output1();
}

输出:

Welcome to gfg!!
You are inside output function of Gfg class.
Welcome to gfg!!
You are inside output function of Gfg class.