📜  Dart – 使用 Vs 工具扩展 Vs

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

所有使用dart使用Flutter框架进行应用程序开发的开发人员都会经常遇到工具、扩展和关键字的不同用法。在Dart,一个类可以继承另一个类,即dart可以从现有类创建一个新类。我们利用关键字来做到这一点。在本文中,我们将研究用于相同目的的 3 个关键字并进行比较,即:

  • 延伸
  • 工具

让我们一次一个地研究它们。

延伸

在Dart, extends 关键字通常用于使用继承来改变类的行为。一个类从另一个类派生属性和特征的能力称为继承。它是程序从现有类创建新类的能力。简单来说,我们可以说我们使用 extends 创建子类,使用 super 来引用超类。其属性由子类继承的类称为父类。父类也称为基类超类。从另一个类继承属性的类称为子类。子类也称为派生类、继承类或子类。 extends 关键字是典型的 OOP 类继承。如果 class Second 扩展 class First,则 class First 中实现的所有属性、变量、方法也可在 Second class 中使用。此外,您可以覆盖方法。

如果要创建类的更具体版本,请使用扩展。例如,如果 Apple 继承自 Fruit 类,则意味着 Fruit 类中定义的所有属性、变量和函数都将在 Apple 类中可用。

例子:

下面我们可以看到 extends 关键字的实现示例。我们不需要覆盖继承类的定义,可以使用子类中的现有定义。

Dart
// Class with name First
class First { 
  static int num = 1;
  void firstFunc(){
    print('hello');
  }
}
  
// inherits from First class
class Second extends First{ 
//   No need to override
}
  
void main(){ 
    
  // instance of First Class
  var one = First(); 
    
  // calling firstFunc()
  one.firstFunc(); 
    
  // printing static variable of class
  print(First.num); 
    
  // instance of Second Class
  var second = Second(); 
    
  // calling firstFunc() that 
  // has been inherited
  second.firstFunc(); 
}


Dart
// Class with name First
class First { 
    
  // function to print "hello"
  void firstFunc(){ 
    print('hello');
  }
}
  
// We inherit the propertied
// of implemented class
class Second implements First{
    
  // by overriding the functions
  // in implemented class
  @override
  void firstFunc(){
    print('We had to declare the methods of implemented class');
  }
}
  
void main(){
    
  // instance of First Class
  var one = First(); 
    
  // calling firstFunc()
  one.firstFunc(); 
  // instance of Second Class
  var second = Second();
    
  // calling firstFunc() that
  // has been inherited
  second.firstFunc();  
}


Dart
// mixin with name First
mixin First {
  void firstFunc(){
    print('hello');
  }
}
  
// mixin with name temp
mixin temp { 
  void number(){
    print(10);
  }
}
  
// mixin type used with keyword
class Second with First, temp{ 
  @override
  void firstFunc(){
    print('can override if needed');
  }
}
  
void main(){
  var second = Second();
  second.firstFunc();
  second.number();
}


输出

hello
1
hello

工具

接口定义了一组可用于对象的方法。 Dart没有声明接口的语法。在Dart声明本身就是接口。接口是强制派生类实现一组公共字段和方法列表的东西。

实现关键字用于通过强制重新定义函数来实现接口。

每个类都隐式地定义了一个接口,该接口包含该类的所有实例成员及其实现的任何接口。如果要创建支持类 B 的 API 的类 A 而不继承 B 的实现,则类 A 应实现 B 接口。类通过在implements 子句中声明一个或多个接口,然后提供接口所需的API 来实现它们。

例子:

Dart

// Class with name First
class First { 
    
  // function to print "hello"
  void firstFunc(){ 
    print('hello');
  }
}
  
// We inherit the propertied
// of implemented class
class Second implements First{
    
  // by overriding the functions
  // in implemented class
  @override
  void firstFunc(){
    print('We had to declare the methods of implemented class');
  }
}
  
void main(){
    
  // instance of First Class
  var one = First(); 
    
  // calling firstFunc()
  one.firstFunc(); 
  // instance of Second Class
  var second = Second();
    
  // calling firstFunc() that
  // has been inherited
  second.firstFunc();  
}

输出:

hello
We had to declare the methods of implemented class

Mixin是一种在多个类层次结构中重用类方法的方法。 Mixin 可以理解为抽象类,用于重用具有相似功能/属性的各种类中的方法。 Mixin 是一种抽象和重用一系列操作和状态的方法。它类似于您从扩展类中获得的重用,但不是多重继承。仍然只存在一个超类。

With用于包含Mixinsmixin是一种不同类型的结构,它只能与关键字with 一起使用

Mixin是一种不同类型的结构,它只能与关键字with 一起使用。在Dart,如果类没有构造函数,则该类可以扮演mixin的角色。同样重要的是要注意mixin不会强制类型限制,也不会对类方法施加使用限制。

例子:

Dart

// mixin with name First
mixin First {
  void firstFunc(){
    print('hello');
  }
}
  
// mixin with name temp
mixin temp { 
  void number(){
    print(10);
  }
}
  
// mixin type used with keyword
class Second with First, temp{ 
  @override
  void firstFunc(){
    print('can override if needed');
  }
}
  
void main(){
  var second = Second();
  second.firstFunc();
  second.number();
}

输出:

hello
can override if needed
想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!