【问题标题】:How to define interfaces in Dart?如何在 Dart 中定义接口?
【发布时间】:2014-01-14 11:46:06
【问题描述】:

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

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 是如何实现接口隔离原理的呢?

【问题讨论】:

    标签: oop inheritance dart interface


    【解决方案1】:

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

    每个类都隐式定义了一个接口,该接口包含该类的所有实例成员以及它实现的任何接口。如果你想创建一个类 A 支持 B 类的 API 而不继承 B 的实现,那么 A 类应该实现 B 接口。

    一个类通过在implements 子句中声明它们来实现一个或多个接口,然后提供接口所需的API。

    所以你的例子可以像这样在 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
      }
    }
    

    【讨论】:

    • 是否可以定义抽象的静态方法或抽象的命名构造函数?
    • 我会比这个琐碎的例子更感激
    【解决方案2】:

    这是一个类或接口,视情况而定。

    abstract class A {
        void sayHello() {
           print("Hello");
        }
        void sayBye();
    }
    

    B类实现了A接口,所以它必须实现A的所有方法。

    class B implements A {
        void sayHello() {
           print("B say Hello");
        }
        void sayBye() {
           print("B say Bye");
        }
    }
    

    C 类扩展了 A 类,因此它必须实现 A 的所有抽象方法。(不是全部)。 C 继承了 A 类的 sayHello() 方法。

    class C extends A {
       void sayBye() {
           print("C say Bye");
       }
    }
    

    【讨论】:

      【解决方案3】:
      
      abstract class ORMInterface {
        void fromJson(Map<String, dynamic> _map);
      }
      
      abstract class ORM implements ORMInterface {
      
        String collection = 'default';
      
        first(Map<String, dynamic> _map2) {
          print("Col $collection");
        }
      }
      
      class Person extends ORM {
      
        String collection = 'persons';
      
        fromJson(Map<String, dynamic> _map) {
          print("Here is mandatory");
        }
      }
      
      

      【讨论】:

        【解决方案4】:

        混淆通常是因为不存在像java和其他语言那样的“接口”这个词。类声明本身就是 Dart 中的接口。

        在 Dart 中,每个类都像其他人所说的那样定义了一个隐式接口。那么……关键是:类应该使用 implements 关键字才能使用接口。

        abstract class IsSilly {
          void makePeopleLaugh();
        }
        
        //Abstract class
        class Clown extends IsSilly {   
          void makePeopleLaugh() {
            // Here is where the magic happens
          }
        }
        
        //Interface
        class Comedian implements IsSilly {
          void makePeopleLaugh() {
            // Here is where the magic happens
          }
        }
        

        【讨论】:

          【解决方案5】:

          在 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
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2014-10-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-01-27
            • 1970-01-01
            • 1970-01-01
            • 2020-11-04
            • 1970-01-01
            相关资源
            最近更新 更多