{"id":884,"date":"2017-02-04T16:07:27","date_gmt":"2017-02-04T07:07:27","guid":{"rendered":"http:\/\/okamurax.com\/?p=884"},"modified":"2020-05-05T01:16:28","modified_gmt":"2020-05-04T16:16:28","slug":"c-iterator","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=884","title":{"rendered":"C# Iterator"},"content":{"rendered":"<pre class=\"lang:c# decode:true  \">using System;\r\n\r\nnamespace ConsoleApplication1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            ConcreateAggregate book = new ConcreateAggregate(10);\r\n            book.add(\"\u30a2\u30eb\u30b8\u30e3\u30fc\u30ce\u30f3\u306b\u82b1\u675f\u3092\");\r\n            book.add(\"\u30e9\u30a4\u9ea6\u7551\u3067\u3064\u304b\u307e\u3048\u3066\");\r\n        }\r\n    }\r\n\r\n    \/* Iterator\r\n    \u5b9f\u969b\u306e\u96c6\u5408\u4f53\u304c\u81ea\u8eab\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3057\u305f\u3001\u5177\u4f53\u7684\u306a\u53cd\u5fa9\u5b50\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3059\u308b\u3002\r\n\r\n        *\/\r\n\r\n    interface Aggregate \/\/Iterator\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\r\n    {\r\n        Iterator iterator();\r\n    }\r\n    interface Iterator \/\/\u8d70\u67fb\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3002\r\n    {\r\n        object Next();\r\n        \/\/HasNext\u306a\u3069\u5fc5\u8981\u306a\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\u3002\r\n    }\r\n\r\n    class ConcreateAggregate : Aggregate \/\/\u5b9f\u969b\u306e\u96c6\u5408\u4f53\u3002\r\n    {\r\n        \/\/\u96c6\u5408\u4f53\u3092\u4fdd\u6301\u3059\u308b\u3002\r\n        private string[] items;\r\n        int last = 0;\r\n\r\n        public ConcreateAggregate(int max)\r\n        {\r\n            this.items = new string[max];\r\n        }\r\n\r\n        public void add(string item)\r\n        {\r\n            items[last] = item;\r\n            last++;\r\n        }\r\n\r\n        public Iterator iterator()\r\n        {\r\n            \/\/\u5b9f\u969b\u306e\u96c6\u5408\u4f53\u304c\u8fd4\u3059\u306e\u306f\u5177\u4f53\u7684\u306a\u53cd\u5fa9\u5b50\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\r\n            return new ConcreateIterator(this);\r\n        }\r\n\r\n        public string GetItemAt(int index)\r\n        {\r\n            return items[index];\r\n        }\r\n\r\n    }\r\n\r\n    class ConcreateIterator: Iterator \/\/\u5177\u4f53\u7684\u306a\u53cd\u5fa9\u5b50\r\n    {\r\n        \/\/\u30ab\u30ec\u30f3\u30c8\u8981\u7d20\u306e\u8a18\u61b6\u3002\r\n        int index;\r\n\r\n        public ConcreateIterator()\r\n        {\r\n            index = 0;\r\n        }\r\n\r\n        \/\/\u5b9f\u969b\u306e\u96c6\u5408\u4f53\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4fdd\u6301\u3002\r\n        private ConcreateAggregate ConcreateAggregate;\r\n   \r\n        public ConcreateIterator(ConcreateAggregate c)\r\n        {\r\n            this.ConcreateAggregate = c;\r\n        }\r\n\r\n        public object Next()\r\n        {\r\n            return ConcreateAggregate.GetItemAt(index++);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ConcreateAggre &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=884\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Iterator&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9,144],"class_list":["post-884","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net","tag-144"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=884"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":885,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/884\/revisions\/885"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}