{"id":263,"date":"2022-06-25T15:26:01","date_gmt":"2022-06-25T07:26:01","guid":{"rendered":"https:\/\/www.fengjijiao.cn\/?p=263"},"modified":"2022-06-29T14:45:03","modified_gmt":"2022-06-29T06:45:03","slug":"csharp%e8%bf%90%e7%ae%97%e7%ac%a6%e9%87%8d%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/www.fengjijiao.cn\/?p=263","title":{"rendered":"C#\u8fd0\u7b97\u7b26\u91cd\u8f7d"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System;\r\n\r\nnamespace ConsoleApp1\r\n{\r\n    class Box\r\n    {\r\n        private double length;\r\n        private double breadth;\r\n        private double height;\r\n        public double getVolume()\r\n        {\r\n            return length * breadth * height;\r\n        }\r\n        public void setLength(double len)\r\n        {\r\n            length = len;\r\n        }\r\n        public void setBreadth(double bre)\r\n        {\r\n            breadth = bre;\r\n        }\r\n        public void setHeight(double hei)\r\n        {\r\n            height = hei;\r\n        }\r\n        public static Box operator+(Box b, Box c)\r\n        {\r\n            Box box = new Box();\r\n            box.length = b.length + c.length;\r\n            box.breadth = b.breadth + c.breadth;\r\n            box.height = b.height + c.height;\r\n            return box;\r\n        }\r\n    }\r\n\r\n    class BoxE\r\n    {\r\n        private double length;\r\n        private double breadth;\r\n        private double height;\r\n        public double getVolume()\r\n        {\r\n            return length * breadth * height;\r\n        }\r\n        public void setLength(double len)\r\n        {\r\n            length = len;\r\n        }\r\n        public void setBreadth(double bre)\r\n        {\r\n            breadth = bre;\r\n        }\r\n        public void setHeight(double hei)\r\n        {\r\n            height = hei;\r\n        }\r\n        public static BoxE operator + (BoxE b, BoxE c)\r\n        {\r\n            BoxE box = new BoxE();\r\n            box.length = b.length + c.length;\r\n            box.breadth = b.breadth + c.breadth;\r\n            box.height = b.height + c.height;\r\n            return box;\r\n        }\r\n        public static bool operator == (BoxE lhs, BoxE rhs)\r\n        {\r\n            bool status = false;\r\n            if(lhs.length == rhs.length &amp;&amp; lhs.height == rhs.height &amp;&amp; lhs.breadth == rhs.breadth)\r\n            {\r\n                status = true;\r\n            }\r\n            return status;\r\n        }\r\n        public static bool operator != (BoxE lhs, BoxE rhs)\r\n        {\r\n            bool status = false;\r\n            if(lhs.length != rhs.length || lhs.height != rhs.height || lhs.breadth != rhs.breadth)\r\n            {\r\n                status = true;\r\n            }\r\n            return status;\r\n        }\r\n        public static bool operator &lt;(BoxE lhs, BoxE rhs)\r\n        {\r\n            bool status = false;\r\n            if(lhs.length &lt; rhs.length &amp;&amp; lhs.height &lt; rhs.height &amp;&amp; lhs.breadth &lt; rhs.breadth)\r\n            {\r\n                status = true;\r\n            }\r\n            return status;\r\n        }\r\n        public static bool operator >(BoxE lhs, BoxE rhs)\r\n        {\r\n            bool status = false;\r\n            if (lhs.length > rhs.length &amp;&amp; lhs.height > rhs.height &amp;&amp; lhs.breadth > rhs.breadth)\r\n            {\r\n                status = true;\r\n            }\r\n            return status;\r\n        }\r\n        public static bool operator &lt;=(BoxE lhs, BoxE rhs)\r\n        {\r\n            bool status = false;\r\n            if (lhs.length &lt;= rhs.length &amp;&amp; lhs.height &lt;= rhs.height &amp;&amp; lhs.breadth &lt;= rhs.breadth)\r\n            {\r\n                status = true;\r\n            }\r\n            return status;\r\n        }\r\n        public static bool operator >=(BoxE lhs, BoxE rhs)\r\n        {\r\n            bool status = false;\r\n            if (lhs.length >= rhs.length &amp;&amp; lhs.height >= rhs.height &amp;&amp; lhs.breadth >= rhs.breadth)\r\n            {\r\n                status = true;\r\n            }\r\n            return status;\r\n        }\r\n        public override string ToString()\r\n        {\r\n            return String.Format(\"({0},{1},{2})\", length, breadth, height);\r\n        }\r\n    }\r\n    class Program\r\n    {\r\n         static void Main(string&#91;] args)\r\n         {\r\n            \/\/\u8fd0\u7b97\u7b26\u91cd\u8f7d\r\n            \/\/\u53ef\u4ee5\u91cd\u5b9a\u4e49\u6216\u91cd\u8f7dC#\u5185\u7f6e\u7684\u8fd0\u7b97\u7b26\u3002\u91cd\u8f7d\u8fd0\u7b97\u7b26\u662f\u5177\u6709\u7279\u6b8a\u540d\u79f0\u7684\u51fd\u6570\uff0c\u662f\u901a\u8fc7\u5173\u952e\u5b57operator\u540e\u8ddf\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\u6765\u5b9a\u4e49\u7684\u3002\u4e0e\u5176\u4ed6\u51fd\u6570\u4e00\u6837\uff0c\u91cd\u8f7d\u8fd0\u7b97\u7b26\u6709\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u5217\u8868\u3002\r\n            \/*\r\n             * public static Box operator+(Box b, Box c)\r\n             * {\r\n             *     Box box = new Box();\r\n             *     box.length = b.length + c.length;\r\n             *     box.breadth = b.breadth + c.breadth;\r\n             *     box.height = b.height + c.height;\r\n             *     return box;\r\n             * }\r\n             * \u4e0a\u9762\u7684\u51fd\u6570\u4e3a\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u7c7bBox\u5b9e\u73b0\u4e86\u52a0\u6cd5\u8fd0\u7b97\u7b26(+)\u3002\u4ed6\u628a\u4e24\u4e2aBox\u5bf9\u8c61\u7684\u5c5e\u6027\u76f8\u52a0\uff0c\u5e76\u8fd4\u56de\u76f8\u52a0\u540e\u7684Box\u5bf9\u8c61\u3002\r\n             *\/\r\n            Box box1 = new Box();\r\n            Box box2 = new Box();\r\n            double volume = 0.0;\r\n            box1.setLength(6.0);\r\n            box1.setBreadth(7.0);\r\n            box1.setHeight(5.0);\r\n            box2.setLength(12.0);\r\n            box2.setBreadth(13.0);\r\n            box2.setHeight(10.0);\r\n            volume = box1.getVolume();\r\n            Console.WriteLine(\"box1\u7684\u4f53\u79ef\uff1a {0}\", volume);\r\n            volume = box2.getVolume();\r\n            Console.WriteLine(\"box2\u7684\u4f53\u79ef\uff1a {0}\", volume);\r\n\r\n            Box box3 = box1 + box2;\r\n\r\n            volume = box3.getVolume();\r\n            Console.WriteLine(\"box3\u7684\u4f53\u79ef\uff1a{0}\", volume);\r\n            \/\/\u53ef\u91cd\u8f7d\u548c\u4e0d\u53ef\u91cd\u8f7d\u8fd0\u7b97\u7b26\r\n            \/*\r\n             * + - ! ~ ++ -- \u8fd9\u4e9b\u4e00\u5143\u8fd0\u7b97\u7b26\u53ea\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\uff0c\u4e14\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\r\n             * + - * \/ % \u8fd9\u4e9b\u4e8c\u5143\u8fd0\u7b97\u7b26\u5e26\u6709\u4e24\u4e2a\u64cd\u4f5c\u6570\uff0c\u4e14\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\r\n             * == != &lt; > &lt;= >= \u8fd9\u4e9b\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u88ab\u91cd\u8f7d\u3002\r\n             * &amp;&amp; || \u8fd9\u4e9b\u6761\u4ef6\u903b\u8f91\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e0d\u80fd\u88ab\u76f4\u63a5\u91cd\u8f7d\u3002\r\n             * += -= *= \/= %= \u8fd9\u4e9b\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e0d\u80fd\u88ab\u76f4\u63a5\u91cd\u8f7d\u3002\r\n             * = . ?: -> new is sizeof typeof \u8fd9\u4e9b\u8fd0\u7b97\u7b26\u4e0d\u80fd\u88ab\u91cd\u8f7d\u3002\r\n             *\/\r\n            BoxE box4 = new BoxE();\r\n            BoxE box5 = new BoxE();\r\n            double volume2 = 0.0;\r\n            box4.setLength(6.0);\r\n            box4.setBreadth(7.0);\r\n            box4.setHeight(5.0);\r\n            box5.setLength(12.0);\r\n            box5.setBreadth(13.0);\r\n            box5.setHeight(10.0);\r\n\r\n            Console.WriteLine(\"box4: {0}\", box4.ToString());\r\n            Console.WriteLine(\"box5: {0}\", box5.ToString());\r\n\r\n            volume2 = box4.getVolume();\r\n            Console.WriteLine(\"box4\u7684\u4f53\u79ef\uff1a{0}\", volume2);\r\n\r\n            volume2 = box5.getVolume();\r\n            Console.WriteLine(\"box5\u7684\u4f53\u79ef\uff1a{0}\", volume2);\r\n\r\n            BoxE box6 = box4 + box5;\r\n            Console.WriteLine(\"box6: {0}\", box6.ToString());\r\n            volume2 = box6.getVolume();\r\n            Console.WriteLine(\"box6\u7684\u4f53\u79ef\uff1a{0}\", volume2);\r\n\r\n            if (box4 > box5)\r\n                Console.WriteLine(\"box4 \u5927\u4e8e box5\");\r\n            else\r\n                Console.WriteLine(\"box4 \u4e0d\u5927\u4e8e box5\");\r\n            if (box4 &lt; box5)\r\n                Console.WriteLine(\"box4 \u5c0f\u4e8e box5\");\r\n            else\r\n                Console.WriteLine(\"box4 \u4e0d\u5c0f\u4e8e box5\");\r\n            if (box4 >= box5)\r\n                Console.WriteLine(\"box4 \u5927\u4e8e\u7b49\u4e8e box5\");\r\n            else\r\n                Console.WriteLine(\"box4 \u4e0d\u5927\u4e8e\u7b49\u4e8e box5\");\r\n            if (box4 &lt;= box5)\r\n                Console.WriteLine(\"box4 \u5c0f\u4e8e\u7b49\u4e8e box5\");\r\n            else\r\n                Console.WriteLine(\"box4 \u4e0d\u5c0f\u4e8e\u7b49\u4e8e box5\");\r\n            if (box4 != box5)\r\n                Console.WriteLine(\"box4 \u4e0d\u7b49\u4e8e box5\");\r\n            else\r\n                Console.WriteLine(\"box4 \u7b49\u4e8e box5\");\r\n            BoxE box7 = box6;\r\n            if (box7 == box6)\r\n                Console.WriteLine(\"box7 \u7b49\u4e8e box6\");\r\n            else\r\n                Console.WriteLine(\"box7 \u4e0d\u7b49\u4e8e box6\");\r\n\r\n\r\n            Console.ReadKey();\r\n         }\r\n    }\r\n    \r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/263"}],"collection":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=263"}],"version-history":[{"count":1,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}