在Java中,我可能有一个IsSilly接口(interface)和一个或多个实现它的具体类型:

public interface IsSilly {
    public void makePeopleLaugh();
}

public class Clown implements IsSilly {
    @Override
    public void makePeopleLaugh() {
        // Here is where the magic happens
    }
}

public class Comedian implements IsSilly {
    @Override
    public void makePeopleLaugh() {
        // Here is where the magic happens
    }
}

Dart中的这段代码等效于什么?

在类上仔细阅读了官方的docs之后,Dart似乎没有本机的interface类型。那么,普通的Dartisan如何完成接口(interface)隔离原则?

最佳答案

在Dart中,有一个implicit interfaces的概念。

因此,您的示例可以像这样在Dart中翻译:

abstract class IsSilly {
  void makePeopleLaugh();
}

class Clown implements IsSilly {
  void makePeopleLaugh() {
    // Here is where the magic happens
  }
}

class Comedian implements IsSilly {
  void makePeopleLaugh() {
    // Here is where the magic happens
  }
}

08-06 16:10