1 / 65

Object Oriented Programming

Basic Programming in Java Primitive Data Types Reference Types and Object Reference Control Structures Week #2 Jarungjit Parnjai. Object Oriented Programming. Java Programming Language. // Sample HelloWorld Application public class HelloWorld { public static void main(String[ ] args) {

yoko-moran
Download Presentation

Object Oriented Programming

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. Basic Programming in JavaPrimitive Data TypesReference Types and Object ReferenceControl StructuresWeek #2Jarungjit Parnjai Object Oriented Programming

  2. Java Programming Language

  3. // Sample HelloWorld Application publicclass HelloWorld { publicstaticvoid main(String[ ] args) { System.out.println("HelloWorld!"); } } First Program in Java • Save as “HelloWorld.java” • Compile • $ javacHelloWorld.java • Run • $ javaHelloWorld • HelloWorld! • $

  4. Java Keywords

  5. Commenting JAVA Code • วิธีการเขียนคำอธิบายโปรแกรม (comment) ในภาษาจาวา ทำได้ 3 วิธี ได้แก่ • // comment • /* comment */ • /** comment * comment **/

  6. Java Data Types

  7. Data Types in Java • ประเภทข้อมูล (Data Type) ในภาษาจาวา • ประเภทข้อมูลพื้นฐาน (Primitive Data Type)คือ ประเภทข้อมูลที่ถูกกำหนดไว้ล่วงหน้า มี 8 ประเภท ได้แก่ • byte, int, short, long, float, double, boolean, char • ประเภทข้อมูลที่ไม่ใช่ข้อมูลพื้นฐาน (Non-Primitive Data Type)คือ ประเภทข้อมูลอื่นๆ นอกเหนือจากประเภทข้อมูลพื้นฐาน • เรียกอีกอย่างหนึ่งได้ว่า ประเภทข้อมูลอ้างอิง (Reference Type)

  8. JAVA Variables • ตัวแปร (Variable) หมายถึง ตำแหน่งที่เก็บข้อมูลในหน่วยความจำ • สามารถประกาศเป็นทั้ง primitive type หรือ reference type โดยที่ค่าที่เก็บในตัวแปร จะสอดคล้องกับประเภทของตัวแปรที่ระบุ • ตั้งชื่อตัวแปร โดยตัวแรกเป็นตัวอักษร (a ถึง z และ A ถึง Z) หรือ สัญลักษณ์ Underscore (_) หรือเครื่องหมาย dollar sign ($) เช่น • userName หรือ _sys_var1 หรือ $change • และชื่อตัวถัดมาจากตัวแปรตัวแรก เป็นตัวอักษร หรือสัญลักษณ์ใดๆ ก็ได้ รวมทั้ง เลข 0 -9 • case-sensitive และ ไม่จำกัดความยาวของตัวแปร

  9. Variable Assignment • ตัวแปรทุกตัวในจาวา จะต้องมีการให้ค่าตัวแปร ก่อนที่จะถูกใช้งาน • ค่าในตัวแปร สามารถเปลี่ยนแปลงได้ โดยการให้ค่าตัวแปร (assignment) ใช้เครื่องหมาย = เช่น • i = 5; หรือ d = 2.656; • Tim = new Person(“Tim”); • หรือ ใช้ prefix/postfix operator เช่น ++ (increment) or -- (decrement)เช่น • i++; หรือi--; • ++i; หรือ --i;

  10. Initializing Variables • ต้องให้ค่าเริ่มต้นกับ Variable ก่อนการใช้งาน Variable แต่ละประเภทมีค่า default ดังนี้ VariableValue byte 0 short 0 int 0 long 0L float 0.0F double 0.0D char ‘\u0000’ boolean false All reference types null

  11. Primitive Data Type • ประเภทข้อมูลพื้นฐาน (Primitive Data Type) • คือ ประเภทข้อมูลที่ถูกกำหนดไว้ล่วงหน้า (pre-defined) • ค่าของตัวแปรที่ประกาศให้เป็นประเภทข้อมูลพื้นฐาน ถูกเก็บอยู่ใน memory พร้อมกับประเภทข้อมูล • ในภาษาจาวา มี 8 ประเภท ได้แก่ • Logical - boolean • Integral - byte, int, short, long • Floating Point - float, double • Textual - char

  12. Logical - boolean • ประเภทข้อมูลพีชคณิต (boolean) • ค่าของตัวแปรมีได้ 2 ค่า ได้แก่ค่า true และ false • boolean flag = true; • boolean truth = false;

  13. Integer Length Name or Type Range 8 bits 16 bits 32 bits 64 bits byte short int long -27to27-1 -215to215-1 -231to231-1 -263to263-1 Integral - byte, short, int and long

  14. Integral - byte, short, int and long • ค่า 3 รูปแบบ • ฐานสิบ (Decimal) เช่น 3 หรือ 2 • ฐานแปด (Octal) - ค่า 0 ตัวแรกหมายถึงค่าเลขในฐานแปด เช่น077 หรือ 025 • ฐานสิบหก (Hexadecimal) - ค่า 0x สองตัวแรกหมายถึงค่าเลขในฐานสิบหก เช่น 0x4C หรือ 0xBA34 • ค่าตั้งต้น (default) เป็น int • กำหนดค่าให้กับตัวแปรประเภทlong โดยใช้ตัวอักษร Lหรือ l เช่น • long x = 3L; หรือ long y = 077L; หรือ long z = 0xBA34l;

  15. Integer Length Name or Type 32 bits 64 bits float double Floating Point - float and double

  16. Floating Point - float and double • สามารถเป็นค่าจุดทศนิยม หรือ ค่าในกรณีใดกรณีหนึ่งดังต่อไปนี้ • E หรือ e • F หรือ f • D หรือ d • ค่าตั้งต้น (default) เป็น double • เช่น • double x =3.1; หรือ double y =6.02E12; • float f =2.718F; หรือ double d =123.4E+306D; • float a = (float) d; หรือ double d = f;

  17. Textual - char • เก็บค่า 16-bit Unicode Character • ประกาศค่าโดยใช้ Single Quote (‘ ’) ดังนี้ • ‘a’ แทนตัวอักษร a • ‘\t’ แทน tab • ‘\u????’ แทนค่า Unicode Character โดยแทน ???? ด้วยเลขฐานสิบหกของ Character นั้น เช่น ‘\u03A6’

  18. char and String • ประเภทข้อมูล char • เป็น Primitive Data Types • ประเภทข้อมูล String • ไม่ใช่ Primitive Data Types แต่เป็น Class (ซึ่งจัดเป็น Reference Type) • ประกาศค่าโดยใช้ Double Quote (“ ”) ดังนี้ • “Good morning!!!”

  19. Java Operators

  20. Operators • = > < ! ~ ? : • == <= >= != && || ++ -- • + - * / & | ^ % << >> >>> • += -= *= /= &= |= ^= %= <<= >>= >>>=

  21. Arithmetic Operators

  22. Relational and Equality Operators < _ > _ =

  23. Logical Operators • Logical Operator • && (logical AND) • || (logical OR) • ! (logical NOT) • ^ (Boolean logical exclusive OR) • Boolean logical AND (&) • Boolean logical inclusive OR (|)

  24. Logical Operators ExpressionResult true && false false true || false true !false true true ^ true false • ตัวอย่างเช่น if ( ( gender == 1 ) && ( age >= 65 ) ) ++seniorFemales;

  25. Cast Operator • cast operator เป็นการแปลง (convert) ค่าจากประเภทข้อมูลปัจจุบันไปเป็น ประเภทข้อมูลอื่น เช่น • int nIntValue = (int) fFloatValue; แปลงค่า float เป็น int • cast operator จะถูกเขียนไว้ในวงเล็บ

  26. 3 xxxx Copy value from x to y yyyy 3 Memory Address Memory Primitive Types • ประเภทข้อมูลพื้นฐาน (Primitive Types/Primitive Data Types) • การประกาศตัวแปร และ การให้ค่าตัวแปร • int x = 3; int y =x;

  27. Person object (name =“Tim”) Tim Reference Type • ประเภทข้อมูลที่ไม่ใช่ข้อมูลพื้นฐาน (Non-Primitive Data Type)เรียกอีกอย่างหนึ่งว่า ประเภทข้อมูลอ้างอิง (Reference Type) • วัตถุ (Object) • คือ สิ่งที่สร้างขึ้น หรือเรียกว่า อินแสตนซ์ (Instance) โดยสร้างขึ้นจากประเภทข้อมูลอ้างอิง (Reference Type) • ตัวแปรอ้างอิง (Reference Variable) • คือ ตัวแปรที่เก็บค่าอ้างอิง(ใน memory) ไปยังวัตถุใดๆ ตัวอย่างเช่น • ตัวแปรที่ถูกให้ค่าอ้างอิงไปยัง วัตถุใดๆ • ตัวแปรที่เก็บค่าอ้างอิงไปยัง String, Array, File Stream

  28. public class Person { privateString myName = null; publicPerson(String Name) { myName = name; } publicvoid setName(String name) { myName = name; } publicString getName() { return myName; } } Person Example 1 publicclass PersonTester { 2 publicstaticvoid main(String[ ] args) { 3 Person Tim = new Person(“Tim”); 4 Person Don = new Person(“Don”); 5 6System.out.println(“Tim’s name:" + Tim.getName()); 7 System.out.println(“Don’s name:" + Don.getName()); 8 9 Don.setName(“Joey”); 10System.out.println(“Tim’s name:" + Tim.getName()); 11 System.out.println(“Don’s name:" + Don.getName()); 12 } 13 } PersonTester.java Person.java

  29. Don Don Tim Person Person Person Joey name=“Joey” name=“Don” name=“Tim” getName() getName() getName() setName() setName() setName() Executing Line 9 After Line 9 After Line 3 After Line 4 Person and PersonTester Object Diagram of “PersonTester.java” and “Person.java” PersonTester main Class Diagram MyDate.java

  30. 1 Person Tim = new Person(“Tim”); 2 Person Don = new Person(“Don”); 3 Don.setName(“Joey”); ประกาศตัวแปรTimและให้ค่า (assign) ให้กับวัตถุที่ถูกสร้างขึ้นใหม่ จากคลาส Person Tim Object Reference • พิจารณาการสร้างวัตถุจากคลาส “Person” Person object (name =“Tim”) After Line 1

  31. 1Person Tim = new Person(“Tim”); 2 Person Don = new Person(“Don”); 3 Don.setName(“Joey”); ประกาศตัวแปรDonและให้ค่า(assign) กับวัตถุที่ถูกสร้างขึ้นใหม่ จากคลาส Person Tim Don Object Reference • พิจารณาการสร้างวัตถุจากคลาส “Person” Person object (name =“Tim”) Person object (name =“Don”) After Line 2

  32. 1Person Tim = new Person(“Tim”); 2 Person Don = new Person(“Don”); 3Don.setName(“Joey”); ใช้ตัวแปรอ้างอิง Don ในการเปลี่ยนแปลงค่าที่ เก็บอยู่ในวัตถุ จากคลาส Person Person object (name=“Joey”) Tim Don Don After Line 3 Person object (name =“Don”) After Line 2 Object Reference • พิจารณาการสร้างวัตถุจากคลาส “Person” Person object (name =“Tim”)

  33. 1 publicclass AccountTester { 2 publicstaticvoid main(String[] args) { 3 BankAccount myAccount = new BankAccount(0); 4 BankAccount yourAccount = new BankAccount(100); 5 6 myAccount.deposit(5000); 7myAccount.withdrawn(2000); 8 System.out.println("my balance:" +myAccount.getBalance()); 9 10 yourAccount.deposit(300); 11yourAccount.withdrawn(200); 12 System.out.println("your balance:” + yourAccount.getBalance()); 13 } 14 } • public class BankAccount { • private int balance; • public BankAccount(int amount) { • balance = amount; • } • public void depositint amount) { • balance = balance + amount; • } • public void withdrawn(int amount) { • balance = balance - amount; • } • public int getBalance() { • return balance; • } • } AccountTester.java BankAccount.java BankAccount example

  34. 5000 myAccount yourAccount myAccount balance=3000 balance=5000 BankAccount BankAccount BankAccount balance=3000 balance=3000 balance = 400 balance = 200 balance = 100 balance = 0 balance = 0 deposit deposit deposit After Line 7 After Line 6 withdraw withdraw withdraw Executing Line 6 After Line 11 After Line 10 getBalance getBalance getBalance After Line 3 After Line 4 AccountTester and BankAccount Object Diagram of “AccountTester.java” and “BankAccount.java” AccountTester main Class Diagram MyDate.java

  35. publicclass AccountTester { publicstaticvoid main(String[] args) { BankAccount sharedAccount = new BankAccount(100); BankAccount myAccount; BankAccount yourAccount; myAccount = sharedAccount; yourAccount = sharedAccount; BankAccount hisAccount = new BankAccount(0); System.out.println("your balance:” + yourAccount.getBalance()); } } AccountTester.java Another AccountTester example

  36. 1 BankAccount sharedAccount = new BankAccount(100); 2 BankAccount myAccount; 3 BankAccount yourAccount; 4 myAccount = sharedAccount; 5 yourAccount = sharedAccount; 6 BankAccount hisAccount = new BankAccount(0); ประกาศตัวแปรsharedAccountและ ให้ค่า (assign) ให้กับวัตถุที่ถูกสร้างขึ้นใหม่ จากคลาส BankAccount sharedAccount Object Assignment • พิจารณาการสร้างวัตถุจากคลาส “BankAccount” BankAccount object (balance = 100)

  37. 1 BankAccount sharedAccount = new BankAccount(100); 2 BankAccount myAccount; 3 BankAccount yourAccount; 4 myAccount = sharedAccount; 5 yourAccount = sharedAccount; 6 BankAccount hisAccount = new BankAccount(0); ประกาศตัวแปรmyAccountและ yourAccount ซึ่งถ้าไม่มีการให้ค่า ตอนประกาศตัวแปร ค่าของตัวแปร จะอ้างอิงค่าเป็น null โดยอัตโนมัติ sharedAccount BankAccount object (balance = 100) myAccount myAccount null null yourAccount After line 3 After line 2 Object Assignment • พิจารณาการสร้างวัตถุจากคลาส “BankAccount”

  38. 1 BankAccount sharedAccount = new BankAccount(100); 2 BankAccount myAccount; 3 BankAccount yourAccount; 4 myAccount = sharedAccount; 5 yourAccount = sharedAccount; 6 BankAccount hisAccount = new BankAccount(0); ให้ค่าอ้างอิงตัวแปรmyAccount จากเดิมอ้างอิงไปที่ null ให้อ้างอิงไป ที่เดียวกับที่ตัวแปรsharedAccount ดังนั้นทั้งตัวแปรmyAccount และ sharedAccount ต่างก็อ้างอิงไปที่ วัตถBankAccount เช่นเดียวกัน sharedAccount sharedAccount BankAccount object (balance = 100) BankAccount object (balance = 100) myAccount myAccount null null yourAccount yourAccount After line 4 After line 3 Object Assignment • พิจารณาการสร้างวัตถุจากคลาส “BankAccount”

  39. 1 BankAccount sharedAccount = new BankAccount(100); 2 BankAccount myAccount; 3 BankAccount yourAccount; 4myAccount = sharedAccount; 5 yourAccount = sharedAccount; 6 BankAccount hisAccount = new BankAccount(0); ให้ค่าอ้างอิงตัวแปรyourAccount จากเดิมอ้างอิงไปที่ null ให้อ้างอิงไป ที่เดียวกับที่ตัวแปรsharedAccount ดังนั้นทั้งตัวแปรyourAccount และ sharedAccount ต่างก็อ้างอิงไปที่ วัตถ BankAccount เช่นเดียวกัน sharedAccount BankAccount object (balance = 100) sharedAccount BankAccount object (balance = 100) myAccount myAccount null yourAccount null yourAccount After line 4 After line 5 Object Assignment • พิจารณาการสร้างวัตถุจากคลาส “BankAccount”

  40. 1 BankAccount sharedAccount = new BankAccount(100); 2 BankAccount myAccount; 3 BankAccount yourAccount; 4myAccount = sharedAccount; 5yourAccount = sharedAccount; 6 BankAccount hisAccount = new BankAccount(0); sharedAccount ประกาศตัวแปรhisAccountและ ให้ค่า (assign) ให้กับวัตถุที่ถูกสร้างขึ้นใหม่ จากคลาส BankAccount BankAccount object (balance = 100) BankAccount object (balance = 0) myAccount null yourAccount Executing line 6 sharedAccount BankAccount object (balance = 100) BankAccount object (balance = 0) sharedAccount BankAccount object (balance = 100) myAccount hisAccount myAccount null yourAccount null yourAccount After line 6 After line 5 Object Assignment • พิจารณาการสร้างวัตถุจากคลาส “BankAccount”

  41. Executing Line 1 After Line 1 stored at aaaa stored at aaaa aaaa aaaa 100 100 myAccount BankAccount (balance=100) BankAccount (balance=100) bbbb bbbb stored at xxxx xxxx xxxx aaaa yyyy yyyy zzzz zzzz Memory Address Memory Address Memory Memory Object Reference in Java • พิจารณาการสร้างวัตถุจากคลาส “BankAccount” ดังต่อไปนี้ 1 BankAccount myAccount = new BankAccount(100); 2 BankAccount yourAccount = new BankAccount(0); 3 BankAccount hisAccount = myAccount;

  42. Executing Line 2 After Line 2 stored at aaaa stored at aaaa aaaa aaaa 100 100 myAccount myAccount BankAccount (balance=100) BankAccount (balance=100) bbbb bbbb 0 0 stored at xxxx stored at xxxx xxxx xxxx aaaa aaaa yourAccount bbbb yyyy yyyy stored at yyyy BankAccount (balance=0) BankAccount (balance=0) zzzz zzzz stored at bbbb stored at bbbb Memory Address Memory Address Memory Memory Object Reference in Java • พิจารณาการสร้างวัตถุจากคลาส “BankAccount” ดังต่อไปนี้ 1 BankAccount myAccount = new BankAccount(100); 2 BankAccount yourAccount = new BankAccount(0); 3 BankAccount hisAccount = myAccount;

  43. Executing Line 3 After Line 3 stored at aaaa stored at aaaa aaaa aaaa 100 100 myAccount myAccount BankAccount (balance=100) BankAccount (balance=100) bbbb bbbb 0 0 stored at xxxx stored at xxxx Copy value from myAccount to hisAccout xxxx xxxx aaaa aaaa yourAccount yourAccount bbbb bbbb yyyy yyyy stored at yyyy stored at yyyy BankAccount (balance=0) BankAccount (balance=0) zzzz zzzz aaaa hisAccount hisAccount stored at zzzz stored at zzzz stored at bbbb stored at bbbb Memory Address Memory Address Memory Memory Object Reference in Java • พิจารณาการสร้างวัตถุจากคลาส “BankAccount” ดังต่อไปนี้ 1 BankAccount myAccount = new BankAccount(100); 2 BankAccount yourAccount = new BankAccount(0); 3 BankAccount hisAccount = myAccount;

  44. 1 publicclass TestMyDate { 2 publicstaticvoid main(String[] args) { 3 MyDate myBirthDay = 4 new MyDate(20, 6, 1990); 5 System.out.println("My birth day is on "+ myBirthDay.toString()); 6 7 MyDate myGoodDay = new MyDate(19, 6, 2001); 8 System.out.println("My good day is on "+ myGoodDay.toString()); 9 10 myGoodDay.setDate(20, 6, 2001); 11 System.out.println("My good day is on "+ myGoodDay.toString()); 12 } 13 } publicclass MyDate { privateint day=0; privateint month=0; privateint year=0; public MyDate(int d, int m, int y) { this.setDate(d,m,y); } publicvoid setDate(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } public String toString() { return (day+"-"+month+"-"+year); } } TestMyDate.java MyDate.java MyDate example

  45. public class MyDate { privateint day, month, year; public MyDate(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } : } The “this” Keyword • การใช้งานคีย์เวิร์ด “this” • ใช้อ้างอิงตัวแปรภายในคลาส (class Attribute) หรือเมทธอดที่ถูกอ้างอิงจากภายในคลาสเดียวกัน (class Method) • ใช้ในการส่งค่าอ้างอิงของวัตถุ เป็น parameter ไปยังเมทธอดอื่น หรือ คอนสตรัคเตอร์อื่น

  46. 1 publicclass TestMyDate { 2 publicstaticvoid main(String[] args) { 3 MyDate myGoodDay = new MyDate(19, 6, 2001); 4 5 myGoodDay.setDate(19, 6, 2001); 6 System.out.println(”My good day is on"+ myGoodDate. toString()); 7 8 myGoodDay.day = 20; 9 myGoodDay.month = 6 ; 10 myGoodDay.year = 2001 11 System.out.println(”My good day is on"+ myGoodDate. toString()); 12 } 13 } publicclass MyDate { publicint day=0; publicint month=0; publicint year=0; public MyDate(int d, int m, int y) { this.setDate(d,m,y); } publicvoid setDate(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } public String toString() { return (day+"-"+month+"-"+year); } } TestMyDate.java MyDate.java Another version of MyDate

  47. MyDate MyDate date date month month year year toString toString setDate setDate Access Control of MyDate Object Diagram (A) of “MyDate.java” Object Diagram (B) of “MyDate.java” Class Diagram MyDate.java

  48. Access Control • การควบคุมการเข้าถึงข้อมูล (Access Control) • คือ วิธีการเชิงวัตถุ ที่ภาษาจาวามีไว้เพื่อ ใช้เขียนโปรแกรมสนับสนุนคุณสมบัติ Encapsulation และ Information Hiding • การระบุค่า Access Control Specifier เป็นการควบคุมการเข้าถึงข้อมูล โดยจะใช้ระบุก่อนหน้า ตัวแปร ค่าคงที่เมทธอด และคลาส • ค่า Access Control Specifier ได้แก่ • public- เปิดให้เข้าถึงข้อมูลได้จากทุกๆที่ • private- เปิดให้เข้าถึงข้อมูลได้ภายในขอบเขตที่กำหนด • protected- เปิดให้เข้าถึงข้อมูลได้จากการสืบทอดคุณสมบัติ

  49. public ประกาศการเข้าถึงข้อมูล ของคอนสตรัคเตอร์ของ คลาสเป็นpublicเพื่อให้ ผู้ขอใช้บริการจากวัตถุที่ ถูกสร้างขึ้นจากวัตถุนี้ สามารถสร้างวัตถุนี้ได้ ประกาศการเข้าถึงข้อมูล ของตัวแปรภายในคลาส เป็นprivateเพื่อให้สามารถ เข้าถึงได้จากภายในคลาส ประกาศการเข้าถึงข้อมูล ของเมทธอดเป็นpublic เพื่อให้เมทธอดของคลาส สามารถให้บริการแก่ผู้ขอ ใช้บริการนอกขอบเขตของ คลาส Note: กรณีที่ประกาศการเข้าถึง ข้อมูลของคลาสเป็นpublicคลาสนั้น จะต้องถูกบันทึกในไฟล์ที่มือชื่อเดียวกับ คลาส “BankAccout.java”แยกจาก ไฟล์ที่ระบุผู้สร้างวัตถุจากคลาส หรือ ขอรับบริการจากคลาสนี้ An Example of Access Control • class BankAccount { • private double balance; • public BankAccount(double initAmount) { • balance = initAmount; • } • public void deposit(double amount) { • balance = balance + amount; • } • public void withdrawn(double amount) { • balance = balance - amount; • } • public int getBalance() { • return balance; • } • }

More Related