งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

TECH30201 Object-Oriented Programming

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "TECH30201 Object-Oriented Programming"— ใบสำเนางานนำเสนอ:

1 TECH30201 Object-Oriented Programming
Chapter 8 Methods TECH30201 Object-Oriented Programming Reading: Ch.8

2 เมธอด (Method) กลุ่มของคำสั่งที่ถูกรวมเข้าไว้ภายใต้ชื่อหนึ่งๆ สำหรับทำหน้าที่ตามที่ผู้เขียนโปรแกรมกำหนด [modifier] return_type MethodName ([parameter]) { [method_body] return varValue; } public class Date { private int day, month, year; public void setDate(int newDay, int newMonth, int newYear) { day = newDay; month = newMonth; year = newYear; }

3 เมธอด (Method) [modifier] return_type MethodName ([parameter]) {
[method_body] return varValue; } public class Balloon { private int size=5; public static int ballCount; public void showSize() { System.out.println(size); } public void bigger() { size=size+5; public void smaller() { size=size-2;

4 เมธอด (Method) [modifier] return_type MethodName ([parameter]) {
[method_body] return varValue; } public class Dog { private int size; String name; public void bark() { System.out.println("Ruff Ruff!!"); } public void setSize(int s) { if(s > 0) size = s; public int getSize() { return size;

5 Argument และ Parameter
public class Date { private int day, month, year; public void setDate(int newDay, int newMonth, int newYear) { day = newDay; month = newMonth; year = newYear; } parameters 18 10 2556 public class DateTest { public static void main(String[] args) { Date date = new Date(); date.setDate(18, 10, 2556); } arguments

6 Argument และ Parameter
public class Dog { private int size; public void bark() { System.out.println("Ruff Ruff!!"); } public void setSize(int s) { if(s > 0) size = s; public int getSize() { return size; parameters 10 public class DogTest { public static void main(String[] arg) { Dog myDog = new Dog(); myDog.bark(); myDog.setSize(10); System.out.println(myDog.getSize()); } 10 arguments

7 ตัวอย่างที่ 2.1 Before calling add5: x=10 In method add5: x=15
3 4 5 6 7 8 9 10 11 12 public class PrimitivePassingTest { public static void add5(int x) { x = x + 5; System.out.println("In method add5: x = " + x); } public static void main(String[] args) { int x = 10; System.out.println("Before calling add5: x = " + x); add5(x); System.out.println("After calling add5: x = " + x); 10 Before calling add5: x=10 In method add5: x=15 After calling add5: x=10

8 ตัวอย่างที่ 3 1 2 3 4 5 6 7 8 9 10 11 public class TestObject { public static int cube(int x) { return x*x*x; } public static void main(String[] args) { int y; y = cube(2); System.out.println(“2^3 = “ + y); System.out.println(“(2*y)^3 = “ + cube(2*y)); 2 8 2^3 = 8

9 ตัวอย่างที่ 3 1 2 3 4 5 6 7 8 9 10 11 public class TestObject { public static int cube(int x) { return x*x*x; } public static void main(String[] args) { int y; y = cube(2); System.out.println(“2^3 = “ + y); System.out.println(“(2*y)^3 = “ + cube(2*y)); 16 4096 2^3 = 8 (2*y)^3 = 4096

10 ปฏิบัติการ เนื้อหาบทที่ 8 ปฏิบัติการที่ 12 เมธอด (1)
หัวข้อที่ 1 – 4 (หน้า 1 – 5) ปฏิบัติการที่ 12 เมธอด (1) เข้าไปที่ webcs.mwit.ac.th การเรียนการสอน ปฏิบัติการที่ 12

11 ประเภทของเมธอด Instance method คือเมธอดที่กระทำกับตัว object โดยตรง
ไม่มีคีย์เวิร์ด static Static method เมธอดที่มีพฤติกรรมที่ไม่ขึ้นอยู่กับ object ใดๆ สามารถเรียกใช้เมธอดได้โดยที่ไม่ต้องทำการสร้าง object มีคีย์เวิร์ด static Constructor method มีชื่อเหมือนกับชื่อคลาส และไม่มีการกำหนดชนิดของข้อมูลที่ถูกส่งกลับ ถูกเรียกใช้โดยอัตโนมัติเมื่อมีการใช้คำสั่ง new (เมื่อมีการสร้าง object)

12 Instance method ก่อนเรียกใช้งานเมธอด จะต้องทำการสร้าง object ก่อน
public class Dog { private int size; String name; public void bark() { System.out.println("Ruff Ruff!!"); } public void setSize(int s) { if(s > 0) size = s; public int getSize() { return size;

13 Instance method ก่อนเรียกใช้งานเมธอด จะต้องทำการสร้าง object ก่อน
public class Date { private int day, month, year; public void setDate(int newDay, int newMonth, int newYear) { day = newDay; month = newMonth; year = newYear; } public class DateTest { public static void main(String[] args) { Date date = new Date(); date.setDate(18, 10, 2556); }

14 Instance method กับ this
public class Date { private int day, month, year; public void setDate(int newDay, int newMonth, int newYear) { day = newDay; month = newMonth; year = newYear; } public class Date { private int day, month, year; public void setDate(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } ไม่แนะนำ

15 เรียกใช้งานผ่านชื่อคลาสได้โดยตรง โดยไม่ต้องสร้าง object
Static method public class MaxFinder { public static int max(int x, int y) { if (x > y) return x; return y; } public class MaxFinderTest { public static void main(String[] args) { System.out.println(MaxFinder.max(3,5)); } เรียกใช้งานผ่านชื่อคลาสได้โดยตรง โดยไม่ต้องสร้าง object

16 Constructor method public class Date { private int day, month, year;
public Date() { day = 1; month = 1; year = 2013; } public Date(int d, int m, int y) { day = d; month = m; year = y; public class DateTest { public static void main(String[] args) { Date date = new Date(18, 10, 2556); }

17 Constructor method public class Date { private int day, month, year;
public Date() { day = 1; month = 1; year = 2013; } public Date(int d, int m, int y) { day = d; month = m; year = y; public class DateTest { public static void main(String[] args) { Date date = new Date(); }

18 Constructor method ERROR เพราะอะไร? public class Date {
private int day, month, year; public Date(int d, int m, int y) { day = d; month = m; year = y; } public class DateTest { public static void main(String[] args) { Date date = new Date(); } ERROR เพราะอะไร?

19 ปฏิบัติการ เนื้อหาบทที่ 8 ปฏิบัติการที่ 13 เมธอด (2)
หัวข้อที่ 5 (หน้า 5 – 8) ปฏิบัติการที่ 13 เมธอด (2) เข้าไปที่ webcs.mwit.ac.th การเรียนการสอน ปฏิบัติการที่ 13

20 Method Overloading คือ การที่เมธอดมากกว่าหนึ่งตัวที่อยู่ภายในคลาสเดียวกันมีชื่อเหมือนกัน public class Date { private int day, month, year; public Date() { day = 1; month = 1; year = 2013; } public Date(int d, int m, int y) { day = d; month = m; year = y;

21 Method Overloading Method overloading สามารถทำได้ถ้าหากว่ามีอย่างน้อยหนึ่งข้อต่อไปนี้เป็นจริง (1) จำนวน parameter ของเมธอดที่จะ overload นั้นไม่เท่ากัน (2) ชนิดข้อมูลของ parameter นั้นต่างกันอย่างน้อยหนึ่งตัว public class Date { private int day, month, year; public Date() { day = 1; month = 1; year = 2013; } public Date(int d, int m, int y) { day = d; month = m; year = y;

22 Method Overloading Method overloading สามารถทำได้ถ้าหากว่ามีอย่างน้อยหนึ่งข้อต่อไปนี้เป็นจริง (1) จำนวน parameter ของเมธอดที่จะ overload นั้นไม่เท่ากัน (2) ชนิดข้อมูลของ parameter นั้นต่างกันอย่างน้อยหนึ่งตัว public class MethodOverloading { public static int add(int x, int y) { return x+y; } public static int add(int x, int y, int z) { return x+y+z; public static double add(double x, double y) {

23 Method Overloading ERROR เพราะอะไร?
(1) จำนวน parameter ของเมธอดที่จะ overload นั้นไม่เท่ากัน (2) ชนิดข้อมูลของ parameter นั้นต่างกันอย่างน้อยหนึ่งตัว public static int add(int x, int y) { return x+y; } public static double add(int x, int y) { return (double) x+y; ERROR เพราะอะไร?

24 ปฏิบัติการ เนื้อหาบทที่ 8 ปฏิบัติการที่ 14 เมธอด (3)
หัวข้อที่ 6 – 7 (หน้า 8 – 10) ปฏิบัติการที่ 14 เมธอด (3) เข้าไปที่ webcs.mwit.ac.th การเรียนการสอน ปฏิบัติการที่ 14


ดาวน์โหลด ppt TECH30201 Object-Oriented Programming

งานนำเสนอที่คล้ายกัน


Ads by Google