Skip to content

Instantly share code, notes, and snippets.

@lyhcode
Created December 12, 2011 07:10
Show Gist options
  • Save lyhcode/1465573 to your computer and use it in GitHub Desktop.
Save lyhcode/1465573 to your computer and use it in GitHub Desktop.

物件導向程式設計

抽象類別,父類別,子類別,blah...

動手練習

全職與兼職時薪計算

定義抽象類別 Job, 提供 getSalary() 抽象方法。 實作 Fulltime 及 Parttime 類別, 繼承 Job 類別。

類別示意圖:

.. graphviz::

        digraph G {
                node [ shape = "box" ]
                edge [ arrowtail = "empty", dir = "back" ]

                Job -> Fulltime
                Job -> Parttime
        }

Fulltime 類別的建構函式可接受 monthSalary 參數, 而 Parttime 類別則接受 hourSalary 及 workHours 兩個參數。 請實作 getSalary() 方法, 並利用繼承減少重複的程式碼。

測試案例:

Job job1 = new Fulltime(28000);
System.out.println(job1.getSalary()); // print 28000

Job job2 = new Parttime(120, 35);
System.out.println(job2.getSalary()); // print 4200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment