1 / 91

การเขียน โปรแกรมเชิงวัตถุ ตอนที่ 2

การเขียน โปรแกรมเชิงวัตถุ ตอนที่ 2. 1. ทำความรู้จักภาษา Java (ต่อ).

hu-stevens
Download Presentation

การเขียน โปรแกรมเชิงวัตถุ ตอนที่ 2

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

  2. 1. ทำความรู้จักภาษา Java (ต่อ) ค.ศ. 1993World Wide Web เริ่มแพร่หลายทีมพัฒนาพบว่าจาวาเป็นภาษาที่มีสถาปัตยกรรมที่เหมาะสมกับการทำงานบน Internet เนื่องจาก-> Small : Faster to download off a page-> Secure : Prevent hacker’s programs that wreak havoc on browser user’s system. -> Portable : It can run on Windows, Macintosh, and other platform without modification.ประกาศตัวครั้งแรกเมื่อเดือนพฤษภาคม ค.ศ. 1995

  3. 1. ทำความรู้จักภาษา Java (ต่อ) ภาษาคอมพิวเตอร์สมัยก่อน-> แปล Source Code เป็นภาษาเครื่อง-> เมื่อมีการเปลี่ยน Chip จะต้อง Compile โปรแกรมใหม่ -> ทำให้ความน่าเชื่อถือของโปรแกรมลดลงภาษาจาวา-> แปล Source Code เป็นภาษาเครื่องที่ไม่ขึ้นกับ Platform -> คำสั่งนั้นจะถูกแปลอีกครั้งในช่วง Run-time โดย Java Virtual Machine (JVM) ทำงานในลักษณะ Interpret -> เป็นภาษาที่เหมาะกับงานอินเตอร์เน็ตและงานโปรแกรม ทั่วไป

  4. Source Code Binary File (Pentium) Compiler (Pentium) Compiler (PowerPC) Binary File (PowerPC) Compiler (SPARC) Binary File (SPARC) แผนภาพแสดงการใช้งานคอมไพเลอร์อื่น ๆ

  5. แผนภาพแสดงการใช้งานจาวาแผนภาพแสดงการใช้งานจาวา Java Bytecode (Platform- Independent) Java Interpreter (Pentium) Java Compiler (Pentium) Java Code .java .class Java Compiler (PowerPC) Java Interpreter (PowerPC) Java Compiler (SPARC) Java Interpreter (SPARC)

  6. 2. ทำความรู้จักกับ Class Class คือการประกาศภาพรวมของกลุ่มวัตถุ (Object) ที่มีคุณลักษณะ (Attribute)ที่เหมือนกัน มีพฤติกรรมที่เหมือนกัน ในการประกาศ Classสิ่งที่ต้องทำการประกาศ ประกอบด้วย Data(ข้อมูล) และ พฤติกรรม (Behavior)หรือ การกระทำ (Operation)สำหรับ Class นั้น

  7. ตัวอย่างที่ 1 Class ของรถ สิ่งที่เราต้องประกาศภาพรวม ได้แก่ Data - ยี่ห้อ - รุ่น - สี - เลขทะเบียน Behavior - เดินหน้า - ถอยหลัง - หยุด

  8. 3. ทำความรู้จักกับ Object (วัตถุ) Objectคือ สิ่งที่เป็นตัวแทนของ Class มีคุณลักษณะที่บ่งบอกถึงความเป็นตัวเอง และสามารถแสดงพฤติกรรมได้

  9. ตัวอย่าง แสดงตัวอย่างของ Object จาก Class ของรถ ตัวอย่างที่ผ่านมา รถยนต์ยี่ห้อ MIZUBISHI รุ่น Lancer สีแดง ทะเบียน กท.7864

  10. ตัวอย่างของ Object   ในโลกแห่งความเป็นจริง  • เช่น “Object : มนุษย์”  ประกอบด้วยคุณสมบัติ (Properties)  สีผม , สีผิว ,ความสูง, สัดส่วน เป็นต้น  และมีพฤติกรรมการตอบสนองต่อเหตุการณ์ (Behavior)  เช่น ตอบสนองต่อการถูกชกต่อย  ตอบสนองต่อเหตุการณ์ถูกไล่ล่า  เป็นต้น

  11. Class & Object • Class = แม่แบบของ Object • Object = วัตถุ ที่สามารถชี้เฉพาะได้ (Identificable) รูปแบบการใช้งาน Object • ประกาศ (Declaration) MyClass1 myObj1; • การให้กำเนิด (Instantiatation) myObj1 = new MyClass1( ); // (Constructor) • การกำหนดค่าเริ่มต้น (Initialization) myObj1 = new MyClass1(200,5); • Class • สร้างใหม่เอง • ใช้ของคนอื่น => ระบบ, ทีมงาน, ตัวเราเอง

  12. สรุปClass และ Object Class คือ การประกาศภาพรวมของกลุ่มวัตถุ (Object) หรือต้นแบบของ Object Object คือ พื้นที่ในหน่วยความจำของเครื่องคอมพิวเตอร์เพื่อใช้ในการประมวลผล อาจกล่าวได้ว่า Object เป็นชื่อเรียกที่ใช้เรียกส่วนที่สร้างจากต้นแบบของ Class

  13. Class และ Object (ต่อ) เมื่อเราออกแบบ Class สิ่งที่เราต้องนึกถึงมีอยู่ 2 อย่าง คือ ใน Class นั้นจะประกอบด้วยData Member อะไรบ้างและประกอบด้วยMethodอะไรบ้าง Data Member สามารถเป็นได้ทั้งตัวแปร (Variable) และ ค่าคงที่ (Constant)

  14. ความแตกตางระหวางคลาสกับออบเจกตความแตกตางระหวางคลาสกับออบเจกต

  15. 4. รูปแบบของโปรแกรมในภาษา Java การโปรแกรมในภาษาจาวาทำได้ใน 2 ลักษณะ1. Applicationเป็นโปรแกรมทั่วไปที่เขียนขึ้นด้วยภาษาจาวา สามารถทำงานได้ด้วยตัวเองโดยไม่ต้องอาศัย Browser2. Applet (จะกล่าวถึงภายหลังเมื่อศึกษาถึงเรื่องนี้)เป็นจาวาโปรแกรมที่ฝังตัวอยู่ในภาษาอื่น ๆ การใช้งาน Applet จะขึ้นอยู่กับความสามารถของ Java Browser

  16. 5. การ Edit-Compile-Run โปรแกรมภาษา Java แบบ Application แผนผังแสดงการทำงานของภาษาจาวา java compiler Java program Java virtual machine program java interpreter

  17. javac FirstPrg.java java FirstPrg FirstPrg.java FirstPrg.class 5. การ Edit-Compile-Run โปรแกรมภาษา Java แบบ Application

  18. Java Compiler and Run java byte code Platform-Independece java source code javac Java Complier .class .java java mobile Unix system Macintosh PDA Notebook java interpreter (run)

  19. class { public static void main (String args[ ] ) { } } 6. รูปแบบของโปรแกรมภาษา Java แบบ Application คำอธิบายโปรแกรม(Comment) Import Statements ชื่อ Class Method Body

  20. ขั้นตอนการสร้างโปรแกรมภาษาจาวาขั้นตอนการสร้างโปรแกรมภาษาจาวา 1.สร้างซอร์ดไฟด์(Source file) - เขียนโปรแกรมโดยใช้คำสั่งภาษาจาวาแล้วบันทึกโดยใช้นามสกุลเป็น.java 2.คอมไพล์(Compile) ซอร์ดไฟล์ - ใช้ compiler javac แปลคำสั่งในซอร์ดไฟล์เป็น ไบต์โค้ด(Bytecodes) ซึ่งจะถูกเก็บไว้ในไบต์โค้ดไฟล์มีนามสกุล .class - ไบต์โค้ด คือ รหัสคำสั่งภาษาเครื่องของ จาวาเวอร์ชวลชีน (java virtual Machine ,JVM) 3.รันโปรแกรม - ใช้คำสั่ง java เพื่อเรียกใช้ JVM ให้รันโปรแกรม

  21. ผังภาพแสดงขั้นตอนการสร้างโปรแกรมภาษาจาวาผังภาพแสดงขั้นตอนการสร้างโปรแกรมภาษาจาวา Mytest.java javac Mytest.java java Mytest Mytest.class

  22. โปรแกรมแรกสำหรับการรู้จักการเขียน Java แบบ Application comment A Simple Example // A first program in Java

  23. โปรแกรมแรกสำหรับการรู้จักการเขียน Java แบบ Application A Simple Example // A first program in Java class Hello { } ตัวคลาสบรรจุอยู่ในวงเล็บปีกกา ชื่อคลาส keyword สำหรับเปิดหัวคลาส

  24. โปรแกรมแรกสำหรับการรู้จักการเขียน Java แบบ Application keyword บอกหัว method พารามิเตอร์สำหรับรับข้อมูลที่ส่งมาให้ method ชื่อ method A Simple Example // A first program in Java class Hello { publicstaticvoidmain(String args[] ){ } }

  25. โปรแกรมแรกสำหรับการรู้จักการเขียน Java แบบ Application ข้อความสั่งสำหรับพิมพ์ข้อความ A Simple Example // A first program in Java class Hello { publicstaticvoidmain(String args[]) { System.out.println("Hello World"); } }

  26. การแสดงผลทางจอภาพ (Console Output) ใช้ Method ที่ชื่อว่า “println”ซึ่งอยู๋ใน “System.out” System.out.println(“ค่าสูงสุดระหว่าง”+num1+”และ”+num2+”คือ”+max(num1,num2)); System.out.println(“เงินเดือนที่ได้เดือนนี้ คือ”+(salary+bonus)+”บาท”); System.out.println(“\n\nวันนี้อากาศดีจัง\n); System.out.println(“สวัสดีค่ะ’คุณสมศรี’ ”);

  27. ผลการ Run โปรแกรม ฟังอธิบายการใช้โปรแกรมสร้าง .java ด้วย Jeliot และ DJ Java Decompiler 3.2

  28. สรุปรูปแบบเบื้องต้น

  29. 1.class 2.main method 3.คำสั่งการทำงานใน main method • กลุ่มลำดับของ values ที่ส่งมายัง methods กลุ่มลำดับของ terms ที่ใช้บอกลักษณะของ method public static void main (String args[]) { คำสั่งการทำงานใน main method; } Modifiers Modifiers Return Type Method Name Parameter class ชื่อclass { • ชื่อของ method • data value ที่คืนให้กับผู้เรียก • เรียกใช้งานได้โดยไม่จำเป็นต้องสร้าง object • กลุ่มลำดับของ instructions ถูกเรียกใช้จากทุก class Method Body เป็นส่วนที่เราใส่ Statements ของการทำงานของโปรแกรม }

  30. 1.class 2.main method 3.คำสั่งการทำงานใน main method • กลุ่มลำดับของ values ที่ส่งมายัง methods กลุ่มลำดับของ terms ที่ใช้บอกลักษณะของ method • data value • ที่คืนให้กับผู้เรียก • ชื่อของ method Modifiers Modifiers Return Type Method Name Parameter class Helloworld { public static void main(String[] args) { System.out.println("Hello World!"); } }

  31. แนะนำชนิดข้อมูลพื้นฐานในจาวาแนะนำชนิดข้อมูลพื้นฐานในจาวา

  32. หัวข้อ • ตัวแปร • ชนิดข้อมูลพื้นฐาน • การคำนวณ • ค่าคงที่

  33. ตัวแปร

  34. ตัวแปร • กล่องสี่เหลี่ยม •  = 5 + 3 • ตัวอักษร • x = 5 + 3 • คำ • count = 5 + 3

  35. การประกาศตัวแปร • รูปแบบ • ชนิดข้อมูล ชื่อตัวแปร; • ตัวอย่าง • int count; • double sum;

  36. การตั้งชื่อ • ต้องเริ่มต้นชื่อด้วย • ตัวอักษรโรมัน (เช่น a, z, A, Z) • เช่น count • เครื่องหมาย _ หรือ $ • เช่น _height หรือ $name • ตัวเลขสามารถอยู่ในชื่อตัวแปรได้ • เช่น car12 หรือ c1a2r • ห้ามขึ้นต้นชื่อด้วยตัวเลข • ห้ามตั้งชื่อด้วยคำสงวน (reserved word)

  37. คำสงวน (Reserved Words) • abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while

  38. กำหนดค่าให้ตัวแปร • รูปแบบ • ตัวแปร =ค่าที่ต้องการกำหนด; • ตัวอย่าง • count = 3;

  39. ชนิดข้อมูลพื้นฐาน

  40. ชนิดข้อมูลพื้นฐาน

  41. แผนผัง

  42. การคำนวณ

  43. การคำนวณ • บวก int a = 1; int b = 2; int sum = a + b; • ลบ • double a = 1.2; • double b = 3.4; • double result = a - b;

  44. การเพิ่มค่า • เครื่องหมาย += int count = 0; count += 1; • เครื่องหมาย ++ (เพิ่มค่าอีกหนึ่ง) int count = 0; count++;

  45. การคำนวณ • คูณ int result; result = 3 * 4; • หาร double a = 14; double result = a / 3;

  46. การหารเอาเศษ • เครื่องหมาย % • ตัวอย่าง int remainder; remainder = 14 % 7; remainder = 15 % 7;

  47. ใช้วงเล็บเพื่อจัดกลุ่มการคำนวณ

  48. เปรียบเทียบผลการคำนวณเปรียบเทียบผลการคำนวณ • int a = 2 + (3 * 4); • int a = (2 + 3) * 4;

  49. การแสดงผลการคำนวณ • คำสั่ง System.out.println() • ตัวอย่าง public class PrintMoney1 { public static void main(String[] arg) { int money = 12; money *= 3; System.out.println(money); } }

  50. การแสดงผลการคำนวณ • ตัวอย่าง public class PrintMoney2 { public static void main(String[] arg) { int money = 12; money *= 3; System.out.println("Money is " + money); } }

More Related