{"id":906,"date":"2022-10-13T23:04:31","date_gmt":"2022-10-13T15:04:31","guid":{"rendered":"https:\/\/qaqaq.top\/?p=906"},"modified":"2022-11-27T12:39:53","modified_gmt":"2022-11-27T04:39:53","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%8b-%e6%8e%a5%e5%8f%a3%e7%bb%83%e4%b9%a0%ef%bc%9a%e6%af%94%e8%be%83%e5%af%b9%e8%b1%a1%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=906","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e0b)-\u63a5\u53e3\u7ec3\u4e60\uff1a\u6bd4\u8f83\u5bf9\u8c61\u5927\u5c0f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P358;\r\n\r\n\/*\r\n *  \u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u7528\u6765\u5b9e\u73b0\u4e24\u4e2a\u5bf9\u8c61\u7684\u6bd4\u8f83\u3002\r\n *  interface CompareObject{\r\n\tpublic int compareTo(Object o); \r\n\t\/\/\u82e5\u8fd4\u56de\u503c\u662f 0 , \u4ee3\u8868\u76f8\u7b49; \u82e5\u4e3a\u6b63\u6570\uff0c\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u5927\uff1b\u8d1f\u6570\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u5c0f\r\n\t}\r\n * \r\n *\/\r\npublic interface CompareObject {\r\n\r\n\t\/\/ \u82e5\u8fd4\u56de\u503c\u662f 0 , \u4ee3\u8868\u76f8\u7b49; \u82e5\u4e3a\u6b63\u6570\uff0c\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u5927\uff1b\u8d1f\u6570\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u5c0f\r\n\tpublic int compareTo(Object o);\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P358;\r\n\r\n\/*\r\n * \u5b9a\u4e49\u4e00\u4e2aCircle\u7c7b\uff0c\u58f0\u660eredius\u5c5e\u6027\uff0c\u63d0\u4f9bgetter\u548csetter\u65b9\u6cd5\r\n * \r\n *\/\r\npublic class Circle {\r\n\r\n\tprivate Double redius;\r\n\r\n\tpublic Circle() {\r\n\t\tsuper();\r\n\t}\r\n\r\n\tpublic Circle(Double redius) {\r\n\t\tsuper();\r\n\t\tthis.redius = redius;\r\n\t}\r\n\r\n\tpublic Double getRedius() {\r\n\t\treturn redius;\r\n\t}\r\n\r\n\tpublic void setRedius(Double redius) {\r\n\t\tthis.redius = redius;\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P358;\r\n\r\n\/*\r\n * \u5b9a\u4e49\u4e00\u4e2aComparableCircle\u7c7b\uff0c\u7ee7\u627fCircle\u7c7b\u5e76\u4e14\u5b9e\u73b0CompareObject\u63a5\u53e3\u3002\r\n * \u5728ComparableCircle\u7c7b\u4e2d\u7ed9\u51fa\u63a5\u53e3\u4e2d\u65b9\u6cd5compareTo\u7684\u5b9e\u73b0\u4f53\uff0c\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u5706\u7684\u534a\u5f84\u5927\u5c0f\u3002\r\n * \r\n * \r\n * \r\n *\/\r\npublic class ComparableCircle extends Circle implements CompareObject {\r\n\r\n\tpublic ComparableCircle(double radius) {\r\n\t\tsuper(radius);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic int compareTo(Object o) {\r\n\r\n\t\tif (this == o) {\r\n\t\t\treturn 0;\r\n\t\t}\r\n\r\n\t\tif (o instanceof ComparableCircle) {\r\n\t\t\tComparableCircle c = (ComparableCircle) o;\r\n\t\t\t\/\/ \u9519\u8bef\u7684\uff1a\r\n\/\/\t\t\treturn (int) (this.getRedius() - c.getRedius());\r\n\t\t\t\/\/ \u6b63\u786e\u7684\u65b9\u5f0f\u4e00\uff1a\r\n\/\/\t\t\tif (this.getRedius() > c.getRedius()) {\r\n\/\/\t\t\t\treturn 1;\r\n\/\/\t\t\t} else if (this.getRedius() &lt; c.getRedius()) {\r\n\/\/\t\t\t\treturn -1;\r\n\/\/\t\t\t} else {\r\n\/\/\t\t\t\treturn 0;\r\n\/\/\t\t\t}\r\n\t\t\t\/\/\u5f53\u5c5e\u6027radius\u58f0\u660e\u4e3aDouble\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u8c03\u7528\u5305\u88c5\u7c7b\u7684\u65b9\u6cd5\r\n\t\t\t\/\/\u6b63\u786e\u7684\u65b9\u5f0f\u4e8c\uff1a\r\n\t\t\treturn this.getRedius().compareTo(c.getRedius());\r\n\t\t} else {\r\n\/\/\t\t\treturn 0;\r\n\t\t\tthrow new RuntimeException(\"\u4f20\u5165\u7684\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\");\r\n\t\t}\r\n\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P358;\r\n\r\npublic class ComparableCircleTest {\r\n\t\r\n\tpublic static void main(String&#91;] args) {\r\n\t\t\r\n\t\tComparableCircle c1 = new ComparableCircle(3.4);\r\n\t\tComparableCircle c2 = new ComparableCircle(3.6);\r\n\t\t\r\n\t\tint compareValue = c1.compareTo(c2);\r\n\t\tif(compareValue > 0) {\r\n\t\t\tSystem.out.println(\"c1\u5bf9\u8c61\u5927\");\r\n\t\t}else if(compareValue &lt; 0) {\r\n\t\t\tSystem.out.println(\"c2\u5bf9\u8c61\u5927\");\r\n\t\t}else {\r\n\t\t\tSystem.out.println(\"c1\u4e0ec2\u4e00\u6837\u5927\");\r\n\t\t}\r\n\t\t\r\n\t\tint comperValue1 = c1.compareTo(new String(\"AA\"));\r\n\t\tSystem.out.println(comperValue1);\r\n\t\t\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-906","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/906"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=906"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/906\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/906\/revisions\/907"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}