{"id":623,"date":"2023-12-25T09:01:01","date_gmt":"2023-12-25T01:01:01","guid":{"rendered":""},"modified":"2023-12-25T09:01:01","modified_gmt":"2023-12-25T01:01:01","slug":"\u91cd\u8f7d\u548c\u91cd\u5199\u3001\u591a\u6001","status":"publish","type":"post","link":"https:\/\/mushiming.com\/623.html","title":{"rendered":"\u91cd\u8f7d\u548c\u91cd\u5199\u3001\u591a\u6001"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4e00\u3001\u91cd\u8f7d\u548c\u91cd\u5199<\/h1>\n

1.\u91cd\u8f7d\u662f\u591a\u6001\u7684\u4e00\u79cd\u4f53\u73b0\uff0c\u65b9\u6cd5\u7684\u91cd\u8f7d\u662f\u8ba9\u7c7b\u4ee5\u7edf\u4e00\u7684\u65b9\u5f0f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u4e00\u79cd\u624b\u6bb5\u3002\u91cd\u8f7d\u662f\u65b9\u6cd5\u540d\u4e0d\u53d8\uff0c\u4f46\u53c2\u6570\u4e00\u5b9a\u53d8\u3002
\u4f7f\u7528\u65b9\u6cd5\u91cd\u8f7d\u65f6\u9700\u6ce8\u610f\u7684\uff1a<\/mark><\/p>\n

\n
    \n
  1. \u540c\u4e00\u4e2a\u7c7b\u4e2d\u65b9\u6cd5\u540d\u76f8\u540c\uff0c\u53c2\u6570\u5217\u8868\u4e0d\u540c<\/li>\n
  2. \u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u80fd\u7528\u4e8e\u533a\u5206\u65b9\u6cd5\u7684\u91cd\u8f7d<\/li>\n<\/ol>\n<\/blockquote>\n

    2.\u6784\u9020\u65b9\u6cd5\u80fd\u91cd\u8f7d\uff0c\u58f0\u660e\u4e3afinal\u7684\u65b9\u6cd5\u4e0d\u80fd\u88ab\u91cd\u8f7d\uff0c\u58f0\u660e\u4e3astatic\u7684\u65b9\u6cd5\u4e0d\u80fd\u88ab\u91cd\u8f7d\u4f46\u80fd\u88ab\u518d\u6b21\u58f0\u660e<\/p>\n

    \u793a\u4f8b\uff1a<\/p>\n

    public<\/span> class<\/span> Book<\/span> { \n   <\/span>\n \/\/\u5b9a\u4e493\u4e2a\u91cd\u8f7d\u65b9\u6cd5<\/span>\n public<\/span> float<\/span>  getPrice<\/span>(<\/span>)<\/span>{ \n   <\/span> \n  return<\/span> 100<\/span>;<\/span>\n }<\/span>\n public<\/span>  float<\/span> getPrice<\/span>(<\/span>int<\/span> page)<\/span>{ \n   <\/span>\n  return<\/span> (<\/span>float<\/span>)<\/span>(<\/span>page*<\/span>0.1<\/span>)<\/span>;<\/span>\n }<\/span>\n public<\/span>  float<\/span>  getPrice<\/span>(<\/span>int<\/span> page ,<\/span>float<\/span> discount)<\/span>{ \n   <\/span>\n  return<\/span> (<\/span>float<\/span>)<\/span> (<\/span>page*<\/span>discount)<\/span>;<\/span>\n }<\/span>\n public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String[<\/span>]<\/span> args)<\/span> { \n   <\/span>\n  Book book=<\/span>new<\/span> Book<\/span>(<\/span>)<\/span>;<\/span>\/\/\u521b\u5efaBook\u5bf9\u8c61<\/span>\n  System.<\/span>out.<\/span>println<\/span>(<\/span>\"default\u56fe\u4e66\u4ef7\u683c:\"<\/span>+<\/span>book.<\/span>getPrice<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n  System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6839\u636e\u9875\u6570\u8ba1\u7b97\u56fe\u4e66\u4ef7\u683c:\"<\/span>+<\/span>book.<\/span>getPrice<\/span>(<\/span>268<\/span>)<\/span>)<\/span>;<\/span> \n      System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6839\u636e\u9875\u6570\u548c\u6298\u6263\u8ba1\u7b97\u56fe\u4e66\u683c:\"<\/span>+<\/span>book.<\/span>getPrice<\/span>(<\/span>360<\/span>,<\/span>0.2f<\/span>)<\/span>)<\/span>;<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n

    \"\u91cd\u8f7d\u548c\u91cd\u5199\u3001\u591a\u6001
    3.\u91cd\u5199<\/p>\n

    \n
      \n
    • \u91cd\u5199\u7684\u65b9\u6cd5\u548c\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u5fc5\u987b\u5177\u6709\u76f8\u540c\u65b9\u6cd5\u540d\u79f0\u3001\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u7c7b\u578b<\/li>\n
    • \u91cd\u5199\u65b9\u6cd5\u4e0d\u80fd\u4f7f\u7528\u6bd4\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u66f4\u4e25\u683c\u7684\u8bbf\u95ee\u6743\u9650<\/li>\n
    • \u91cd\u5199\u65b9\u6cd5\u4e0d\u80fd\u58f0\u660e\u629b\u51fa\u6bd4\u88ab\u91cd\u5199\u65b9\u6cd5\u8303\u56f4\u66f4\u5927\u7684\u5f02\u5e38\u7c7b\u578b<\/li>\n
    • \u91cd\u5199\u662f\u5916\u58f3\u4e0d\u53d8\uff0c\u6838\u5fc3\u53d8<\/li>\n<\/ul>\n<\/blockquote>\n

      \u793a\u4f8b\uff1a<\/p>\n

      public<\/span> class<\/span> Bird<\/span> { \n   <\/span> \/\/ \u7236\u7c7b \u98de\u9e1f<\/span>\n \/\/Bird\u7c7b\u7684fly()\u65b9\u6cd5<\/span>\n public<\/span> void<\/span> fly<\/span>(<\/span>)<\/span>{ \n   <\/span>\n          System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6211\u5728\u84dd\u5929\u767d\u4e91\u95f4\u81ea\u7531\u7684\u98de\u7fd4...\"<\/span>)<\/span>;<\/span>\n }<\/span>\n}<\/span>\npublic<\/span> class<\/span> Ostrich<\/span> extends<\/span> Bird<\/span>{ \n   <\/span>\/\/ \u5b50\u7c7b \u9e35\u9e1f<\/span>\n public<\/span>  void<\/span> fly<\/span>(<\/span>)<\/span>{ \n   <\/span> \/\/\u91cd\u5199\u7236\u7c7b\u65b9\u6cd5<\/span>\n                System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6211\u53ea\u80fd\u5728\u9646\u5730\u4e0a\u62cd\u62cd\u7fc5\u8180\u5954\u8dd1...\"<\/span>)<\/span>;<\/span>\n }<\/span>\n public<\/span>  void<\/span> prey<\/span>(<\/span>)<\/span>{ \n   <\/span> \/\/\u6355\u98df\u65b9\u6cd5<\/span>\n  fly<\/span>(<\/span>)<\/span>;<\/span>\n  System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6267\u884c\u6355\u98df\u65b9\u6cd5\"<\/span>)<\/span>;<\/span>\n }<\/span>\n public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String[<\/span>]<\/span> args)<\/span> { \n   <\/span>\n  Ostrich ostrich=<\/span>new<\/span> Ostrich<\/span>(<\/span>)<\/span>;<\/span>\n  ostrich.<\/span>prey<\/span>(<\/span>)<\/span>;<\/span> \/\/\u8c03\u7528ostrich\u5bf9\u8c61\u7684prey()<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n

      \"\u91cd\u8f7d\u548c\u91cd\u5199\u3001\u591a\u6001<\/p>\n

      4.\u91cd\u5199\u8986\u76d6\u95ee\u9898
      \u5b50\u7c7b\u5305\u542b\u4e0e\u7236\u7c7b\u540c\u540d\u7684\u65b9\u6cd5\uff0c\u8be5\u73b0\u8c61\u88ab\u79f0\u4e3a\u65b9\u6cd5\u91cd\u5199\uff0c\u4e5f\u88ab\u79f0\u4e3a\u65b9\u6cd5\u8986\u76d6\u3002<\/p>\n

      \u793a\u4f8b\uff1a<\/p>\n

      public<\/span> class<\/span> Ostrich<\/span> extends<\/span> Bird<\/span>{ \n   <\/span>\n public<\/span>  void<\/span> fly<\/span>(<\/span>)<\/span>{ \n   <\/span>\n  System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6211\u53ea\u80fd\u5728\u9646\u5730\u4e0a\u62cd\u62cd\u7fc5\u8180\u5954\u8dd1...\"<\/span>)<\/span>;<\/span>\n }<\/span>\n \/\/\u6355\u98df\u65b9\u6cd5<\/span>\n public<\/span>  void<\/span> prey<\/span>(<\/span>)<\/span>{ \n   <\/span>\n  \/\/\u5728prey()\u65b9\u6cd5\u4e2d\u4f7f\u7528super\u5173\u952e\u8c03\u7528\u7236\u7c7b\u7684fly()\u65b9\u6cd5<\/span>\n  super<\/span>.<\/span>fly<\/span>(<\/span>)<\/span>;<\/span>\n  System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u6267\u884c\u6355\u98df\u65b9\u6cd5\"<\/span>)<\/span>;<\/span>\n }<\/span>\n public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String[<\/span>]<\/span> args)<\/span> { \n   <\/span>\n  \/\/\u521b\u5efaOstrich\u5bf9\u8c61<\/span>\n  Ostrich ostrich=<\/span>new<\/span> Ostrich<\/span>(<\/span>)<\/span>;<\/span>\n  ostrich.<\/span>prey<\/span>(<\/span>)<\/span>;<\/span>\/\/\u8c03\u7528ostrich\u5bf9\u8c61\u7684fly()<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n

      \u4e8c\u3001\u591a\u6001<\/h1>\n

      1.\u591a\u6001\u662f\u6307\u540c\u4e00\u64cd\u4f5c\u4f5c\u7528\u4e8e\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u89e3\u91ca\uff0c\u4ea7\u751f\u4e0d\u540c\u7684\u6267\u884c\u7ed3\u679c\u3002
      \u5728\u7f16\u8bd1\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7236\u7c7b\u7684\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\uff0c\u800c\u5728\u8fd0\u884c\u65f6\uff0c\u5219\u901a\u8fc7\u7236\u7c7b\u6240\u6307\u5411\u7684\u5b50\u7c7b\u5bf9\u8c61\uff0c\u8c03\u7528\u5b50\u7c7b\u4e2d\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n

      \u4ee3\u7801\u5982\u4e0b\uff08\u793a\u4f8b\uff09\uff1a<\/font><\/p>\n

      \/\/\u7236\u7c7b \u4eba\u7269<\/span>\npublic<\/span> class<\/span> Figure<\/span> { \n   <\/span>\n protected<\/span> String name;<\/span>\n public<\/span> void<\/span> attack<\/span>(<\/span>)<\/span>{ \n   <\/span>\n  \/\/\u6b64\u5904\u4e0d\u8fdb\u884c\u5177\u4f53\u7684\u653b\u51fb\u64cd\u4f5c\uff0c\u4e0d\u540c\u804c\u4e1a\u7684\u82f1\u96c4\u653b\u51fb\u65b9\u5f0f\u4e0d\u540c<\/span>\n }<\/span>\n public<\/span>  void<\/span> run<\/span>(<\/span>)<\/span>{ \n   <\/span>  \n  System.<\/span>out.<\/span>println<\/span>(<\/span>\"\u5728\u5954\u8dd1\"<\/span>)<\/span>;<\/span>\n }<\/span>\n}<\/span>\n \n\/\/\u5b50\u7c7b \u6218\u58eb<\/span>\npublic<\/span> class<\/span> Warrior<\/span>  extends<\/span>  Figure<\/span>{ \n   <\/span>\n \/\/\u91cd\u5199\u7236\u7c7b\u7684attack()\u65b9\u6cd5<\/span>\n public<\/span> void<\/span> attack<\/span>(<\/span>)<\/span>{ \n   <\/span>\n             System.<\/span>out.<\/span>println<\/span>(<\/span>this<\/span>.<\/span>name+<\/span>\"\u6b63\u5728\u7269\u7406\u653b\u51fb......\"<\/span>)<\/span>;<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n
      \/\/\u5b50\u7c7b \u6cd5\u5e08<\/span>\npublic<\/span> class<\/span> Master<\/span> extends<\/span> Figure<\/span> { \n   <\/span>\n \/\/\u91cd\u5199\u7236\u7c7b\u7684attack()\u65b9\u6cd5<\/span>\n public<\/span> void<\/span> attack<\/span>(<\/span>)<\/span>{ \n   <\/span>  \n  System.<\/span>out.<\/span>println<\/span>(<\/span>this<\/span>.<\/span>name+<\/span>\"\u6b63\u5728\u9b54\u6cd5\u653b\u51fb......\"<\/span>)<\/span>;<\/span>\n }<\/span> \n public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String[<\/span>]<\/span> args)<\/span> { \n   <\/span>\n      Figure master=<\/span>new<\/span> Master<\/span>(<\/span>)<\/span>;<\/span>\n      master.<\/span>name=<\/span>\"\u6076\u9b54\u5deb\u5e08\"<\/span>;<\/span>\n      Figure warrior=<\/span>new<\/span> Warrior<\/span>(<\/span>)<\/span>;<\/span>\n      warrior.<\/span>name=<\/span>\"\u64bc\u5730\u795e\u725b\"<\/span>;<\/span>\n      master.<\/span>attack<\/span>(<\/span>)<\/span>;<\/span>\n      warrior.<\/span>attack<\/span>(<\/span>)<\/span>;<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n

      \"\u91cd\u8f7d\u548c\u91cd\u5199\u3001\u591a\u6001<\/p>\n

      2.\u5f15\u7528\u53d8\u91cf\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/p>\n

      \u5f15\u7528\u53d8\u91cf\u53ea\u80fd\u8c03\u7528\u5176\u7f16\u8bd1\u65f6\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u80fd\u8c03\u7528\u8fd0\u884c\u65f6\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u5982\u679c\u9700\u8981\u8ba9\u5f15\u7528\u53d8\u91cf\u8c03\u7528\u8fd0\u884c\u65f6\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u5219\u5fc5\u987b\u5c06\u5176\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u4e3a\u8fd0 \u884c\u65f6\u7c7b\u578b\uff0c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u9700\u501f\u52a9\u4e8e\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\u3002
      3.instanced of \u8fd0\u7b97\u7b26
      \u8bed\u6cd5\uff1a<\/p>\n

      a instanceof<\/span> A<\/span>\n<\/code><\/pre>\n

      \u793a\u4f8b\uff1a<\/p>\n

      \/\/\u5b50\u7c7b \u6cd5\u5e08<\/span>\npublic<\/span> class<\/span> Master<\/span> extends<\/span> Figure<\/span> { \n   <\/span>\n \/\/\u91cd\u5199\u7236\u7c7b\u7684attack()\u65b9\u6cd5<\/span>\n public<\/span> void<\/span> attack<\/span>(<\/span>)<\/span>{ \n   <\/span>  \n             System.<\/span>out.<\/span>println<\/span>(<\/span>this<\/span>.<\/span>name+<\/span>\"\u6b63\u5728\u9b54\u6cd5\u653b\u51fb......\"<\/span>)<\/span>;<\/span>\n }<\/span>\n \/\/\u5b50\u7c7b\u7279\u6709\u7684\u8f70\u70b8\u529f\u80fd<\/span>\n public<\/span> void<\/span> bomb<\/span>(<\/span>)<\/span>{ \n   <\/span>\n             System.<\/span>out.<\/span>println<\/span>(<\/span>this<\/span>.<\/span>name+<\/span>\u201c\u6b63\u5728\u9b54\u6cd5\u8f70\u70b8.<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span>\")<\/span>;<\/span>\n }<\/span> \n public<\/span> static<\/span> void<\/span> mafigurein<\/span>(<\/span>String[<\/span>]<\/span> args)<\/span> { \n   <\/span>\n      Figure figure=<\/span>new<\/span> Master<\/span>(<\/span>)<\/span>;<\/span>\n      figure.<\/span>name=<\/span>\"\u6076\u9b54\u5deb\u5e08\"<\/span>;<\/span>\n                       figure.<\/span>attack<\/span>(<\/span>)<\/span>;<\/span>\n                       if<\/span>(<\/span>figure instanceof<\/span> Master<\/span> )<\/span>\n        Master master =<\/span> (<\/span>Master)<\/span>figure;<\/span> \/\/ \u4f7f\u7528\u5411\u4e0b\u7c7b\u578b\u8f6c\u6362<\/span>\n       master.<\/span>bomb<\/span>(<\/span>)<\/span>;<\/span>   \/\/\u6210\u529f\u8c03\u7528Master\u7c7b\u7684bomb<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n

      \u603b\u7ed3<\/h1>\n
        \n
      • \u91cd\u8f7d\u5c31\u662f\u5728\u7c7b\u4e2d\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u65b9\u6cd5\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u540d\u5b57\uff0c\u4f46\u5177\u6709\u4e0d\u540c\u7684\u53c2\u6570\u548c\u4e0d\u540c\u7684\u5b9a\u4e49\u3002<\/li>\n
      • \u91cd\u5199\u5c31\u662f\u5728\u5b50\u7c7b\u65b9\u6cd5\u91cd\u5199\u7236\u7c7b\u65b9\u6cd5\uff0c\u91cd\u5199\u7684\u65b9\u6cd5\u548c\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u5fc5\u987b\u5177\u6709\u76f8\u540c\u65b9\u6cd5\u540d\u79f0\u3001\u53c2\u6570<\/li>\n
      • \u5217\u8868\u548c\u8fd4\u56de\u7c7b\u578b\u3002<\/li>\n
      • \u591a\u6001\u662f\u6307\u540c\u4e00\u64cd\u4f5c\u4f5c\u7528\u4e8e\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u89e3\u91ca\uff0c\u4ea7\u751f\u4e0d\u540c\u7684\u6267\u884c\u7ed3\u679c\u3002<\/li>\n
      • \u5728\u7f16\u8bd1\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7236\u7c7b\u7684\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\uff0c\u800c\u5728\u8fd0\u884c\u65f6\uff0c\u5219\u901a\u8fc7\u7236\u7c7b\u6240\u6307\u5411\u7684\u5b50\u7c7b\u5bf9\u8c61\uff0c\u8c03\u7528\u5b50\u7c7b\u4e2d\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u91cd\u8f7d\u548c\u91cd\u5199\u3001\u591a\u6001\u4e00\u3001\u91cd\u8f7d\u548c\u91cd\u51991.\u91cd\u8f7d\u662f\u591a\u6001\u7684\u4e00\u79cd\u4f53\u73b0\uff0c\u65b9\u6cd5\u7684\u91cd\u8f7d\u662f\u8ba9\u7c7b\u4ee5\u7edf\u4e00\u7684\u65b9\u5f0f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u4e00\u79cd\u624b\u6bb5\u3002\u91cd\u8f7d\u662f...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/623"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}