{"id":1213,"date":"2022-05-30T22:38:59","date_gmt":"2022-05-30T14:38:59","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1213"},"modified":"2024-03-21T17:04:18","modified_gmt":"2024-03-21T09:04:18","slug":"kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0%ef%bc%882%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/05\/30\/kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0%ef%bc%882%ef%bc%89\/","title":{"rendered":"Kotlin\u5b89\u5353\u5f00\u53d1\u5b66\u4e60\uff082\uff09"},"content":{"rendered":"\n<!--more-->\n\n\n\n<p>\u548c\u5f88\u591a\u73b0\u4ee3\u9ad8\u7ea7\u8bed\u8a00\u4e00\u6837\uff0cKotlin\u4e5f\u662f\u9762\u5411\u5bf9\u8c61\u7684\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u7c7b\u4e0e\u5bf9\u8c61<\/p>\n\n\n\n<p>Kotlin\u7684\u7c7b\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Person {\n    var name = \"\"\n    var age = 0\n    fun eat() {\n       println(name + \" is eating. He is \" + age + \" years old.\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5bf9\u7c7b\u8fdb\u884c\u5b9e\u4f8b\u5316\u5e76\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val p = Person()\nval age=p.age<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5728\u4f7f\u7528Kotlin\u65f6\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u50cfJava\u90a3\u6837\u5b9a\u4e49 get\/set \u65b9\u6cd5\uff0c\u5728\u8c03\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\u4e5f\u4e0d\u9700\u8981\u8c03\u7528get\/set\u65b9\u6cd5\u6765\u4f7f\u7528\u5c5e\u6027\u3002\u4f7f\u7528Kotlin\u65f6\uff0c\u6211\u4eec\u662f\u76f4\u63a5\u5bf9\u5c5e\u6027\u8fdb\u884c\u8d4b\u503c\u548c\u8bfb\u53d6\uff0c\u56e0\u4e3aKotlin\u4f1a\u81ea\u52a8\u5c06\u8d4b\u503c\u3001\u8bfb\u53d6\u8bed\u53e5\u8f6c\u6362\u6210get\/set\u65b9\u6cd5\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u4f7f\u7528\u3002<\/p>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u7ee7\u627f\u4e0e\u6784\u9020\u51fd\u6570<\/p>\n\n\n\n<p>Kotlin\u8fdb\u884c\u7c7b\u7684\u7ee7\u627f\u6d41\u7a0b\u4e0eJava\u6709\u7740\u4e0d\u5c0f\u7684\u5dee\u8ddd\u3002\u9996\u5148\uff0c\u60f3\u8981\u4e00\u4e2a\u7c7b\u88ab\u7ee7\u627f\uff0c\u9996\u5148\u9700\u8981\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u88ab\u7ee7\u627f\u3002\u8fd9\u5c31\u662fKotlin\u4e0d\u540c\u7684\u5730\u65b9\uff0c\u5728Kotlin\u4e2d\u4efb\u4f55\u4e00\u4e2a\u975e\u62bd\u8c61\u7c7b\u9ed8\u8ba4\u90fd\u662f\u4e0d\u53ef\u4ee5\u88ab\u7ee7\u627f\u7684\uff0c\u76f8\u5f53\u4e8eJava\u4e2d\u7ed9\u7c7b\u58f0\u660e\u4e86final\u5173\u952e\u5b57\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u88ab\u7ee7\u627f\uff0c\u9700\u8981\u5728\u7c7b\u524d\u6dfb\u52a0 \u4fee\u9970\u7b26\u201dopen\u201c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">open class Person {\n   \u2026\n}<\/code><\/pre>\n\n\n\n<p>\u52a0\u4e0aopen\u5173\u952e\u5b57\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u662f\u5728\u4e3b\u52a8\u544a\u8bc9Kotlin\u7f16\u8bd1\u5668\uff0cPerson\u8fd9\u4e2a\u7c7b\u662f\u4e13\u95e8\u4e3a\u7ee7\u627f\u800c\u8bbe\u8ba1\u7684\uff0c\u8fd9\u6837Person\u7c7b\u5c31\u5141\u8bb8\u88ab\u7ee7\u627f\u4e86\u3002<\/p>\n\n\n\n<p>\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u8ba9\u5b50\u7c7b\u53bb\u7ee7\u627f\u7236\u7c7b\u3002<\/p>\n\n\n\n<p>\u5728Java\u4e2d\u7ee7\u627f\u7684\u5173\u952e\u5b57\u662fextends\uff0c\u800c\u5728Kotlin\u4e2d\u53d8\u6210\u4e86\u4e00\u4e2a\u5192\u53f7\uff0c\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student : Person() {\n    var sno = \"\"\n    var grade = 0\n}<\/code><\/pre>\n\n\n\n<p>\u4e0eJava\u4e2d\u4e0d\u540c\uff0cKotlin\u4e2d\u7ee7\u627f\u9700\u8981\u5728\u7ee7\u627f\u7c7b\u540e\u9762\u52a0\u4e0a\u4e00\u5bf9\u62ec\u53f7\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u52a0\u4e0a\u62ec\u53f7\uff0c\u8fd9\u5c31\u6d89\u53ca\u4e86Kotlin\u4e3b\u6784\u9020\u51fd\u6570\u548c\u6b21\u6784\u9020\u51fd\u6570\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<p>\u4e3b\u6784\u9020\u51fd\u6570\u662f\u6211\u4eec\u6700\u5e38\u7528\u7684\u6784\u9020\u51fd\u6570\uff0c\u6bcf\u4e2a\u7c7b\u9ed8\u8ba4\u90fd\u4f1a\u6709\u4e00\u4e2a\u4e0d\u5e26\u53c2\u6570\u7684\u4e3b\u6784\u9020\u51fd\u6570\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u663e\u5f0f\u5730\u7ed9\u5b83\u6307\u660e\u53c2\u6570\u3002\u4e3b\u6784\u9020\u51fd\u6570\u7684\u7279\u70b9\u662f\u6ca1\u6709\u51fd\u6570\u4f53\uff0c\u76f4\u63a5\u5b9a\u4e49\u5728\u7c7b\u540d\u7684\u540e\u9762\u5373\u53ef\u3002\u6bd4\u5982\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student(val sno: String, val grade: Int) : Person() {<br>}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u5b66\u53f7\u548c\u5e74\u7ea7\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u90fd\u653e\u5230\u4e86\u4e3b\u6784\u9020\u51fd\u6570\u5f53\u4e2d\uff0c\u8fd9\u5c31\u8868\u660e\u5728\u5bf9Student\u7c7b\u8fdb\u884c\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u4f20\u5165\u6784\u9020\u51fd\u6570\u4e2d\u8981\u6c42\u7684\u6240\u6709\u53c2\u6570\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val student = Student(\"a123\", 5)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u521b\u5efa\u4e86\u4e00\u4e2aStudent\u7684\u5bf9\u8c61\uff0c\u540c\u65f6\u6307\u5b9a\u8be5\u5b66\u751f\u7684\u5b66\u53f7\u662fa123\uff0c\u5e74\u7ea7\u662f5\u3002\u53e6\u5916\uff0c\u7531\u4e8e\u6784\u9020\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u662f\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u65f6\u5019\u4f20\u5165\u7684\uff0c\u4e0d\u50cf\u4e4b\u524d\u7684\u5199\u6cd5\u90a3\u6837\u8fd8\u5f97\u91cd\u65b0\u8d4b\u503c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u53c2\u6570\u5168\u90e8\u58f0\u660e\u6210val\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4e3b\u6784\u9020\u51fd\u6570\u6ca1\u6709\u51fd\u6570\u4f53\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u5728\u4e3b\u6784\u9020\u51fd\u6570\u91cc\u7f16\u5199\u4e00\u4e9b\u903b\u8f91\uff0c\u53ef\u4ee5\u4f7f\u7528init\u7ed3\u6784\u4f53\uff0c\u6240\u6709\u4e3b\u6784\u9020\u51fd\u6570\u4e2d\u7684\u903b\u8f91\u90fd\u53ef\u4ee5\u5199\u5728\u91cc\u9762\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student(val sno: String, val grade: Int) : Person() {\n    init {\n        println(\"sno is \" + sno)\n        println(\"grade is \" + grade)\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5728\u521b\u5efa\u5b9e\u4f8b\u4e4b\u540e\u4e00\u5b9a\u4f1a\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u521d\u59cb\u503c\u7684\u5bf9\u8c61\uff0c\u4e14\u6253\u5370\u8fd9\u4e9b\u503c\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u8fd9\u548c\u90a3\u5bf9\u62ec\u53f7\u53c8\u6709\u4ec0\u4e48\u5173\u7cfb\u5462\uff1f\u8fd9\u5c31\u6d89\u53ca\u4e86Java\u7ee7\u627f\u7279\u6027\u4e2d\u7684\u4e00\u4e2a\u89c4\u5b9a\uff0c\u5b50\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570\u5fc5\u987b\u8c03\u7528\u7236\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u4e2a\u89c4\u5b9a\u5728Kotlin\u4e2d\u4e5f\u8981\u9075\u5b88\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u7ee7\u627f\u7279\u6027\u7684\u89c4\u5b9a\uff0c\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u5fc5\u987b\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ef\u662f\u4e3b\u6784\u9020\u51fd\u6570\u5e76\u6ca1\u6709\u51fd\u6570\u4f53\u3002Kotlin\u5f53\u7136\u6ca1\u6709\u91c7\u7528\u8fd9\u79cd\u8bbe\u8ba1\uff0c\u800c\u662f\u7528\u4e86\u53e6\u5916\u4e00\u79cd\u7b80\u5355\u4f46\u662f\u53ef\u80fd\u4e0d\u592a\u597d\u7406\u89e3\u7684\u8bbe\u8ba1\u65b9\u5f0f\uff1a\u62ec\u53f7\u3002\u5b50\u7c7b\u7684\u4e3b\u6784\u9020\u51fd\u6570\u8c03\u7528\u7236\u7c7b\u4e2d\u7684\u54ea\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5728\u7ee7\u627f\u7684\u65f6\u5019\u901a\u8fc7\u62ec\u53f7\u6765\u6307\u5b9a\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u5728\u7236\u7c7b\u4e3b\u6784\u9020\u51fd\u6570\u4e0a\u5b9a\u4e49\u53c2\u6570\uff0c\u5728\u7ee7\u627f\u65f6\u4e5f\u8981\u5728\u62ec\u53f7\u91cc\u5b9a\u4e49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student(val sno: String, val grade: Int, name: String, age: Int) :\n  Person(name, age) {\n     \u2026\n}<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u6765\u521b\u5efa\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val student = Student(\"a123\", 5, \"Jack\", 19)<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u6b21\u6784\u9020\u51fd\u6570\uff0c\u4efb\u4f55\u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a\u4e3b\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u53ef\u4ee5\u6709\u591a\u4e2a\u6b21\u6784\u9020\u51fd\u6570\u3002\u6b21\u6784\u9020\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\uff0c\u8fd9\u4e00\u70b9\u548c\u4e3b\u6784\u9020\u51fd\u6570\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u662f\u6709\u51fd\u6570\u4f53\u7684\u3002<\/p>\n\n\n\n<p>Kotlin\u89c4\u5b9a\uff0c\u5f53\u4e00\u4e2a\u7c7b\u65e2\u6709\u4e3b\u6784\u9020\u51fd\u6570\u53c8\u6709\u6b21\u6784\u9020\u51fd\u6570\u65f6\uff0c\u6240\u6709\u7684\u6b21\u6784\u9020\u51fd\u6570\u90fd\u5fc5\u987b\u8c03\u7528\u4e3b\u6784\u9020\u51fd\u6570\uff08\u5305\u62ec\u95f4\u63a5\u8c03\u7528\uff09\u3002\u8fd9\u91cc\u6211\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u5c31\u80fd\u7b80\u5355\u9610\u660e\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student(val sno: String, val grade: Int, name: String, age: Int) :\n   Person(name, age) {\n    constructor(name: String, age: Int) : this(\"\", 0, name, age) {\n    }\n    constructor() : this(\"\", 0) {\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6b21\u6784\u9020\u51fd\u6570\u662f\u901a\u8fc7constructor\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u7684\uff0c\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u6b21\u6784\u9020\u51fd\u6570\uff1a\u7b2c\u4e00\u4e2a\u6b21\u6784\u9020\u51fd\u6570\u63a5\u6536name\u548cage\u53c2\u6570\uff0c\u7136\u540e\u5b83\u53c8\u901a\u8fc7this\u5173\u952e\u5b57\u8c03\u7528\u4e86\u4e3b\u6784\u9020\u51fd\u6570\uff0c\u5e76\u5c06sno\u548cgrade\u8fd9\u4e24\u4e2a\u53c2\u6570\u8d4b\u503c\u6210\u521d\u59cb\u503c\uff1b\u7b2c\u4e8c\u4e2a\u6b21\u6784\u9020\u51fd\u6570\u4e0d\u63a5\u6536\u4efb\u4f55\u53c2\u6570\uff0c\u5b83\u901a\u8fc7this\u5173\u952e\u5b57\u8c03\u7528\u4e86\u6211\u4eec\u521a\u624d\u5b9a\u4e49\u7684\u7b2c\u4e00\u4e2a\u6b21\u6784\u9020\u51fd\u6570\uff0c\u5e76\u5c06name\u548cage\u53c2\u6570\u4e5f\u8d4b\u503c\u6210\u521d\u59cb\u503c\uff0c\u7531\u4e8e\u7b2c\u4e8c\u4e2a\u6b21\u6784\u9020\u51fd\u6570\u95f4\u63a5\u8c03\u7528\u4e86\u4e3b\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u8fd9\u4ecd\u7136\u662f\u5408\u6cd5\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u62e5\u6709\u4e86\u4e09\u79cd\u65b9\u6cd5\u6765\u5b9e\u4f8b\u5316\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val student1 = Student()<br>val student2 = Student(\"Jack\", 19)<br>val student3 = Student(\"a123\", 5, \"Jack\", 19)<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u518d\u6765\u770b\u4e00\u79cd\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\uff1a\u7c7b\u4e2d\u53ea\u6709\u6b21\u6784\u9020\u51fd\u6570\uff0c\u6ca1\u6709\u4e3b\u6784\u9020\u51fd\u6570\u3002\u8fd9\u79cd\u60c5\u51b5\u771f\u7684\u5341\u5206\u5c11\u89c1\uff0c\u4f46\u5728Kotlin\u4e2d\u662f\u5141\u8bb8\u7684\u3002\u5f53\u4e00\u4e2a\u7c7b\u6ca1\u6709\u663e\u5f0f\u5730\u5b9a\u4e49\u4e3b\u6784\u9020\u51fd\u6570\u4e14\u5b9a\u4e49\u4e86\u6b21\u6784\u9020\u51fd\u6570\u65f6\uff0c\u5b83\u5c31\u662f\u6ca1\u6709\u4e3b\u6784\u9020\u51fd\u6570\u7684\u3002\u6211\u4eec\u7ed3\u5408\u4ee3\u7801\u6765\u770b\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student : Person {\n    constructor(name: String, age: Int) : super(name, age) {\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u56e0\u4e3aStudent\u540e\u9762\u6ca1\u6709\u663e\u793a\u5730\u5b9a\u4e49\u4e3b\u6784\u9020\u51fd\u6570\uff0c\u540c\u65f6\u53c8\u56e0\u4e3a\u5b9a\u4e49\u4e86\u6b21\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u73b0\u5728Student\u7c7b\u662f\u6ca1\u6709\u4e3b\u6784\u9020\u51fd\u6570\u7684\u3002\u56e0\u4e3a\u6ca1\u6709\uff0c\u6240\u4ee5\u7ee7\u627fPerson\u65f6\u4e0d\u9700\u8981\u52a0\u4e0a\u62ec\u53f7\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u7531\u4e8e\u6ca1\u6709\u4e3b\u6784\u9020\u51fd\u6570\uff0c\u6b21\u6784\u9020\u51fd\u6570\u53ea\u80fd\u76f4\u63a5\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u4e0a\u8ff0\u4ee3\u7801\u4e5f\u662f\u5c06this\u5173\u952e\u5b57\u6362\u6210\u4e86super\u5173\u952e\u5b57\uff0c\u8fd9\u90e8\u5206\u5c31\u548cJava\u5f88\u50cf\u4e86\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u63a5\u53e3<\/p>\n\n\n\n<p>Kotlin\u7684\u63a5\u53e3\u548cJava\u7684\u5f88\u50cf\uff0c\u4f7f\u7528interface\u4fee\u9970\uff0c\u51fd\u6570\u4e0d\u8981\u6c42\u6709\u51fd\u6570\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">interface Study {\n    fun readBooks()\n    fun doHomework()\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u53bb\u5b9e\u73b0\u63a5\u53e3\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Student(name: String, age: Int) : Person(name, age), Study {\n    override fun readBooks() {\n        println(name + \" is reading.\")\n    }\n    override fun doHomework() {\n        println(name + \" is doing homework.\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728Java\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528extends\u8fdb\u884c\u7ee7\u627f\uff0c\u4f7f\u7528implements\u8fdb\u884c\u63a5\u53e3\u4f7f\u7528\uff0c\u4f46\u5728Kotlin\u4e2d\u7edf\u4e00\u4f7f\u7528\u5192\u53f7\uff0c\u4e2d\u95f4\u7528\u9017\u53f7\u8fdb\u884c\u5206\u9694\u3002\u4e14\u56e0\u4e3a\u63a5\u53e3\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u5199\u4e0a\u62ec\u53f7\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u63a5\u53e3\u4e2d\u5b9a\u4e49\u4e86\u5f85\u5b9e\u73b0\u51fd\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u4f7f\u7528\u63a5\u53e3\u540e\u5fc5\u987b\u5bf9\u51fd\u6570\u8fdb\u884c\u5b9e\u73b0\u3002Kotlin\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528override\u5173\u952e\u8bcd\u6765\u91cd\u5199\u6216\u8005\u5b9e\u73b0\u63a5\u53e3\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916Kotlin\u8fd8\u589e\u52a0\u4e86\u4e00\u4e2a\u989d\u5916\u7684\u529f\u80fd\uff1a\u5141\u8bb8\u5bf9\u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u8fdb\u884c\u9ed8\u8ba4\u5b9e\u73b0\u3002\u4ee3\u7801\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">interface Study {\n     fun readBooks()\n     fun doHomework() {\n        println(\"do homework default implementation.\")\n     }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5728 doHomework\u65b9\u6cd5\u4e0a\u52a0\u4e86\u51fd\u6570\u4f53\u3002\u5982\u679c\u63a5\u53e3\u4e2d\u4e00\u4e2a\u51fd\u6570\u62e5\u6709\u4e86\u51fd\u6570\u4f53\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f53\u7684\u5185\u5bb9\u5c31\u662f\u5b83\u7684\u9ed8\u8ba4\u5b9e\u73b0\u3002\u73b0\u5728\u5f53\u4e00\u4e2a\u7c7b\u53bb\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u65f6\uff0c\u53ea\u4f1a\u5f3a\u5236\u8981\u6c42\u5b9e\u73b0readBooks()\u51fd\u6570\uff0c\u800cdoHomework()\u51fd\u6570\u5219\u53ef\u4ee5\u81ea\u7531\u9009\u62e9\u5b9e\u73b0\u6216\u8005\u4e0d\u5b9e\u73b0\uff0c\u4e0d\u5b9e\u73b0\u65f6\u5c31\u4f1a\u81ea\u52a8\u4f7f\u7528\u9ed8\u8ba4\u7684\u5b9e\u73b0\u903b\u8f91\u3002<\/p>\n\n\n\n<div style=\"height:28px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">Java\u548cKotlin\u51fd\u6570\u53ef\u89c1\u6027\u4fee\u9970\u7b26\u5bf9\u7167\u8868<\/p>\n\n\n\n<div class=\"wp-block-getwid-table has-horizontal-align-center has-vertical-align-middle\"><table><thead><tr><th>\u4fee\u9970\u7b26<\/th><th>Java<\/th><th>Kotlin<\/th><\/tr><\/thead><tbody><tr><td style=\"text-align:center;vertical-align:middle\">public<\/td><td>\u6240\u6709\u7c7b\u53ef\u89c1<\/td><td>\u6240\u6709\u7c7b\u53ef\u89c1(\u9ed8\u8ba4)<\/td><\/tr><tr><td>private<\/td><td>\u5f53\u524d\u7c7b\u53ef\u89c1<\/td><td>\u5f53\u524d\u7c7b\u53ef\u89c1<\/td><\/tr><tr><td>protected<\/td><td>\u5f53\u524d\u7c7b\u3001\u5b50\u7c7b\u3001\u540c\u4e00\u5305\u8def\u5f84\u4e0b\u7684\u7c7b\u53ef\u89c1<\/td><td>\u5f53\u524d\u7c7b\u3001\u5b50\u7c7b\u53ef\u89c1<\/td><\/tr><tr><td>default<\/td><td>\u540c\u4e00\u5305\u8def\u5f84\u4e0b\u7684\u7c7b\u53ef\u89c1\uff08\u9ed8\u8ba4\uff09<\/td><td>\u65e0<\/td><\/tr><tr><td>internal<\/td><td>\u65e0<\/td><td>\u540c\u4e00\u6a21\u5757\u4e2d\u7684\u7c7b\u53ef\u89c1<\/td><\/tr><\/tbody><\/table><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[1,16],"tags":[],"class_list":["post-1213","post","type-post","status-publish","format-standard","hentry","category-wcd","category-16"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1213"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1213"}],"version-history":[{"count":4,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1213\/revisions"}],"predecessor-version":[{"id":1231,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1213\/revisions\/1231"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}