王嘉暐
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 'Java Learning' --- # Java 基礎教學 --- ## 第三堂、物件導向(Object Oriented Programming,OOP) --- ### 壹、前情提要 一、類別 class 在介紹什麼是物件導向之前,我們先介紹 class(類別)。 我們可以想像 class(類別)是一張設計圖,而在這張設計圖中,我們必須規劃一些項目,以汽車為例,一台汽車,可以有廠牌、里程數、價格等資訊...。 因此我們可以規劃一個類別如下: ``` class Car{ //宣告 公開 型別 變數 public String brand; public int drive_km; public int price; //宣告 公開 型別 方法名稱(){} public String getInformation(){ //回傳 廠牌:OOO,里程數:123,價格:123456 return "廠牌:" + brand+",里程數:" + drive_km + ",價格:" + price; } } ``` 二、物件 object 物件(object)可以說是類別的實體產物(實例),也就是在有了汽車的設計圖的情況下,我們可以製作出很多台汽車,而每台汽車都是獨立的,彼此互不影響。 以下為舉例: ``` //實作汽車 Car Toyota = new Car("Toyota","1200",345678); Car BMW = new Car("BMW","3400",5678912); //印出資料 Toyota.getInformation(); BMW.getInformation(); ``` --- ### 貳、介紹物件導向三大佬(三大特性) 1. 封裝 (Encapsulation) 2. 繼承 (Inheritance) 3. 多型 (Polymorphism) --- 1. 封裝 (Encapsulation) 封裝顧名思義就是將東西封起來、包裝起來。在程式中我們利用封裝將物件內部的資料包起來、隱藏起來(ex:設置 private),我們能利用特定允許之窗口向其索取資料(ex:物件本身所提供之介面interface),反之,若沒有經過允許之窗口來取資料,可能會吃閉門羹喔!! 例如:我們上面介紹可以透過 getInformation() 這個方法取得汽車資訊,然而我們(使用者)並不需要內部如何運作。在職場上,稱之為商業邏輯。 --- 2. 繼承 (Inheritance) 寫程式的過程中,會遇到一些大家(類別中)共同擁有的屬性(attribute)或方法(method),這時候,我們可以使用繼承來將這些屬性或方法交給另外的類別使用。我們稱提供屬性或方法的類別為父類別(parent class),而使用這些的類別為子類別(child class)。 舉汽車的類別為例,此時汽車為父類別,而使用父類別功能的 Uber 為子類別,請看以下範例。(後面會詳細教學詳細的繼承,因此這裡簡單地撰寫程式碼) ``` Car 父類別 public class Car { public String run() { return "run()..."; } } ``` ``` Uber 子類別 public class Uber extends Car{ public String reserve() { return "reserve()..."; } } ``` ``` Main 主類別 public class MainClass { public static void main(String[] args) { Uber uber = new Uber(); System.out.println(uber.reserve()); System.out.println(uber.run()); } } ``` --- 3. 多型 (Polymorphism) 多型可以說是:多個相同名稱的方法,傳入不同的參數,會執行不同的敘述(結果) 多型包含兩種: 1. 多載(Overloading) 2. 覆寫(Overriding) --- > 多載(Overloading) 在相同類別(class)中,定義相同名稱,但是參數個數不同,或是參數型態不同的方法method(函式 function)。 廢話不多說,我們直接看範例(矩形面積): ``` //正方形面積 (邊長x邊長) public int calculateArea(int length){ return length * length; } //長方形面積 (長x寬) public int calculateArea(int length, int width){ return length * width; } ``` > 覆寫(Overriding) 覆寫內容顧名思義就是將原有的內容蓋掉。在程式中,即為覆寫掉父類別中的方法(函式) 下面以動物類別為例: ``` class Animal{ public int foot = 1; public int getFeet(){ return foot*4; } } class Bird extends Animal{ public int getFeet(){ return foot*2; } } public class MainClass { public static void main(String[] args) { Animal animal = new Bird(); //此用法之後會詳細介紹,先簡單說明用法 -> 父類別 變數 = new 子類別,此也是多型的典型用法 System.out.println(animal.getFeet()); } } ```

    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