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.
การคืนค่าผ่านตัวแปรในพารามิเตอร์การคืนค่าผ่านตัวแปรในพารามิเตอร์ • การคืนค่าผ่านตัวแปรในพารามิเตอร์สามารถทำได้ 2 แบบคือ • ส่งผ่านเฉพาะค่า (Pass by value) • ส่งผ่านโดยการอ้างอิง (Pass by reference) ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
การคืนค่าผ่านตัวแปรในพารามิเตอร์การคืนค่าผ่านตัวแปรในพารามิเตอร์ • class Class2 { • public static void main(String[] args) { • int num = 60; • int add = addValue(num); • System.out.printf("Before: number is %d%n",num); • System.out.printf("Now: number from addValue() is %d%n",add); • System.out.printf("After: number is %d%n",num); • } • public static int addValue(int number) { • number *=2; • return number; • } • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
การคืนค่าผ่านตัวแปรในพารามิเตอร์การคืนค่าผ่านตัวแปรในพารามิเตอร์ • ผลลัพธ์ที่ได้คือ • Before: number is 60 • Now: number from addValue() is 120 • After: number is 60 ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
การส่งผ่านโดยการอ้างอิง (Pass by reference) • class Num { • int a=1; • public static void addValue(Num num) { • num.a++; • } • } • class Class3 { • public static void main (String[] args) { • Num obj = new Num(); • System.out.printf("Before: obj.a is %d%n",obj.a); • obj.addValue(obj); • System.out.printf("Now : obj.a from addValue() is %d%n",obj.a); • System.out.printf("After : obj.a is %d%n",obj.a); • } • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
การส่งผ่านโดยการอ้างอิง (Pass by reference) • ผลลัพธ์ที่ได้คือ • Before: obj.a is 1 • Now : obj.a from addValue() is 2 • After : obj.a is 2 ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างประกอบการใช้งานเริ่มจาก ไม่ใส่คำใด ๆ เลย • class ClassA { • int one=1; • public int two=2; • protected int three=3; • private int four=4; • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างประกอบการใช้งานเริ่มจาก ไม่ใส่คำใด ๆ เลย • class SubClassA extends ClassA { • ClassA a = new ClassA(); • public void display() { • System.out.printf("%d%n",a.one); • } • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างประกอบการใช้งานเริ่มจาก ไม่ใส่คำใด ๆ เลย • class mainClassA { • public static void main (String[] args) { • ClassA obj = new ClassA(); • System.out.printf("%d%n",obj.one); • SubClassA obj2 = new SubClassA(); • obj2.display(); • } • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างต่อไปใส่คำว่า public ดังนี้ • class ClassB { • int one=1; • public int two=2; • protected int three=3; • private int four=4; • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างต่อไปใส่คำว่า public ดังนี้ • class SubClassB extends ClassB { • ClassB b = new ClassB(); • public void display() { • System.out.printf("%d%n",b.two); • } • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างต่อไปใส่คำว่า public ดังนี้ • class mainClassB { • public static void main (String[] args) { • ClassB obj = new ClassB(); • System.out.printf("%d%n",obj.two); • SubClassB obj2 = new SubClassB(); • obj2.display(); • } • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล
ตัวอย่างต่อไปใส่คำว่า protected ดังนี้ • class ClassC { • int one=1; • public int two=2; • protected int three=3; • private int four=4; • } ผศ. พิเชษฐ์ ศิริรัตนไพศาลกุล