`interface class` 在 Dart 3 中的作用是什么?

huangapple go评论69阅读模式
英文:

What is the point of `interface class` in Dart 3?

问题

我一直在尝试 Dart 3,对新的类修饰符很喜欢。但有一点我似乎无法理解:为什么存在 interface class?我理解 interface mixin,因为你可以声明抽象成员。但在最新的 alpha 版本中,你不能这样做。

具体来说,你可以这样做:

interface mixin A {
  void foo();
}

abstract class B {
  void bar();
}

但你不能这样做:

interface class C {
  void foobar(); // 编译错误:需要具体实现
}

在什么情况下应该使用 interface class 而不是 interface mixin

英文:

I have been playing around with Dart 3 a bit and am a fan of the new class modifiers. But one I can't seem to get my head around: why does interface class exist? I understand interface mixin because you can declare abstract members. But you can't do that with an interface class, at least in the latest alpha.

Specifically, you can do this:

interface mixin A {
  void foo();
}

abstract class B {
  void bar();
}

But you can't do this:

interface class C {
  void foobar(); // compilation error: needs a concrete implementation
}

What is the point of having an interface class where you will always need provide stub implementations and then implement them later on? When should you use an interface class over interface mixin?

答案1

得分: 4

以下是翻译好的部分:

摘要接口
是什么:更像传统接口。只能被实现(而不是被扩展)。但你可以定义没有具体实现的函数。

为什么你应该关注:你可以定义接口的“形状”,而不需要定义具体的功能。父类中没有隐藏的内容。

// -- 文件 a.dart
abstract interface class AbstractInterfaceClass {
  String name = 'Dave'; // 允许
  void body() { print('body'); } // 允许
  
  // 这是更传统的实现方式
  int get myField; // 允许
  void noBody(); // 允许
}

// -- 文件 b.dart
// 不允许
class ExtensionClass extends AbstractInterfaceClass{}
// 允许
class ConcreteClass implements AbstractInterfaceClass {
  // 必须重写所有内容
  @override
  String name = 'ConcreteName';
  @override
  void body() { print('body'); }

  @override
  int get myField => 5;
  @override
  void noBody() => print('concreteBody');
}

vs 接口

// -- 文件 a.dart
interface class InterfaceClass {
  String name = 'Dave'; // 允许
  void body() { print('body'); } // 允许

  int get myField; // 不允许
  void noBody(); // 不允许
}

// -- 文件 b.dart
// 不允许
class ExtensionClass extends InterfaceClass{}
// 允许
class ConcreteClass implements InterfaceClass{
  // 必须重写所有内容
  @override
  String name = 'ConcreteName';
  @override
  void body() { print('body'); }
}
英文:

This article covers a bit more details. And this question answers it more broadly.

But the summary is, I agree with you, and that's why you should use abstract interface instead of just interface. You'd only use interface over abstract interface if you wanted to be able to instantiate it.

abstract interface

What is it: More like a traditional interface. Can only be implemented (not extended). But you can define functions without bodies.

Why you should care: You can define just the “shape” without defining any functionality. There’s nothing hidden in the parent class.

// -- File a.dart
abstract interface class AbstractInterfaceClass {
  String name = 'Dave'; // Allowed
  void body() { print('body'); } // Allowed
  
  // This is a more traditional implementation
  int get myField; // Allowed
  void noBody(); // Allowed
}

// -- File b.dart
// Not allowed
class ExtensionClass extends AbstractInterfaceClass{}
// Allowed
class ConcreteClass implements AbstractInterfaceClass {
  // Have to override everything
  @override
  String name = 'ConcreteName';
  @override
  void body() { print('body'); }

  @override
  int get myField => 5;
  @override
  void noBody() => print('concreteBody');
}

vs interface

// -- File a.dart
interface class InterfaceClass {
  String name = 'Dave'; // Allowed
  void body() { print('body'); } // Allowed

  int get myField; // Not allowed
  void noBody(); // Not allowed
}

// -- File b.dart
// Not allowed
class ExtensionClass extends InterfaceClass{}
// Allowed
class ConcreteClass implements InterfaceClass{
  // Have to override everything
  @override
  String name = 'ConcreteName';
  @override
  void body() { print('body'); }
}

huangapple
  • 本文由 发表于 2023年3月3日 19:27:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定