李銘
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 繼承 > 讓一個類別去 **修改**&**擴充** 另一個的類別的內容。 **當一個類別是其他類別共通的部分,就可以拉出來做成父類別**。 例如,已知要做出正方形、長方形、圓形三種類別,因為三種類別都是形狀,就可以將形狀拉出來做成父類別。 *但一般來說,開發時會是要先預先定義好父類別,去評估子類別會要如何去使用。* ## 存取修飾詞 ### `private` 子類別不能繼承被設定成 `private` 的屬性,也就是沒有存取權,但依然擁有該屬性。 ```[java]= // Shape.java public class Shape { // 設定成 private,因此子類別沒有存取權 private String name = "形狀"; /* 但實際上,name 屬性依然存在,只是取不到。 所以如果設定了 public String getName,可以取到 name */ public String getName() { return name; } } ``` `Circle` 繼承了 `Shape` 類別 ```[java]=12 // Circle.java public class Circle extends Shape { private static float PI = 3.14f; private int r; public Circle(int r){ this.r = r; } } ``` ```[java]=20 // Main.java public class Main { public static void main(String[] args) { Circle circle = new Circle(3); System.out.println(circle.getName()); // 印出"形狀" } } ``` ![執行結果](https://i.imgur.com/EZzb4cS.png) --- ### 存取修飾詞整理 | 存取修飾詞 | 同一類別 | 同一套件 | 不同套件的子類別 | 不同套件的非子類別 | | --- | --- | --- | --- | --- | | private | OK | | | | | default(無修飾詞) | OK | OK | | | | protected | OK | OK | OK | | | public | OK | OK | OK | OK | - protectd 常會用在方法,讓子類別可以使用,但不希望外部可以使用。 *** ## `@Override` >跟父類別同署名 ( 名字 + 參數 ) 的方法,不一定回傳值相同,但有限制。 如果父類別沒有方法,子類別新增一個方法,叫做擴充。有方法,則子類別去覆寫,叫修改,也就是`@override`。 *Java 有規範,父子類別都有的情況下,優先使用子類別。* 實際上,`@Override` 是把父類別的同署名方法給隱藏,不同署名則符合多載 ( Overloading ),兩個方法都能找到,兩者很常被拿來做比較,面試也常考。 **Override & Overloading 比較** - Override : 當父子類別有同署名 ( 同名同參數 ) 的方法,子類別會去覆蓋掉父類別的方法。 - Overloading : 不論在相同類別還是父子類別,有同名不同參數的方法,則會同時存在。 ### 回傳值的限制 子類別在 `@Override` 方法的時候,回傳值必須是父類別 "**原本回傳值的類別**" 或是 "**原本回傳值的子類別**"。 因此,如果是回傳基本資料型態,基本資料型態沒有繼承關係,所以不能更改成回傳其他基本資料型態。 ```[java]= // Shape.java public class Shape { private String name = "形狀"; public String getName() { return name; } public Shape test() { return this; } } ``` 因為 Circle 裡面包含 Shape,因此可以回傳 Circle。 ```[java]=13 // Circle.java public class Circle extends Shape { private static float PI = 3.14f; private int r; public Circle(int r){ this.r = r; } /* * 父類別設定要回傳 Shape, * 因為 Circle 是 Shape 的子類別, * 所以包含所有 Shape 的屬性, * 因此可以回傳 Circle。 * */ @Override public Circle test() { return null; } } ``` ## `super` `super` 就跟 `this` 一樣,`this` 可以用來呼叫自己的建構子,我們可以利用 `super()` 來呼叫父類別的建構子。 ```[java]= // Shape.java public class Shape { private String name; public Shape(String name){ this.name = name; } public String getName() { return name; } public Shape test() { return this; } } ``` 當你在繼承的同時,`java` 會自動幫你在建構子最上方加上 `super()` ,但**如果父類別的建構子有參數**,則系統會不知道你要帶什麼參數,就需要自己呼叫,並且要在第一行呼叫。 ```[java]=17 // Circle.java public class Circle extends Shape { private static float PI = 3.14f; private int r; public Circle(String name, int r){ /* * 當你在繼承的同時, * java 會自動幫你在建構子最上方加上 super(); * */ // super("圓形"); // 直接寫死也可以 super(name); /* super 指的是父類別, * 因此可以用來呼叫父類別的方法, * 不會去呼叫自己 Override 的' * */ super.test(); this.r = r; } @Override public Circle test() { return null; } } ``` 必須要在**建構子第一行**呼叫 `super();` 是因為當繼承了父類別,系統在創建類別時,第一步會先創建父類別。 比如說,要創建一個 `Circle`,系統會先創建 `Shape`,再將 `Circle` 包在 `Shape` 外面,所以如果還有其他類別繼承了 `Circle` ,則會繼續包在外面,以此類推。 *可以確保說,每個類別在創建時,**父類別都已經是創建好的狀態**。* ## 課堂補充 - 當開一個類別,但不希望可以讓人擴充或修改,可以把類別設成 `final`。 - 方法也是,當你不希望被子類別去 Override 你的方法,一樣可以把方法設定成 `final`。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully