1 / 8

Overriding Method

Overriding Method . เป็นเมธอดที่มีคุณลักษณะของ Polymorphism รูปแบบหนึ่ง โดยใช้ชื่อเมธอดเดียวกัน มากกว่า 1 เมธอด เพื่อทำงานในแบบเดียวกัน คลาสลูกสามารถเขียนทับ เมธอดของคลาสแม่ได้ โดยที่จำนวน อาร์กิวเมนต์ , ชนิดข้อมูลของ อาร์กิวเมนต์ และชนิดของข้อมูลที่คืนค่า

hina
Download Presentation

Overriding Method

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. Overriding Method เป็นเมธอดที่มีคุณลักษณะของ Polymorphism รูปแบบหนึ่ง โดยใช้ชื่อเมธอดเดียวกัน มากกว่า 1 เมธอด เพื่อทำงานในแบบเดียวกัน คลาสลูกสามารถเขียนทับ เมธอดของคลาสแม่ได้ โดยที่จำนวนอาร์กิวเมนต์, ชนิดข้อมูลของอาร์กิวเมนต์ และชนิดของข้อมูลที่คืนค่า จะต้องเหมือนในคลาสแม่

  2. Overloading Method (1) กรณีเลือกประเภทพนักงานเป็น 1 และระดับพนักงานเป็น 9 กรณีเลือกประเภทพนักงานเป็น 2 และเกรดพนักงานเป็น A เป็นเมธอดที่มีคุณลักษณะที่มีได้หลายรูปแบบ (Polymorphism) โดยใช้ชื่อเมธอดเดียวกันมากกว่า 1 เมธอด เพื่อทำงานในแบบเดียวกัน สิ่งที่ต่างกันคือชนิดข้อมูลของผลลัพธ์ หรือมีจำนวนและชนิดข้อมูลของอาร์กิวเมนต์ที่ใช้ในการรับข้อมูลแตกต่างกัน โปรแกรมตรวจสอบเงินเดือน โดยการทำ overload เมธอด setsalary()

  3. Overloading Method (2) class employee { float setsalary(int t) { float salary=0; switch (t) { case 1: salary = 10000; break; case 2: salary = 20000; break; } return salary; } String setsalary(String t) { String salary=""; if (t.equals( "A")) salary = "$750"; if (t.equals( "B")) salary = "$500"; return salary; } }

  4. Overloading Method (3) public class EMPLOYEES { public static void main(String[] args) { String data =JOptionPane.showInputDialog("Enter Employee Type (1 or 2) "); int n =new Integer(data); employee x = new employee(); float s1 = x.setsalary(n); JOptionPane.showMessageDialog(null,"Salary = "+ s1 + " TH Baht"); data =JOptionPane.showInputDialog("Enter Employee Level (A or B) "); String s2 = x.setsalary(data); JOptionPane.showMessageDialog(null,"Salary = "+s2 + " US Dollar"); } }

  5. Overloading Method (4) String calGrade(float t1, float t2, float t3) { grade = "F"; float t = t1+t2+t3; totalscore = t; if (t >= 80) grade = "A"; else if(t >= 70) grade = "B"; else if(t >= 60) grade = "C"; else if(t >= 50) grade = "D"; return grade; }} class grade { float totalscore; String grade; String calGrade(int t) { grade = "U"; totalscore = t; if (t >= 60) grade = "S"; return grade; }

  6. Overloading Method (5) public class GRADES { public static void main(String[] args) { String s="", data1, data2, data3, title=""; grade x = new grade(); data1 = (JOptionPane.showInputDialog(null,"1.วิชาโครงงาน/ ฝึกงาน\n2.วิชาเรียนทั่วไป\nSelect 1 or 2","Score Input", JOptionPane.QUESTION_MESSAGE)); int n = new Integer(data1);

  7. Overloading Method (6) switch (n) { case 1 : title = "วิชาโครงงาน/ฝึกงาน"; data1 = JOptionPane.showInputDialog(null,"Enter Score : ",title, JOptionPane.QUESTION_MESSAGE); s = x.calGrade(new Integer(data1)); break; case 2 : title = "วิชาเรียนทั่วไป"; data1 =JOptionPane.showInputDialog(null,"Enter Midterm Score : ",title, JOptionPane.QUESTION_MESSAGE); data2 =JOptionPane.showInputDialog(null,"Enter Final Score : ",title, JOptionPane.QUESTION_MESSAGE); data3 =JOptionPane.showInputDialog(null,"Enter Quiz Scores : ",title, JOptionPane.QUESTION_MESSAGE); s = x.calGrade(new Float(data1),new Float(data2),new Float(data3)); break; default : System.out.println("Select Type 1 or 2 Only"); } JOptionPane.showMessageDialog(null,"คะแนนรวม = " + x.totalscore + " ได้เกรด " + s,title, JOptionPane.INFORMATION_MESSAGE); } }

  8. Overloading Method (7) switch (n) { case 1 : title = "วิชาโครงงาน/ฝึกงาน"; data1 = JOptionPane.showInputDialog(null,"Enter Score : ",title, JOptionPane.QUESTION_MESSAGE); s = x.calGrade(new Integer(data1)); break; case 2 : title = "วิชาเรียนทั่วไป"; data1 =JOptionPane.showInputDialog(null,"Enter Midterm Score : ",title, JOptionPane.QUESTION_MESSAGE); data2 =JOptionPane.showInputDialog(null,"Enter Final Score : ",title, JOptionPane.QUESTION_MESSAGE); data3 =JOptionPane.showInputDialog(null,"Enter Quiz Scores : ",title, JOptionPane.QUESTION_MESSAGE); s = x.calGrade(new Float(data1),new Float(data2),new Float(data3)); break; default : System.out.println("Select Type 1 or 2 Only"); } JOptionPane.showMessageDialog(null,"คะแนนรวม = " + x.totalscore + " ได้เกรด " + s,title, JOptionPane.INFORMATION_MESSAGE); } }

More Related