嵌入式系統及實驗 Embedded System and Experiment 詹曉龍 長庚大學電機系
Java 的類別與物件 : 宣告類別 建構子 public class Customer { private String name; private String address; // Customer 類別宣告 // 成員資料 public int age; // 建構子 : 使用參數設定成員資料初始值 public Customer(String name, String a) { this.name = name; // 設定姓名 address = a; // 設定地址 2
Java 的類別與物件 // 方法 : 傳回姓名 public String getname() { return name; // 方法 : 傳回地址 public String getaddress() { return address; // 方法 : 傳回郵寄資料 public String getlabeldata() { String str = ""; str += "\n 姓名 : " + name; str += "\n 地址 : " + address; str += "\n 年齡 : " + age; str += "\n-------------------"; return str; 3
Java 的類別與物件 : 宣告物件變數 建立物件實例存取物件變數與方法 public class test1 { public static void main(string[ ] args) { String str = ""; Customer joe; // 宣告物件變數 joe = new Customer(" 陳會安 "," 新北巿 "); // 建立物件 joe.age = 40; // 指定 public 成員變數 str += joe.getlabeldata(); // 呼叫方法 str += "\nname: "+joe.getname(); str += "\naddress: "+joe.getaddress(); str += "\nage: "+joe.age; System.out.println(str); 4
Java 的類別與物件 執行結果 5
過載方法與串流呼叫方法 public class test2 { public static void main(string[ ] args) { String str = ""; Counter c = new Counter(); // 建立物件 // 呼叫過載方法 str += "a(1)+b(2) = " + c.sum(1, 2); str += "\na(1)+b(2)+c(3) = " + c.sum(1, 2, 3); // 串流呼叫方法 int count = c.count(2).count(3.0).getcount(); str += "\n 目前計數 : " + count; System.out.println(str); 6
過載方法與串流呼叫方法 class Counter { private int count; // 建構子 public Counter() { count = 0; // 過載方法 : 不同型態 public Counter count(double num) { count += num; return this; public Counter count(int num) { count += num; return this; 7
過載方法與串流呼叫方法 // 取得計數值 public int getcount() { return count; // 過載方法 : 不同參數個數 public int sum(int a, int b) { return a + b; public int sum(int a, int b, int c) { return a + b + c; 8
過載方法與串流呼叫方法 執行結果 9
靜態成員與靜態初始程式區塊 public class test3 { public static void main(string[ ] args) { String str = ""; // 宣告 Student 類別型態的變數且建立物件 Student std1 = new Student(1, 68.0); Student std2 = new Student(2, 77.0); str = " 老師編號 (Student): " + Student.teacherNo + "\n"; str += std1.getstudentinfo(); // 呼叫物件的實例方法 str += std2.getstudentinfo(); // 顯示學生人數 str += " 顯示類別變數值 ========="; str += "\n 學生人數 (Student): " + Student.getStudentCount(); str += "\n 更新老師編號為 501======"; Student.teacherNo = 501; // 更新老師編號 // 顯示老師編號 str += "\n 老師編號 (Student): " + Student.teacherNo; System.out.println(str); 10
靜態成員與靜態初始程式區塊 // Student 類別宣告 class Student { // 類別變數 : 老師編號 public static int teacherno = 500; private static int count; // 類別變數 : 學生人數 private int id; // 成員資料學號 private double grade; // 成員資料成績 // 建構子 : 使用參數設定初始值 public Student(int id, double grade) { this.id = id; this.grade = grade; // 設定成績 count++; // 學生人數加一 11
靜態成員與靜態初始程式區塊 // 類別方法 : 傳回學生的個數 public static int getstudentcount() { return count; // 成員方法 : 取得學生資料 public String getstudentinfo() { // 取得學生基本和成績資料 String str = "[ 學生資料 ]==============\n"; str += " 學生學號 : " + id + "\n"; str += " 學生成績 : " + grade + "\n"; return str; 12
靜態成員與靜態初始程式區塊 執行結果 13
Exercise 1 Based on Student Class 設計 2 個過載的 double getmaxgrade() 方法, 分別傳入 2 個或 3 個 double 參數 (using overloading), 傳回值是參數中的最大值,and update to the grade variable Add a new static variable, the name of Chairman. 14
類別的繼承 public class test4 { public static void main(string[ ] args) { String str = ""; // 宣告 Car 類別型態的變數, 並且建立物件 Car nissan = new Car("Sentra180", 49.9,4); Car honda = new Car("Civil", 55, 2); str += nissan.getcarinfo(); // 取得轎車資料 str += honda.getcarinfo(); System.out.println(str); 15
類別的繼承 class Vehicle { private String name; // Vehicle 類別宣告 // 型號名稱 private double price; // 價格 // 建構子 public Vehicle(String name, double price) { this.name = name; this.price = price; // 方法 : 取得交通工具的資料 public String getvehicleinfo() { return " 型號 : " + name + "\n 價格 : " + price + "\n"; 16
類別的繼承 class Car extends Vehicle { // Car 類別宣告 private int doors; // 建構子 // 幾門車 public Car(String name, double price, int doors) { super(name, price); // 呼叫父類別的建構子 this.doors = doors; // 方法 : 取得轎車資料 public String getcarinfo() { return "==== 轎車資料 ====\n" + getvehicleinfo() + // 呼叫父類別的方法 " 車有幾門 : " + doors + "\n"; 17
類別的繼承 執行結果 18
抽象類別與方法 public class test6 { public static void main(string[ ] args) { String str = ""; // 宣告 Circle 類別型態的變數, 並且建立物件 Circle c = new Circle(5.0, 10.0, 4.0); // 顯示圓形 c 的資料 str += " 圓形 c 的資料 ====="; str += "\nx 座標 : " + c.x; str += "\ny 座標 : " + c.y; str += "\n 半徑 : " + c.r; // 呼叫物件的實例方法 str += "\n 圓面積 : " + c.area(); System.out.println(str); 19
抽象類別與方法 abstract class Shape { // Shape 抽象類別宣告 public double x; // X 座標 public double y; // y 座標 abstract double area(); // 抽象方法 : 計算面積 class Circle extends Shape { // Circle 類別宣告 public double r; // 半徑 public Circle(double x, double y, double r) { this.x = x; this.y = y; this.r = r; public double area() {return 3.1416*r*r; // 成員方法 : 實作抽象方法 area() 20
抽象類別與方法 執行結果 21
Java 的介面 public class test7 { public static void main(string[ ] args) { String str = ""; // 宣告 Circle 類別型態的變數, 並且建立物件 Circle1 c = new Circle1(5.0, 5.0, 8.0); // 呼叫物件的介面方法 area() str += " 圓面積 : " + c.area() + "\n"; // 顯示介面的常數值 str += "PI 常數 : " + AreaInterface.PI; System.out.println(str); 22
Java 的介面 abstract class Shape1 { // Shape 抽象類別宣告 // 成員資料 public double x; // X 座標 public double y; // Y 座標 interface AreaInterface { // AreaInterface 介面宣告 // 常數的宣告 final double PI = 3.1415926; // 介面方法 : 計算面積 double area(); 23
Java 的介面 // Circle 類別宣告 class Circle1 extends Shape1 implements AreaInterface { // 成員資料 private double r; // 建構子 // 半徑 public Circle1(double x, double y, double r) { this.x = x; this.y = y; this.r = r; // 實作 AreaInterface 介面的方法 area() public double area() { return PI*r*r; 24
Java 的介面 執行結果 25
Exercise 2 Based on test6 Declare a class named Shape3D with its center value (x,y,z), and two methods named SurfaceArea and Volume Declare two new Classes named Ball and Cylinder inherit from Shape3D to calculate their SurfaceArea and Volume, respectively. Write down your own main program to show the results 26
執行緒 繼承 Thread 類別來建立執行緒 public class test8 { public static void main(string[ ] args) { System.out.println(" 執行緒 : " + Thread.currentThread()); // 建立執行緒物件 SumThread st = new SumThread(150, " 執行緒 A"); st.start(); // 啟動執行緒 // 建立匿名內層類別來啟動執行緒 new Thread(" 執行緒 B") { int length = 150; 27
執行緒 繼承 Thread 類別來建立執行緒 // 執行執行緒 public void run() { long temp = 0; for (int i = 1; i <= length; i++) { try { // 暫停一段時間 Thread.sleep((int)(Math.random()*10)); catch(interruptedexception e){ temp += i; System.out.println(Thread.currentThread() + " 總和 = " + temp);.start(); // 啟動執行緒 28
執行緒 繼承 Thread 類別來建立執行緒 class SumThread extends Thread { private long length; // 建構子 public SumThread(long length, String name) { super(name); this.length = length; 29
執行緒 繼承 Thread 類別來建立執行緒 // 執行執行緒 public void run() { long temp = 0; for (int i = 1; i <= length; i++) { try { // 暫停一段時間 Thread.sleep((int)(Math.random()*10)); catch(interruptedexception e){ temp += i; System.out.println(Thread.currentThread() + " 總和 = " + temp); 30
執行緒 繼承 Thread 類別來建立執行緒 執行結果 31