{"id":716,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/716.html","title":{"rendered":"Java \u65b9\u6cd5"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java \u65b9\u6cd5<\/h1>\n<\/p><\/div>\n
\n \u65b9\u6cd5<\/strong>\u662f\u4ee3\u7801\u5757\u6216\u8bed\u53e5\u96c6\u5408\u6216\u4e00\u7ec4\u4ee3\u7801\u7ec4\u5408\u5728\u4e00\u8d77\u4ee5\u6267\u884c\u67d0\u4e9b\u4efb\u52a1\u6216\u64cd\u4f5c\u3002\u5b83\u7528\u4e8e\u5b9e\u73b0\u4ee3\u7801\u7684
\n \u53ef\u91cd\u7528\u6027<\/strong>\u3002\u6211\u4eec\u7f16\u5199\u4e00\u6b21\u65b9\u6cd5\uff0c\u5e76\u591a\u6b21\u4f7f\u7528\u3002\u6211\u4eec\u4e0d\u9700\u8981\u4e00\u6b21\u53c8\u4e00\u6b21\u5730\u7f16\u5199\u4ee3\u7801\u3002\u53ea\u9700\u6dfb\u52a0\u6216\u5220\u9664\u4e00\u90e8\u5206\u4ee3\u7801\uff0c\u5b83\u8fd8\u63d0\u4f9b\u4e86
\n \u8f7b\u677e\u4fee\u6539<\/strong>\u548c
\n \u53ef\u8bfb\u6027<\/strong>\u7684\u4ee3\u7801\u3002\u8be5\u65b9\u6cd5\u4ec5\u5728\u6211\u4eec\u8c03\u7528\u6216\u8c03\u7528\u65f6\u6267\u884c\u3002\n <\/div>\n

\u65b9\u6cd5\u58f0\u660e<\/h3>\n
\n \u65b9\u6cd5\u58f0\u660e\u63d0\u4f9b\u6709\u5173\u65b9\u6cd5\u5c5e\u6027\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u53ef\u89c1\u6027\uff0c\u8fd4\u56de\u7c7b\u578b\uff0c\u540d\u79f0\u548c\u53c2\u6570\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5b83\u5177\u6709\u516d\u4e2a\u79f0\u4e3a
\n method header <\/strong>\u7684\u7ec4\u4ef6\u3002\n <\/div>\n

Java \u65b9\u6cd5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u65b9\u6cd5\u7b7e\u540d: <\/strong>\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u6709\u4e00\u4e2a\u65b9\u6cd5\u7b7e\u540d\u3002\u5b83\u662f\u65b9\u6cd5\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002\u5b83\u5305\u62ec
\n \u65b9\u6cd5\u540d\u79f0<\/strong>\u548c
\n \u53c2\u6570\u5217\u8868<\/strong>\u3002\n <\/div>\n
\n \u8bbf\u95ee\u8bf4\u660e\u7b26: <\/strong>\u8bbf\u95ee\u8bf4\u660e\u7b26\u6216\u4fee\u9970\u7b26\u662f\u65b9\u6cd5\u3002\u5b83\u6307\u5b9a\u65b9\u6cd5\u7684\u53ef\u89c1\u6027\u3002 Java\u63d0\u4f9b\u4e86
\n \u56db\u79cd<\/strong>\u7c7b\u578b\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26:\n <\/div>\n

public: <\/strong>\u5f53\u6211\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u516c\u5171\u8bf4\u660e\u7b26\u65f6\uff0c\u6240\u6709\u7c7b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8be5\u65b9\u6cd5\u3002<\/span>
\n private: <\/strong>\u5f53\u6211\u4eec\u4f7f\u7528\u79c1\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26\u65f6\uff0c\u4ec5\u5728\u5b9a\u4e49\u8be5\u65b9\u6cd5\u7684\u7c7b\u4e2d\u53ef\u4ee5\u8bbf\u95ee\u8be5\u65b9\u6cd5\u3002<\/span>
\n protect: <\/strong>\u5f53\u6211\u4eec\u4f7f\u7528\u53d7\u4fdd\u62a4\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u65f6\uff0c\u8be5\u65b9\u6cd5\u53ef\u5728\u76f8\u540c\u5305\u4e2d\u6216\u5728\u4e0d\u540c\u5305\u4e2d\u7684\u5b50\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/span>
\n default: <\/strong>\u5f53\u6211\u4eec\u5728\u65b9\u6cd5\u58f0\u660e\u4e2d\u4e0d\u4f7f\u7528\u4efb\u4f55\u8bbf\u95ee\u8bf4\u660e\u7b26\u65f6\uff0cJava\u9ed8\u8ba4\u4f7f\u7528\u9ed8\u8ba4\u7684\u8bbf\u95ee\u8bf4\u660e\u7b26\u3002\u4ec5\u4ece\u540c\u4e00\u7a0b\u5e8f\u5305\u4e2d\u53ef\u89c1\u3002<\/span> <\/p>\n

\n \u8fd4\u56de\u7c7b\u578b: <\/strong>\u8fd4\u56de\u7c7b\u578b\u662f\u65b9\u6cd5\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u3002\u5b83\u53ef\u80fd\u5177\u6709\u539f\u59cb\u6570\u636e\u7c7b\u578b\uff0c\u5bf9\u8c61\uff0c\u96c6\u5408\uff0cvoid\u7b49\u3002\u5982\u679c\u8be5\u65b9\u6cd5\u672a\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\uff0c\u5219\u4f7f\u7528void\u5173\u952e\u5b57\u3002\n <\/div>\n
\n \u65b9\u6cd5\u540d\u79f0: <\/strong>\u7528\u4e8e\u5b9a\u4e49\u65b9\u6cd5\u540d\u79f0\u7684\u540d\u79f0\u3002\u5b83\u5fc5\u987b\u4e0e\u65b9\u6cd5\u7684\u529f\u80fd\u76f8\u5bf9\u5e94\u3002\u5047\u8bbe\uff0c\u5982\u679c\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2a\u5c06\u4e24\u4e2a\u6570\u5b57\u76f8\u51cf\u7684\u65b9\u6cd5\uff0c\u5219\u8be5\u65b9\u6cd5\u7684\u540d\u79f0\u5fc5\u987b\u4e3a
\n subtraction()\u3002<\/strong>\u8be5\u65b9\u6cd5\u7684\u540d\u79f0\u4f1a\u88ab\u8c03\u7528\u3002\n <\/div>\n
\n \u53c2\u6570\u5217\u8868: <\/strong>\u662f\u7528\u9017\u53f7\u5206\u9694\u5e76\u62ec\u5728\u5706\u62ec\u53f7\u4e2d\u7684\u53c2\u6570\u5217\u8868\u3002\u5b83\u5305\u542b\u6570\u636e\u7c7b\u578b\u548c\u53d8\u91cf\u540d\u79f0\u3002\u5982\u679c\u8be5\u65b9\u6cd5\u6ca1\u6709\u53c2\u6570\uff0c\u5219\u5c06\u62ec\u53f7\u7559\u7a7a\u3002\n <\/div>\n
\n \u65b9\u6cd5\u4e3b\u4f53: <\/strong>\u5b83\u662f\u65b9\u6cd5\u58f0\u660e\u7684\u4e00\u90e8\u5206\u3002\u5b83\u5305\u542b\u6240\u6709\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002\u5b83\u7528\u4e00\u5bf9\u5927\u62ec\u53f7\u62ec\u8d77\u6765\u3002\n <\/div>\n

\u547d\u540d\u65b9\u6cd5<\/h2>\n
\n \u5728\u5b9a\u4e49\u65b9\u6cd5\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u65b9\u6cd5\u540d\u79f0\u5fc5\u987b\u4e3a
\n \u52a8\u8bcd<\/strong>\uff0c\u5e76\u4ee5
\n \u5c0f\u5199<\/strong>\u5b57\u6bcd\u5f00\u5934\u3002\u5982\u679c\u65b9\u6cd5\u540d\u79f0\u5305\u542b\u4e24\u4e2a\u4ee5\u4e0a\u7684\u5355\u8bcd\uff0c\u5219\u540d\u5b57\u5fc5\u987b\u662f\u52a8\u8bcd\uff0c\u540e\u8ddf\u5f62\u5bb9\u8bcd\u6216\u540d\u8bcd\u3002\u5728\u591a\u5355\u8bcd\u65b9\u6cd5\u540d\u79f0\u4e2d\uff0c\u6bcf\u4e2a\u5355\u8bcd\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5fc5\u987b\u4e3a
\n \u5927\u5199<\/strong>\uff0c\u4f46\u7b2c\u4e00\u4e2a\u5355\u8bcd\u9664\u5916\u3002\u4f8b\u5982:\n <\/div>\n
\n \u5355\u5b57\u65b9\u6cd5\u540d\u79f0: <\/strong> sum()\uff0carea()\n <\/div>\n
\n \u591a\u5b57\u65b9\u6cd5\u540d\u79f0: <\/strong> AreaOfCircle()\uff0cstringComparision()\n <\/div>\n
\n \u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4e00\u4e2a\u65b9\u6cd5\u4e5f\u53ef\u80fd\u5177\u6709\u4e0e\u53e6\u4e00\u4e2a\u65b9\u6cd5\u540d\u79f0\u76f8\u540c\u7684\u540d\u79f0\uff0c\u8fd9\u79f0\u4e3a
\n \u65b9\u6cd5\u91cd\u8f7d<\/strong>\u3002\n <\/div>\n

\u65b9\u6cd5\u7c7b\u578b<\/h2>\n
\n Java\u4e2d\u6709\u4e24\u79cd\u65b9\u6cd5:\n <\/div>\n

\u9884\u5b9a\u4e49\u65b9\u6cd5<\/span>
\n \u7528\u6237\u5b9a\u4e49\u7684\u65b9\u6cd5<\/span> <\/p>\n

\u9884\u5b9a\u4e49\u65b9\u6cd5<\/h3>\n
\n \u5728Java\u4e2d\uff0c\u9884\u5b9a\u4e49\u65b9\u6cd5\u662fJava\u7c7b\u5e93\u4e2d\u5df2\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u79f0\u4e3a\u9884\u5b9a\u4e49\u65b9\u6cd5\u3002\u4e5f\u79f0\u4e3a
\n \u6807\u51c6\u5e93\u65b9\u6cd5<\/strong>\u6216
\n \u5185\u7f6e\u65b9\u6cd5<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u968f\u65f6\u901a\u8fc7\u5728\u7a0b\u5e8f\u4e2d\u8c03\u7528\u5b83\u4eec\u6765\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u65b9\u6cd5\u662f
\n length()\uff0cequals()\uff0ccompareTo()\uff0csqrt()<\/strong>\u7b49\u3002\u5f53\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u8c03\u7528\u4efb\u4f55\u9884\u5b9a\u4e49\u7684\u65b9\u6cd5\u65f6\uff0c\u4e0e\u4ee3\u7801\u76f8\u5173\u7684\u4e00\u7cfb\u5217\u4ee3\u7801\u76f8\u5e94\u7684\u65b9\u6cd5\u5728\u5df2\u5b58\u50a8\u5728\u5e93\u4e2d\u7684\u540e\u53f0\u8fd0\u884c\u3002\n <\/div>\n
\n \u6bcf\u4e2a\u9884\u5b9a\u4e49\u65b9\u6cd5\u90fd\u5728\u7c7b\u4e2d\u5b9a\u4e49\u3002\u5728
\n java.io.PrintStream <\/strong>\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u8bf8\u5982
\n print()<\/strong>\u65b9\u6cd5\u3002\u5b83\u6253\u5370\u6211\u4eec\u5728\u65b9\u6cd5\u5185\u90e8\u7f16\u5199\u7684\u8bed\u53e5\u3002\u4f8b\u5982\uff0c
\n print(\"Java\")<\/strong>\uff0c\u5b83\u5c06\u5728\u63a7\u5236\u53f0\u4e0a\u6253\u5370Java\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u9884\u5b9a\u4e49\u65b9\u6cd5\u7684\u793a\u4f8b\u3002\n <\/div>\n
\n Demo.java <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>Demo 
{
    public <\/span>static void <\/span>main(String[] args)
{
        \/\/ using the max() method of Math class
System.out.print<\/span>(\"The maximum number is: \"<\/span> + Math.max<\/span>(9,7));
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n
\n
 The maximum number is: 9
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e09\u79cd\u9884\u5b9a\u4e49\u7684\u65b9\u6cd5
\n main()\uff0cprint()\uff0c<\/strong>\u548c
\n max()<\/strong>\u3002\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u800c\u65e0\u9700\u58f0\u660e\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u9884\u5b9a\u4e49\u7684\u3002 print()\u65b9\u6cd5\u662f
\n PrintStream <\/strong>\u7c7b\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u53ef\u5728\u63a7\u5236\u53f0\u4e0a\u6253\u5370\u7ed3\u679c\u3002 max()\u65b9\u6cd5\u662f
\n Math <\/strong>\u7c7b\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u4e2d\u7684\u8f83\u5927\u8005\u3002\n <\/div>\n
\n \u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u94fe\u63a5
\n https:\/\/docs.oracle.com\/\u3002\u5f53\u6211\u4eec\u6d4f\u89c8\u94fe\u63a5\u5e76\u770b\u5230max()\u65b9\u6cd5\u7b7e\u540d\u65f6\uff0c\u4f1a\u53d1\u73b0\u4ee5\u4e0b\u5185\u5bb9:\n <\/div>\n

Java \u65b9\u6cd5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u5728\u4e0a\u8ff0\u65b9\u6cd5\u7b7e\u540d\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u8be5\u65b9\u6cd5\u7b7e\u540d\u5177\u6709\u8bbf\u95ee\u8bf4\u660e\u7b26
\n public <\/strong>\uff0c\u975e\u8bbf\u95ee\u4fee\u9970\u7b26
\n static <\/strong>\uff0c\u8fd4\u56de\u7c7b\u578b
\n int <\/strong>\uff0c\u65b9\u6cd5\u540d\u79f0
\n max()\uff0c<\/strong>\u53c2\u6570\u5217\u8868
\n (int a\uff0cint b)\u3002<\/strong>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4ec5\u5b9a\u4e49\u4e86\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5b9a\u4e49\u65b9\u6cd5\u3002\u8fd9\u662f\u9884\u5b9a\u4e49\u65b9\u6cd5\u7684\u4f18\u70b9\u3002\n <\/div>\n
\n \u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u770b\u5230print()\u65b9\u6cd5\u7684\u65b9\u6cd5\u7b7e\u540d\u3002\n <\/div>\n

\u7528\u6237\u5b9a\u4e49\u7684\u65b9\u6cd5<\/h3>\n
\n \u7531\u7528\u6237\u6216\u7a0b\u5e8f\u5458\u7f16\u5199\u7684\u65b9\u6cd5\u79f0\u4e3a
\n \u7528\u6237\u5b9a\u4e49\u7684<\/strong>\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u3002\n <\/div>\n

\u5982\u4f55\u521b\u5efa\u7528\u6237\u5b9a\u4e49\u7684\u65b9\u6cd5<\/h3>\n
\n \u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u6570\u5b57\u662f\u5076\u6570\u8fd8\u662f\u5947\u6570\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u5b9a\u4e49\u65b9\u6cd5\u3002\n <\/div>\n
\n
 \/\/user defined method
public <\/span>static void <\/span>findEvenOdd(int <\/span>num)
{
    \/\/method body
if(num%2==0)
System.out.println<\/span>(num+\" is even\"<\/span>);
    else <\/span>
System.out.println<\/span>(num+\" is odd\"<\/span>);
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u4e86\u4e0a\u8ff0\u540d\u4e3afindevenodd()\u7684\u65b9\u6cd5\u3002\u5b83\u5177\u6709\u4e00\u4e2a\u7c7b\u578b\u4e3aint\u7684\u53c2\u6570
\n num <\/strong>\u3002\u8be5\u65b9\u6cd5\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u4f7f\u7528void\u7684\u539f\u56e0\u3002\u65b9\u6cd5\u4e3b\u4f53\u5305\u542b\u68c0\u67e5\u6570\u5b57\u4e3a\u5076\u6570\u6216\u5947\u6570\u7684\u6b65\u9aa4\u3002\u5982\u679c\u6570\u5b57\u662f\u5076\u6570\uff0c\u5219\u6253\u5370\u6570\u5b57
\n \u662f\u5076\u6570<\/strong>\uff0c\u5426\u5219\u6253\u5370\u6570\u5b57
\n \u662f\u5947\u6570<\/strong>\u3002\n <\/div>\n

\u5982\u4f55\u547c\u53eb\u6216\u8c03\u7528\u7528\u6237\u5b9a\u4e49\u7684\u65b9\u6cd5<\/h3>\n
\n \u4e00\u65e6\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5c31\u5e94\u8be5\u8c03\u7528\u5b83\u3002\u7a0b\u5e8f\u4e2d\u65b9\u6cd5\u7684\u8c03\u7528\u5f88\u7b80\u5355\u3002\u5f53\u6211\u4eec\u8c03\u7528\u6216\u8c03\u7528\u7528\u6237\u5b9a\u4e49\u7684\u65b9\u6cd5\u65f6\uff0c\u7a0b\u5e8f\u63a7\u5236\u6743\u5c06\u8f6c\u79fb\u5230\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u3002\n <\/div>\n
\n
 import java.util.Scanner;
public <\/span>class <\/span>EvenOdd
{
    public <\/span>static void <\/span>main (String args[])
{
        \/\/creating Scanner class <\/span>object
Scanner scan=new Scanner(System.in);
        System.out.print<\/span>(\"Enter the number: \"<\/span>);
        \/\/reading value from the user
int <\/span>num=scan.nextInt<\/span>();
        \/\/method calling
findEvenOdd(num);
    }
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u7f16\u8bd1\u5668\u5728\u5230\u8fbe
\n findEvenOdd(num)\u884c<\/strong>\u65f6\uff0c\u63a7\u4ef6\u7acb\u5373\u8f6c\u79fb\u5230\u8be5\u65b9\u6cd5\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u8f93\u51fa\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u5c06\u4e24\u4e2a\u4ee3\u7801\u6bb5\u5408\u5e76\u5230\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u5e76\u6267\u884c\u5b83\u3002\n <\/div>\n
\n EvenOdd.java <\/strong>\n <\/div>\n
\n
 import java.util.Scanner;
public <\/span>class <\/span>EvenOdd
{
    public <\/span>static void <\/span>main (String args[])
{
        \/\/creating Scanner class <\/span>object
Scanner scan=new Scanner(System.in);
        System.out.print<\/span>(\"Enter the number: \"<\/span>);
        \/\/reading value from user
int <\/span>num=scan.nextInt<\/span>();
        \/\/method calling
findEvenOdd(num);
    }
    \/\/user defined method
public <\/span>static void <\/span>findEvenOdd(int <\/span>num)
{
        \/\/method body
if(num%2==0)
System.out.println<\/span>(num+\" is even\"<\/span>);
        else <\/span>
System.out.println<\/span>(num+\" is odd\"<\/span>);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa1: <\/strong>\n <\/div>\n
\n
 Enter the number: 12
12 is even
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa2: <\/strong>\n <\/div>\n
\n
 Enter the number: 99
99 is odd
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u770b\u53e6\u4e00\u4e2a\u5c06\u503c\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\u6cd5\u7684\u7a0b\u5e8f\u3002\n <\/div>\n
\n \u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a
\n add()<\/strong>\u7684\u65b9\u6cd5\uff0c\u5c06\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u76f8\u52a0\u6570\u5b57\u3002\u5b83\u5177\u6709\u4e24\u4e2a\u6574\u6570\u7c7b\u578b\u7684\u53c2\u6570n1\u548cn2\u3002 n1\u548cn2\u7684\u503c\u5206\u522b\u5bf9\u5e94\u4e8ea\u548cb\u7684\u503c\u3002\u56e0\u6b64\uff0c\u8be5\u65b9\u6cd5\u5c06a\u548cb\u7684\u503c\u76f8\u52a0\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cfs\u4e2d\u5e76\u8fd4\u56de\u603b\u548c\u3002\n <\/div>\n
\n Addition.java <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>Addition 
{
    public <\/span>static void <\/span>main(String[] args)
{
        int <\/span>a = 19;
        int <\/span>b = 5;
        \/\/method calling
int <\/span>c = add(a, b);
        \/\/a and b are actual parameters
System.out.println<\/span>(\"The sum of a and b is= \"<\/span> + c);
    }
    \/\/user defined method
public <\/span>static <\/span>int <\/span>add(int <\/span>n1, int <\/span>n2)
    \/\/n1 and n2 are formal parameters
{
        int <\/span>s;
        s=n1+n2;
        return <\/span>s;
    \/\/returning the sum
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n
\n
 The sum of a and b is= 24
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u9759\u6001\u65b9\u6cd5<\/h3>\n
\n \u5177\u6709\u9759\u6001\u5173\u952e\u5b57\u7684\u65b9\u6cd5\u79f0\u4e3a\u9759\u6001\u65b9\u6cd5\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c5e\u4e8e\u7c7b\u800c\u4e0d\u662f\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\u79f0\u4e3a\u9759\u6001\u65b9\u6cd5\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u65b9\u6cd5\u540d\u79f0\u4e4b\u524d\u4f7f\u7528\u5173\u952e\u5b57
\n static <\/strong>\u6765\u521b\u5efa\u9759\u6001\u65b9\u6cd5\u3002\n <\/div>\n
\n \u9759\u6001\u65b9\u6cd5\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u65e0\u9700\u521b\u5efa\u5bf9\u8c61\u5373\u53ef\u8c03\u7528\u5b83\u3002\u5b83\u53ef\u4ee5\u8bbf\u95ee\u9759\u6001\u6570\u636e\u6210\u5458\uff0c\u4e5f\u53ef\u4ee5\u66f4\u6539\u5176\u503c\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u5b9e\u4f8b\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528\u7c7b\u540d\u6765\u8c03\u7528\u5b83\u3002\u9759\u6001\u65b9\u6cd5\u7684\u6700\u4f73\u793a\u4f8b\u662f
\n main()<\/strong>\u65b9\u6cd5\u3002\n <\/div>\n

\u9759\u6001\u65b9\u6cd5\u7684\u793a\u4f8b<\/h3>\n
\n Display.java <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>Display
{
    public <\/span>static void <\/span>main(String[] args)
{
        show();
    }
    static void <\/span>show()
{
        System.out.println<\/span>(\"It is an example of static <\/span>method.\"<\/span>);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n
\n
 It is an example of a static <\/span>method.
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5b9e\u4f8b\u65b9\u6cd5<\/h3>\n
\n \u8be5\u7c7b\u7684\u65b9\u6cd5\u79f0\u4e3a
\n \u5b9e\u4f8b\u65b9\u6cd5<\/strong>\u3002\u8fd9\u662f\u7c7b\u4e2d\u5b9a\u4e49\u7684
\n \u975e\u9759\u6001<\/strong>\u65b9\u6cd5\u3002\u5728\u8c03\u7528\u6216\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u4e4b\u524d\uff0c\u5fc5\u987b\u521b\u5efa\u5176\u7c7b\u7684\u5bf9\u8c61\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5\u7684\u793a\u4f8b\u3002\n <\/div>\n
\n InstanceMethodExample.java <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>InstanceMethodExample
{
    public <\/span>static void <\/span>main(String [] args)
{
        \/\/Creating an object of the class
InstanceMethodExample obj = new <\/span>InstanceMethodExample();
        \/\/invoking instance method
System.out.println<\/span>(\"The sum is: \"<\/span>+obj.add<\/span>(12, 13));
    }
    int <\/span>s;
    \/\/user-defined method because we have not used static <\/span>keyword
public <\/span>int <\/span>add(int <\/span>a, int <\/span>b)
{
        s = a+b;
        \/\/returning the sum
return <\/span>s;
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n
\n
 The sum is: 25
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9e\u4f8b\u65b9\u6cd5\u6709\u4e24\u79cd:\n <\/div>\n

\u8bbf\u95ee\u5668\uff08accessor\uff09\u65b9\u6cd5: <\/strong> <\/span>
\n \u4fee\u6539\u5668\uff08mutator\uff09\u65b9\u6cd5: <\/strong> <\/span> <\/p>\n

\n \u8bbf\u95ee\u5668\uff08accessor\uff09\u65b9\u6cd5: <\/strong>\u8bfb\u53d6\u5b9e\u4f8b\u53d8\u91cf\u7684\u65b9\u6cd5\u79f0\u4e3a\u8bbf\u95ee\u5668\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u5b83\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u7684\u524d\u7f00\u4e3a
\n get <\/strong>\u3002\u4e5f\u79f0\u4e3a
\n \u83b7\u53d6\u5668<\/strong>\u3002\u5b83\u8fd4\u56de\u503c\u79c1\u6709\u9886\u57df\u7684ue\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
 public <\/span>int <\/span>getId() 
{
    return <\/span>Id;
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4fee\u6539\u5668\uff08mutator\uff09\u65b9\u6cd5: <\/strong>\u8be5\u65b9\u6cd5\u8bfb\u53d6\u5b9e\u4f8b\u53d8\u91cf\uff0c\u5e76\u4fee\u6539\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8bc6\u522b\u51fa\u5b83\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u7684\u524d\u7f00\u662f\u5355\u8bcd
\n set <\/strong>\u3002\u4e5f\u79f0\u4e3a
\n \u8bbe\u7f6e\u8005<\/strong>\u6216
\n \u4fee\u9970\u7b26<\/strong>\u3002\u5b83\u4e0d\u8fd4\u56de\u4efb\u4f55\u4e1c\u897f\u3002\u5b83\u63a5\u53d7\u53d6\u51b3\u4e8e\u5b57\u6bb5\u7684\u76f8\u540c\u6570\u636e\u7c7b\u578b\u7684\u53c2\u6570\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
 public void <\/span>setRoll(int <\/span>roll) 
{
    this<\/span>.roll = roll;
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8bbf\u95ee\u5668\u548c\u66f4\u6539\u5668\u65b9\u6cd5\u7684\u793a\u4f8b<\/h3>\n
\n Student.java <\/strong>\n <\/div>\n
\n
 public <\/span>class <\/span>Student 
{
    private <\/span>int <\/span>roll;
    private <\/span>String name;
    public <\/span>int <\/span>getRoll()
    \/\/accessor method
{
        return <\/span>roll;
    }
    public void <\/span>setRoll(int <\/span>roll)
    \/\/mutator method
{
        this<\/span>.roll = roll;
    }
    public <\/span>String getName()
{
        return <\/span>name;
    }
    public void <\/span>setName(String name)
{
        this<\/span>.name = name;
    }
    public void <\/span>display()
{
        System.out.println<\/span>(\"Roll no.: \"<\/span>+roll);
        System.out.println<\/span>(\"Student name: \"<\/span>+name);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u62bd\u8c61\u65b9\u6cd5<\/h3>\n
\n \u6ca1\u6709\u65b9\u6cd5\u4e3b\u4f53\u7684\u65b9\u6cd5\u79f0\u4e3a\u62bd\u8c61\u65b9\u6cd5\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6ca1\u6709\u5b9e\u73b0\u7684\u65b9\u6cd5\u88ab\u79f0\u4e3a\u62bd\u8c61\u65b9\u6cd5\u3002\u5b83\u603b\u662f\u5728
\n \u62bd\u8c61\u7c7b<\/strong>\u4e2d\u58f0\u660e\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u7c7b\u5177\u6709\u62bd\u8c61\u65b9\u6cd5\uff0c\u5219\u8be5\u7c7b\u672c\u8eab\u5fc5\u987b\u662f\u62bd\u8c61\u7684\u3002\u8981\u521b\u5efa\u62bd\u8c61\u65b9\u6cd5\uff0c\u6211\u4eec\u4f7f\u7528\u5173\u952e\u5b57
\n \u62bd\u8c61<\/strong>\u3002\n <\/div>\n
\n \u8bed\u6cd5<\/strong>\n <\/div>\n
\n
 abstract void <\/span>method_name();
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u62bd\u8c61\u65b9\u6cd5\u7684\u793a\u4f8b<\/h3>\n
\n Demo.java <\/strong>\n <\/div>\n
\n
 abstract class <\/span>Demo
\/\/abstract class
{
    \/\/abstract method declaration
abstract void <\/span>display();
}
public <\/span>class <\/span>Myclass <\/span>extends Demo
{
    \/\/method impelmentation
void display()
{
        System.out.println<\/span>(\"Abstract method?\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[])
{
        \/\/creating object of abstract <\/span>class
Demo obj = new <\/span>MyClass();
        \/\/invoking abstract <\/span>method
obj.display<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n
\n
 Abstract method...
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5de5\u5382\u65b9\u6cd5<\/h3>\n
\n \u8fd9\u662f\u4e00\u79cd\u5c06\u5bf9\u8c61\u8fd4\u56de\u5230\u5176\u6240\u5c5e\u7c7b\u7684\u65b9\u6cd5\u3002\u6240\u6709\u9759\u6001\u65b9\u6cd5\u90fd\u662f\u5de5\u5382\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c
\n NumberFormat obj = NumberFormat.getNumberInstance(); <\/strong>\n <\/div>\n
\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u65b9\u6cd5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/716"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=716"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/716\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}