{"id":202982,"date":"2025-05-22T12:20:45","date_gmt":"2025-05-22T04:20:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202982\/"},"modified":"2025-05-22T12:20:45","modified_gmt":"2025-05-22T04:20:45","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e5%a3%b0%e6%98%8e%e6%8a%bd%e8%b1%a1%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202982\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u62bd\u8c61\u51fd\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u62bd\u8c61\u51fd\u6570\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u62bd\u8c61\u51fd\u6570\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>Java \u4e2d\u58f0\u660e\u62bd\u8c61\u51fd\u6570\uff1a\u4f7f\u7528 abstract \u5173\u952e\u5b57\u58f0\u660e\u51fd\u6570\u3002\u63d0\u4f9b\u8fd4\u56de\u7c7b\u578b\u3002\u547d\u540d\u51fd\u6570\u3002\u6307\u5b9a\u53c2\u6570\u5217\u8868\u3002\u4f8b\u5982\uff1apublic abstract void calculate(); public abstract int sum(int a, int b);<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730011535671de18fda250.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u62bd\u8c61\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u62bd\u8c61\u51fd\u6570\u662f\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7528\u4e8e\u5b9a\u4e49\u64cd\u4f5c\u7684\u884c\u4e3a\u6216\u5b9a\u4e49\u63a5\u53e3\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u9632\u6b62\u51fa\u73b0\u786c\u7f16\u7801\u7684\u5b9e\u73b0\u3002\u5728 Java \u4e2d\uff0c\u62bd\u8c61\u51fd\u6570\u662f\u4f7f\u7528 <code>abstract<\/code> \u5173\u952e\u5b57\u58f0\u660e\u7684\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>[\u4fee\u9970\u7b26] abstract \u8fd4\u56de\u7c7b\u578b \u51fd\u6570\u540d([[\u53c2\u6570\u5217\u8868]]);<\/pre>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u7528 <code>abstract<\/code> \u5173\u952e\u5b57\u58f0\u660e\u51fd\u6570\u3002<\/li>\n<li>\u63d0\u4f9b\u8fd4\u56de\u7c7b\u578b\u3002<\/li>\n<li>\u547d\u540d\u51fd\u6570\u3002<\/li>\n<li>\u6307\u5b9a\u53c2\u6570\u5217\u8868\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u58f0\u660e\u4e00\u4e2a\u6ca1\u6709\u53c2\u6570\u7684\u62bd\u8c61\u51fd\u6570\uff1a<\/p>\n<pre>public abstract void calculate();<\/pre>\n<p>\u58f0\u660e\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u62bd\u8c61\u51fd\u6570\uff1a<\/p>\n<pre>public abstract int sum(int a, int b);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>Shape<\/code> \u7684\u62bd\u8c61\u7c7b\uff0c\u5b83\u5b9a\u4e49\u4e86 <code>area()<\/code> \u65b9\u6cd5\u7684\u62bd\u8c61\u7b7e\u540d\uff1a<\/p>\n<pre>public abstract class Shape {\n    public abstract double area();\n}<\/pre>\n<p>\u7136\u540e\uff0c\u521b\u5efa <code>Circle<\/code> \u548c <code>Rectangle<\/code> \u7c7b\uff0c\u5b83\u4eec\u6269\u5c55\u4e86 <code>Shape<\/code> \u7c7b\u5e76\u5b9e\u73b0\u4e86 <code>area()<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre>public class Circle extends Shape {\n    private double radius;\n\n    @Override\n    public double area() {\n        return Math.PI * radius * radius;\n    }\n}\n\npublic class Rectangle extends Shape {\n    private double width;\n    private double height;\n\n    @Override\n    public double area() {\n        return width * height;\n    }\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6211\u4eec\u7684\u4e3b\u7c7b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u62bd\u8c61\u51fd\u6570\u6765\u8ba1\u7b97\u4e0d\u540c\u5f62\u72b6\u7684\u9762\u79ef\uff1a<\/p>\n<pre>public class Main {\n    public static void main(String[] args) {\n        Circle circle = new Circle(5);\n        Rectangle rectangle = new Rectangle(3, 4);\n\n        System.out.println(\"\u5706\u5f62\u9762\u79ef\uff1a\" + circle.area());\n        System.out.println(\"\u77e9\u5f62\u9762\u79ef\uff1a\" + rectangle.area());\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u62bd\u8c61\u51fd\u6570&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202982","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202982"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202982\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}