1 / 21

物件導向技術概念

物件導向技術概念. 物件導向基本概念與程式練習. 物件導向技術概念. 一般所講的「物件」,其實就是有形體的東西。. 每個物件都有它特別的外形、尺寸和功能。. 鉛筆的外觀. 物件擁有「 屬性 」和「 功能 」。. 由於觀點的不同,每個人對相同的一個物件之描述也可能不盡相同。. 物件導向技術概念. 在觀念上,物件就如同我們週遭的許多事物一樣,其組成份子基本上都可以分類為狀態 (state) 及功能 (operation) 。舉例來說:

mercia
Télécharger la présentation

物件導向技術概念

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 物件導向技術概念 物件導向基本概念與程式練習

  2. 物件導向技術概念 • 一般所講的「物件」,其實就是有形體的東西。 • 每個物件都有它特別的外形、尺寸和功能。 • 鉛筆的外觀 • 物件擁有「屬性」和「功能」。 • 由於觀點的不同,每個人對相同的一個物件之描述也可能不盡相同。

  3. 物件導向技術概念 • 在觀念上,物件就如同我們週遭的許多事物一樣,其組成份子基本上都可以分類為狀態 (state) 及功能 (operation)。舉例來說: • 一部車子 (物件) 的基本組成,在有形方面:有引擎,輪子,油門,煞車,排檔桿等。在無形方面:有速度,方向,載客數及汽油存量等。在功能上,踩油門可以增加速度,減少汽油存量。踩煞車則可以降低車速。方向盤的轉動則可以變換方向。 • 一個學生的在學資料也可以是一個物件,它的基本組成有學號,姓名,班級及各學期的各科成績。在運作上,它可以計算總分,平均分數及紀錄各學期的成績表現。 • 一個日期也可以被視為是一個物件。以其組成份子來說,一個日期有年、月、日,有西元的表示方式,也有以民國的方式來加以表示。由方法上來說,我們可以設定日期,也可以讀取日期。

  4. 物件導向概念

  5. 類別與物件

  6. 其實你之前就已經使用過類別函數(方法,Method) 數學類別函數: double DNum = Math.pow(2,3) //為2的3次方 double DNum = Math.sqrt(4) //4的開根號 double DNum = (double) Math.random() //傳回0-1之雙倍浮點數的亂數 int Num = (int) (Math.random()*42+1) //傳回1-42之整數的亂數 public class Ex1 { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader keyin; keyin = new java.io.BufferedReader( new java.io.InputStreamReader(System.in)); System.out.print("請輸入年利率: "); float fNum = Float.parseFloat(keyin.readLine()); float fMoney = (float)Math.pow((1.0+fNum),20.0); System.out.println("本利和共: " + fMoney + "百萬元"); } } int a =(int)( Math.random()*42+1); //a=亂數 System.out.println("a = " + a); //輸出亂數

  7. 其實你之前就已經使用過類別函數(方法,Method) import java.util.Arrays; public class CompareArray { public static void main(String[] args) { int[] arr1 = {4, 2, 1, 5, 3}; int[] arr2 = {4, 2, 3, 5, 1}; boolean flag = Arrays.equals(arr1,arr2); if (flag==true) System.out.println("arr1與arr2陣列資料完全相同"); else System.out.println("arr1與arr2陣列資料不完全相同"); } } import java.util.Arrays; public class SortArray { public static void main(String[] args) { int[] arr1 = {4, 2, 1, 5, 3}; Arrays.sort(arr1); for(int i = 0; i < arr1.length; i++) System.out.print(arr1[i] + " "); } }

  8. 類別與物件 • 類別是建構物件的藍圖(Blueprint) • 物件是符合類別定義所產生的實體(instance) 屬性 類別~>模子 方法 Dog blackdog = new Dog(); Dog mixdog = new Dog(); Blackdog.bark(); Blackdog.setColor(“black”); 方法

  9. class Ball { private double radius; // 半徑 private String name; // 名稱 public Ball(double radius, String name) { this.radius = radius; this.name = name; } public double getRadius() { return radius; } public String getName() { return name; } public void setRadius(double radius) { this.radius = radius; } public void setName(String name) { this.name = name; } } public class SimpleClass1 { public static void main(String[] args) { Ball b1 = new Ball(18.4, "red ball"); System.out.println("名稱: " + b1.getName()); System.out.println("半徑: " + b1.getRadius()); } } 在其他的類別內宣告類別方法與類別變數 名稱: red ball 半徑: 18.4

  10. 獨立宣告類別Ball public class Ball { private double radius; // 半徑 private String name; // 名稱 public Ball(double radius, String name) { this.radius = radius; this.name = name; } public double getRadius() { return radius; } public String getName() { return name; } public void setRadius(double radius) { this.radius = radius; } public void setName(String name) { this.name = name; } } public class SimpleClass2 { public static void main(String[] args) { Ball b1 = new Ball(18.4, "red ball"); System.out.println("名稱: " + b1.getName()); System.out.println("半徑: " + b1.getRadius()); } } 名稱: red ball 半徑: 18.4

  11. class Ball { public double radius; // 半徑 public String name; // 名稱 public Ball() { } public double getRadius() { return radius; } public void setName(String name) { this.name = name; } public double getArea() { double Area; Area=Math.pow(this.radius,2)*3.14; return Area; } } public class SimpleClass3 { public static void main(String[] args) { Ball b1 = new Ball(); b1.name="black ball"; b1.radius=10.5; System.out.println("名稱: " + b1.name); System.out.println("面積: " + b1.getArea()); } } 在其他的類別內宣告類別方法與類別變數 名稱: black ball 面積: 346.185

  12. 獨立宣告類別Ball public class Ball { public double radius; // 半徑 public String name; // 名稱 public Ball() { } public double getRadius() { return radius; } public void setName(String name) { this.name = name; } public double getArea() { double Area; Area=Math.pow(this.radius,2)*3.14; return Area; } } public class SimpleClass4 { public static void main(String[] args) { Ball b1 = new Ball(); b1.name="black ball"; b1.radius=10.5; System.out.println("名稱: " + b1.name); System.out.println("面積: " + b1.getArea()); } } 名稱: black ball 面積: 346.185

  13. //範例程式SimpleClass5.java 判斷成績及格與不及格 class grademethod { //定義其他的類別grademethod static int javaScore; //宣告static整數變數 javaScore static void show(){ //定義一個static方法show() if (javaScore >= 60) System.out.println("此科成績"+javaScore+"為及格"); else System.out.println("此科成績"+javaScore+"為不及格"); } } class SimpleClass5 { public static void main(String args[]){ //定義方法main grademethod.javaScore = 80; //存取類別變數 grademethod.show(); //存取類別方法 } } 此科成績80為及格

  14. //範例程式SimpleClass6.java,示範如何在一般的方法內存取另一個方法//範例程式SimpleClass6.java,示範如何在一般的方法內存取另一個方法 class SimpleClass6 { static int addtwo(int x, int y){ //定義一個有二個整數參數,而會傳回整數值的方法addtwo int num = x + y; return num; //回傳num } static int addthree(int c1,int c2,int c3){ //定義一個有三個整數參數,而會傳回整數值的方法addthree int num; num = addtwo(c1 , c2); num = addtwo(num , c3); return num; //回傳num } public static void main(String args[]){ int result; result = addthree(4, 5, 6); System.out.println("4+5+6 = " + result); //呼叫使用類別方法addthree } } 4+5+6 = 15

  15. //範例程式SimpleClass7.java 示範如何在一般的方法設定與使用回傳void資料型態 import java.io.*; class SimpleClass7{ static void avgOftwo(int x, int y){ //定義一個有二個整數參數,而不會傳回值的方法avgOftwo System.out.println((x + y)/2.0f); } public static void main(String args[])throws IOException{ System.out.println("請輸入二個整數。"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num1 = Integer.parseInt(br.readLine()); int num2 = Integer.parseInt(br.readLine()); System.out.print(num1 + "與" + num2 + "的平均="); avgOftwo(num1, num2); //呼叫使用類別方法avgOftwo } } 請輸入二個整數。 30 40 30與40的平均=35.0

  16. //範例程式SimpleClass8.java 示範如何在一般的方法設定與使用回傳int資料型態與float資料型態 import java.io.*; class SimpleClass8{ static int addtwo(int x, int y){ //定義一個有二個整數參數,而會傳回整數值的方法addtwo int num = x + y; return num; //回傳num } static float avgOftwo(int x, int y){ //定義一個有二個整數參數,而會傳回float值的方法avgOftwo float avg; avg = addtwo(x , y)/2.0f; //呼叫使用類別方法addtwo return avg; //回傳avg } public static void main(String args[])throws IOException{ float result; System.out.println("請輸入二個整數。"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num1 = Integer.parseInt(br.readLine()); int num2 = Integer.parseInt(br.readLine()); result = avgOftwo(num1, num2); //呼叫使用類別方法avgOftwo System.out.print(num1 + "與" + num2 + "的平均=" + result); } } 請輸入二個整數。 30 40 30與40的平均=35.0

  17. Application 基本程式架構

  18. 方法 Method

  19. 傳回型態 參數資料型態 參數名稱 方法名稱 定義方法 • [public][static] 傳回型態 方法名稱 (參數0, 參數1, 參數2, …){ //方法區塊; return 傳回值;} • 範例一 有傳回參數的方法 static int total(int n) //定義整數加總方法 { int sum = 0; //宣告區域變數 for (int i = 1; i <= n; i++) //計算總和迴圈 sum += i; return sum; //傳回sum值 } 傳回值

  20. 定義方法 void 宣告 無傳遞參數 • 範例二 沒有傳回參數的方法 static void factorial( ) //定義整數階乘方法 { System.out.println( “Hello”); }

  21. 上機演練 試寫一個方法其功能為列印您的班級座號姓名,同時在主方法( Main) 中呼叫此方法。

More Related