{"id":1137,"date":"2023-03-16T21:56:21","date_gmt":"2023-03-16T13:56:21","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1137"},"modified":"2023-04-29T10:00:30","modified_gmt":"2023-04-29T02:00:30","slug":"implementation-of-dart-interface","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/16\/implementation-of-dart-interface\/","title":{"rendered":"Dart\u63a5\u53e3\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>Dart \u8bed\u8a00\u5e76\u6ca1\u6709\u63d0\u4f9b interface \u5173\u952e\u5b57\uff0c\u4f46\u662f\u6bcf\u4e00\u4e2a\u7c7b\u90fd\u9690\u5f0f\u5730\u5b9a\u4e49\u4e86\u4e00\u4e2a\u63a5\u53e3\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-dart\">import &#039;dart:math&#039;;\n\nabstract class Shape {\n  factory Shape(String type) {\n    if (type == &#039;circle&#039;) return Circle(2);\n    if (type == &#039;square&#039;) return Square(2);\n    throw &#039;Can\\&#039;t create $type.&#039;;\n  }\n  num get area;\n}\n\nclass Circle implements Shape {\n  final num radius;\n  Circle(this.radius);\n  num get area =&gt; pi * pow(radius, 2);\n}\n\nclass Square implements Shape {\n  final num side;\n  Square(this.side);\n  num get area =&gt; pow(side, 2);\n}\n\nclass CircleMock implements Circle {\n  num area;\n  num radius;\n}\n\nmain() {\n  final circle = Shape(&#039;circle&#039;);\n  final square = Shape(&#039;square&#039;);\n  print(circle.area);\n  print(square.area);\n}<\/code><\/pre>\n<p>\u6269\u5c55 Circle \u7c7b\uff0c\u589e\u52a0\u4e00\u4e2a CircleMock:<\/p>\n<pre><code class=\"language-dart\">class CircleMock implements Circle {}<\/code><\/pre>\n<p>\u5c06\u4f1a\u770b\u5230\u4e00\u4e2a &quot;Missing concrete implementations&quot; \u7684\u9519\u8bef\uff0c\u6dfb\u52a0\u4e24\u4e2a\u5b9e\u4f8b\u53d8\u91cf area \u548c radius \u5373\u53ef\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898<\/p>\n<pre><code class=\"language-dart\">class CircleMock implements Circle {\n  num area;\n  num radius;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dart \u8bed\u8a00\u5e76\u6ca1\u6709\u63d0\u4f9b interface \u5173\u952e\u5b57\uff0c\u4f46\u662f\u6bcf\u4e00\u4e2a\u7c7b\u90fd\u9690\u5f0f\u5730\u5b9a\u4e49\u4e86\u4e00\u4e2a\u63a5\u53e3\u3002 import &#038;# [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[304],"class_list":["post-1137","post","type-post","status-publish","format-standard","hentry","category-flutter","tag-dart"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1137"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1137\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}