首页 > inheritance > 如何使用相同的类名在dart中扩展类

如何使用相同的类名在dart中扩展类 (How to extend a class in dart using the same class name)

问题

有没有办法在dart中使用与其父类相同的名称扩展类,就像java一样?像这样......:

class Connection extends com.mypackage.Connection{
    ...
}

通过使用包名定义父类,我能够使用相同的名称扩展类,但是这可能在dart中???

解决方法

在Dart中,您可以导入另一个带前缀的库。这允许您限定对名称的引用,因此它不会与当前库中的相同名称冲突:

import "other_connection.dart" as other;
class Connections extends other.Connection { 
  ...
}

这也可用于限定来自两个不同导入的冲突名称。

(Dart库有一个默认的导入范围,其中添加了导入的名称,可以直接引用。as prefix导入上的限定符意味着将导入放入不同的导入范围,您可以引用它prefix。多个库可以被导入到相同的前缀导入范围。)

问题

Is there a way to extend a class in dart using the same name as its parent, like java does? Something like this...:

class Connection extends com.mypackage.Connection{
    ...
}

By defining the parent class with the package name I'm able to extend the class with the same name, but is this possible in dart???

解决方法

In Dart you can import another library with a prefix. This allows you to qualify the reference to the name, so it doesn't conflict with the same name in the current library:

import "other_connection.dart" as other;
class Connections extends other.Connection { 
  ...
}

This can also be used to qualify conflicting names from two different imports.

(A Dart library has a default import scope where imported names are added, and which can be referenced directly. The as prefix qualifyer on an import means that that import is put into a different import scope, which you can reference by prefix. More than one library can be imported into the same prefixed import scope.)

相似信息