都是一個或多個 Class 組成 • How to use classes ? • Scanner keyboard; • How to create objects from classes ? • Keyboard = new String(); • How to invoke their methods ? • keyboard.nextInt(); 3
or declared by a variable of the class type: • Date object1; • Use new operator is to create the object and associate it with its variable name: • object1 = new Date(); • These can be combined as follows: • Date object1 = new Date(); • Date object2 = new Date(); String month; int day; int year; writeOutput() month = May; day = 6; year = 1994; month = October; day = 25; year = 1990; object1 object2 new new Date 6
object1.month; • object2.day; • 呼叫 methods • object1.writeOutput(); String month; int day; int year; writeOutput(); month = May; day = 6; year = 1994; month = October; day = 25; year = 1990; object1 object2 new new Demo 9
如果兩個 methods 擁有相同名字的區域變數? • They are still two entirely different variables public void method_a() { int a; … } public void method_b() { int a; … } 兩個 a 是不同的 13 Local Instance Class 三種 variables 各自不同
class 的變數名稱一 樣 public class Date { String month; int day; int year; public void setDate(String month, int day, int year) { this.month = month; this.day = day; this.year = year; } } Q: 等號哪一側的 month. day. year 才是 Date 的 variables ? 15
這個動作就叫 Override 覆寫 45 String name; int hireDate; public int getSalary() { definition① } int hours; int Salaryrate; public int getSalary() { definition② } Employee HourlyEmployee 繼承 Inherit
50 public derivedClass(int p1, int p2, double p3) { super(p1, p2); instanceVariable = p3; } Constructor method(); Method1(); Constructor method(); super() Base Class Derived Class
53 A class obj1 B class obj2 C class obj3 A obj1 = new A( ); B obj2 = new B( ); C obj3 = new C( ); obj1 = obj2; (1) obj1 = obj3; (2) obj2 = obj3; (3) obj3 = obj1; (4) 哪個錯?