【笔记】Dart 8 Class modifiers

Overview & usage

Class modifiers, besides abstract, require a language version of at least 3.0.

类修饰符中跟Java差距比较大的主要是 abstract, sealed, mixin,其他的都差不多。

abstract, base, final, interface, sealed, mixin;

abstract

既可以extend,又可以implement;

https://stackoverflow.com/questions/70824365/dart-implements-extends-for-abstract-class

// Library a.dart
abstract class Vehicle {
  void moveForward(int meters);
}

// Library b.dart
import 'a.dart';

// Error: Cannot be constructed
Vehicle myVehicle = Vehicle();

// Can be extended
class Car extends Vehicle {
  int passengers = 4;
  // ···
}

// Can be implemented
class MockVehicle implements Vehicle {
  
  void moveForward(int meters) {
    // ...
  }
}

base

只能extend;

You must mark any class which implements or extends a base class as base, final, or sealed. This prevents outside libraries from breaking the base class guarantees.

// Library a.dart
base class Vehicle {
  void moveForward(int meters) {
    // ...
  }
}

// Library b.dart
import 'a.dart';

// Can be constructed
Vehicle myVehicle = Vehicle();

// Can be extended
base class Car extends Vehicle {
  int passengers = 4;
  // ...
}

// ERROR: Cannot be implemented
base class MockVehicle implements Vehicle {
  
  void moveForward() {
    // ...
  }
}

interface

只能implement;

Other libraries can’t override methods that the interface class’s own methods might later call in unexpected ways.

https://en.wikipedia.org/wiki/Fragile_base_class

// Library a.dart
interface class Vehicle {
  void moveForward(int meters) {
    // ...
  }
}

// Library b.dart
import 'a.dart';

// Can be constructed
Vehicle myVehicle = Vehicle();

// ERROR: Cannot be inherited
class Car extends Vehicle {
  int passengers = 4;
  // ...
}

// Can be implemented
class MockVehicle implements Vehicle {
  
  void moveForward(int meters) {
    // ...
  }
}

abstract interface

The most common use for the interface modifier is to define a pure interface.

Like an interface class, other libraries can implement, but cannot inherit, a pure interface. Like an abstract class, a pure interface can have abstract members.

常用来定义比较纯粹的接口,只能实现,不能继承,不能自实现方法;

sealed

用来干嘛呢,定义一种enum模式的继承?

To create a known, enumerable set of subtypes, use the sealed modifier.

The compiler is aware of any possible direct subtypes because they can only exist in the same library. This allows the compiler to alert you when a switch does not exhaustively handle all possible subtypes in its cases:

sealed class Vehicle {}

class Car extends Vehicle {}

class Truck implements Vehicle {}

class Bicycle extends Vehicle {}

// ERROR: Cannot be instantiated
Vehicle myVehicle = Vehicle();

// Subclasses can be instantiated
Vehicle myCar = Car();

String getVehicleSound(Vehicle vehicle) {
  // ERROR: The switch is missing the Bicycle subtype or a default case.
  return switch (vehicle) {
    Car() => 'vroom',
    Truck() => 'VROOOOMM',
  };
}

Combining modifiers

通常来说按照如下分类进行组合,每个组合可选0到1种;

[abstract] + [base, interface, final, sealed] + [mixin] + class;

需要注意以下几种情况不可以混用;

A sealed class is always implicitly abstract.

sealed不需要abstract前缀;

[interface, final, sealed] 不能和 mixin 一起;

Class modifiers for API maintainers

这一节主要讲 dart3.0 的变动对自开发的库造成的影响以及如何升级,随便看看;

mixin

Dart 3.0 no longer allows classes to be used as mixins by default.

sealed

It exists primarily to enable exhaustiveness checking in pattern matching.

这里switch加了case编译不通过;

sealed class Amigo {
}
class Lucky extends Amigo {}
class Dusty extends Amigo {}
class Ned extends Amigo {}

String lastName(Amigo amigo) =>
    switch (amigo) {
       Lucky _ => 'Day',
       Dusty _ => 'Bottoms',
       Ned _   => 'Nederlander',
    };

void main() {
  Amigo l = Lucky();
  print(lastName(l));
}

The sealed class can’t itself be directly constructible.

保证子类可完全穷举;

Every direct subtype of the sealed type must be in the same library where the sealed type is declared.

不然编译器找不到……

Reference

https://dart.dev/language/modifier-reference

特性对比表,感觉不需要看;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值