📜  Dart的接口

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

dart的接口为用户提供了类的蓝图,如果任何类与该类接口,则应遵循该蓝图,即如果一个类继承另一个类,则应以它的方式重新定义接口类中存在的每个函数。它们只不过是为对象定义的一组方法。 Dart没有任何直接的方法来创建继承类,我们必须使用implements关键字来实现。

句法:

class Interface_class_name{
   ...
}

class Class_name implements Interface_class_name {
   ...
}

示例 1:

Dart
void main(){
    
   // Creating Object
   // of the class Gfg
   Gfg geek1= new Gfg();
    
   // Calling method
   // (After Implementation )
   geek1.printdata(); 
}
  
// Class Geek (Interface)
class Geek { 
   void printdata() { 
      print("Hello Geek !!"); 
   } 
}  
  
// Class Gfg implementing Geek
class Gfg implements Geek { 
   void printdata() {  
      print("Welcome to GeeksForGeeks"); 
   } 
}


Dart
// Dart Program to show Multiple Inheritance 
void main(){
    
   // Creating Object of
   // the class Gfg
   Gfg geek1= new Gfg();
    
   // Calling method (After Implementation )
   geek1.printdata1(); 
   geek1.printdata2(); 
   geek1.printdata3(); 
}
  
// Class Geek1 (Interface1)
class Geek1 { 
   void printdata1() { 
      print("Hello Geek1 !!"); 
   } 
}  
 // Class Geek2 (Interface2)
class Geek2 { 
   void printdata2() { 
      print("Hello Geek2 !!"); 
   } 
}  
 // Class Geek3 (Interface3)
class Geek3 { 
   void printdata3() { 
      print("Hello Geek3 !!"); 
   } 
}  
  
// Class Gfg implementing Geek1, Geek2, Geek3.
class Gfg implements Geek1, Geek2, Geek3 { 
   void printdata1() {  
      print("Howdy Geek1,\nWelcome to GeeksForGeeks"); 
   } 
    
  void printdata2() {  
      print("Howdy Geek2,\nWelcome to GeeksForGeeks"); 
   } 
    
  void printdata3() {  
      print("Howdy Geek3,\nWelcome to GeeksForGeeks"); 
   }   
}


输出:

Welcome to GeeksForGeeks

注意: Class 应该使用implements关键字,而不是通过扩展来使用接口方法。

Dart的多重继承

在dart,通过使用实现来实现多重继承。虽然实际上dart不支持多重继承,但它支持多个接口。

句法:

class interface_class1 {
    ...
}
class interface_class2 {
    ...
}
.
.
.
.
class interface_classN {
    ...
}

class class_name implements interface_class1, interface_class2, ...., interface_classN {
    ...
}

例子:

Dart

// Dart Program to show Multiple Inheritance 
void main(){
    
   // Creating Object of
   // the class Gfg
   Gfg geek1= new Gfg();
    
   // Calling method (After Implementation )
   geek1.printdata1(); 
   geek1.printdata2(); 
   geek1.printdata3(); 
}
  
// Class Geek1 (Interface1)
class Geek1 { 
   void printdata1() { 
      print("Hello Geek1 !!"); 
   } 
}  
 // Class Geek2 (Interface2)
class Geek2 { 
   void printdata2() { 
      print("Hello Geek2 !!"); 
   } 
}  
 // Class Geek3 (Interface3)
class Geek3 { 
   void printdata3() { 
      print("Hello Geek3 !!"); 
   } 
}  
  
// Class Gfg implementing Geek1, Geek2, Geek3.
class Gfg implements Geek1, Geek2, Geek3 { 
   void printdata1() {  
      print("Howdy Geek1,\nWelcome to GeeksForGeeks"); 
   } 
    
  void printdata2() {  
      print("Howdy Geek2,\nWelcome to GeeksForGeeks"); 
   } 
    
  void printdata3() {  
      print("Howdy Geek3,\nWelcome to GeeksForGeeks"); 
   }   
} 

输出:

Howdy Geek1,
Welcome to GeeksForGeeks
Howdy Geek2,
Welcome to GeeksForGeeks
Howdy Geek3,
Welcome to GeeksForGeeks

接口的重要性:

  1. 用于在Dart实现抽象。
  2. 它是在Dart实现多重继承的一种方式。

要点:

  1. 如果一个类已经被实现,那么它的所有方法和实例变量都必须在接口期间被覆盖。
  2. 在dart,没有直接的方法来声明接口,因此类的声明本身被视为对接口的声明。
  3. 一个类只能扩展一个类,但可以实现任意多个类。