ba

Friday, July 16, 2010

การใช้งาน เมธอด

การใช้งาน เมธอด
ในการสร้างเมธอด เมธอดจะต้องอยู่ในคลาสเสมอ มีรูปแบบดังนี้[modifier] return_type method_name([parameter]){//คำสั่ง...........}
modifier ชื่อการเข้าถึงเมธอดนี้ เช่น public,private,protectedreturn_type ถ้าเมธอดส่งค่ากลับไปที่จุดเรียกเมธอดก็ต้องใส่ชนิดของค่าที่จะส่งกลับด้วยแต่ถ้าเมธอดนี้ ไม่ส่งค่ากลับก็ใส่ void แต่ถ้ามีการส่งค่ากลับจะต้องมี keywork คำว่า return ด้วยmethod_name เป็นชื่อเมธอดเช่น Add,GetValueparameter พารามิเตอร์นี้จะมีหรือไม่มีก็ได้ถ้าต้องการรับค่าเข้ามาก็ใส่พารามิเตอร์นี้ด้วยแต่ถ้าไม่ต้องการ รับค่าเข้ามาก็ไม่ต้องใส่ตัวอย่างการสร้างเมธอด
public void SetValue(int value){}ในเมธอดนี้มีชื่อว่า SetValue โดยรับค่า value เข้ามาและในเมธอดนี้ไม่มีการส่งค่ากลับ
ตัวอย่างการใช้งานเมธอด เปิด visual studio 2008 หรือ visual studio 2005 ขึ้นมาไปที่ File->new->Project->Visual c#->Console Application
ตัวอย่าง
using System;public class TestMethod{ public double AddValue(double x,double y) //สร้างเมธอด AddValue รับค่าเข้ามา 2 ตัวและส่งค่ากับเป็นชนิด double { return x + y; } public double SubstractValue(double x, double y) //สร้างเมธอด SubstractValue รับค่าเข้ามา 2 ตัวและส่งค่ากับเป็นชนิด double { return x - y; } public static void Main() { TestMethod a = new TestMethod(); Console.WriteLine("Call AddValue(2,4) : "+a.AddValue(2,4));// เรียกใช้เมธอด a.AddValue(2,4); เพื่อส่งตัวเลข 2 ไปที่ ตัวแปร x และ 4 ไปที่ตัวแปร y Console.WriteLine("Call SubstractValue(10,5) : " + a.SubstractValue(10, 5)); // เรียกใช้เมธอด a.SubstractValue(10,5); เพื่อส่งตัวเลข 10 ไปที่ ตัวแปร x และ 5 ไปที่ตัวแปร y Console.ReadLine(); }}
ผลลัพธ์จะได้Call AddValue(2,4) : 6Call SubstractValue(10,5) : 5
ตัวอย่างการสร้างเมธอดเพื่อแสดง Date
using System;public class TestMethod{ public string ShowDate() { return DateTime.Now.ToShortDateString().ToString(); //แสดงวันที่ปัจจุบัน } public static void Main() { TestMethod a = new TestMethod(); Console.WriteLine("Date : " + a.ShowDate()); Console.ReadLine(); }}
ผลลัพธ์จะแสดงวันที่ปัจจุบันออกมาคือDate : 22/12/2550 (เนื่องจากผมเขียนในวันที่นี้ก็จะแสดงวันที่นี้ออกมานะครับ)
ตัวอย่างการสร้างเมธอดแบบสามารถรับค่าเข้ามาแล้วกำหนดค่าและส่งกลับ
using System;public class Employee{ private string name; private int age; private string id; public Employee(string name, int age)//เป็น Constructor รับค่าเข้ามา 2 ค่า { this.id = System.Guid.NewGuid().ToString();//แสดง Guid this.name = name; this.age = age; } public void SetName(string name)//กำหนด name ใหม่ { this.name = name; } public string GetName()//ส่งค่า name กลับไปที่จุดเรียกใช้เมธอด { return name; } public void SetAge(int age)//กำหนด age ใหม่ { this.age = age; } public int GetAge()//ส่งค่า age กลับไปที่จุดเรียกใช้เมธอด { return age; } public string Id()//ส่ง id กลับไปที่จุดเรียกใช้เมธอด { return id; } }public class TestMethod{ public static void Main() { Employee emp1 = new Employee("John", 40);//ส่ง John และ 40 ไปที่ Constructor Console.WriteLine("#Id {0} ", emp1.Id()); Console.WriteLine("Name {0} ", emp1.GetName()); Console.WriteLine("Age {0} ", emp1.GetAge()); Console.WriteLine("Change name and age"); emp1.SetName("Adam");//กำหนดค่า name ใหม่ emp1.SetAge(42);//กำหนดค่า age ใหม่ Console.WriteLine("Name {0} ", emp1.GetName()); Console.WriteLine("Age {0} ", emp1.GetAge()); Console.WriteLine("\n\n\n"); Employee emp2 = new Employee("Jane", 30);//ส่ง Jane และ 30 ไปที่ Constructor Console.WriteLine("#Id {0} ", emp2.Id()); Console.WriteLine("Name {0} ", emp2.GetName()); Console.WriteLine("Age {0} ", emp2.GetAge()); Console.ReadLine(); }}ผลลัพธ์แสดงดังรูป
ตัวอย่าง
using System;public class Point{ private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int GetXValue() { return x; } public int GetYValue() { return y; }}public class TestMethod{ static void Main() { Point p = new Point(3, 4); Console.WriteLine("x = {0}", p.GetXValue()); Console.WriteLine("y = {0}", p.GetYValue()); Console.ReadLine(); }}
ผลลัพธ์x = 3y = 4
การใช้งาน Nested MethodNested method เป็นเมธอดที่เรียกเมธอดอีกที
ตัวอย่าง
using System;public class TestMethod{ private int n = 1; public void MethodOne() { Console.WriteLine("This is methodone"); Console.WriteLine("Value is " + n); Console.WriteLine("\n"); MethodTwo(); // เรียกใช้เมธอด MethodTwo() } public void MethodTwo() { n++; Console.WriteLine("This is methodtwo"); Console.WriteLine("Value is " + n); Console.WriteLine("\n"); MethodThree();//เรียกใช้ MethodThree() } public void MethodThree() { n++; Console.WriteLine("This is methodthree"); Console.WriteLine("Value is " + n); } public static void Main() { TestMethod a = new TestMethod(); a.MethodOne(); Console.ReadLine(); }}
ผลลัพธ์แสดงดังรูป
การใช้งาน Method recursionmethod recursion เป็นเมธอดที่เรียกตัวเองเช่นต้องการหา factorial ของ 5! ซึ่งมีรูปแบบทางคณิตศาสตร์ดังนี้5! = 5x4x3x2x1 สังเกตุว่าในกระบวนการนี้จะเป็นการทำซ้ำ คือ n(n-1)(n-2)(n-3),...(1) หมายความว่าตัวมันเองจะคูณกับตัวมันเองที่ลดค่าที่ละหนึ่งไปเรี่อยๆถึง 1ซึ่งแนวคิดนี้เราสามารถนำมาใช้กับ method recursion ได้
ตัวอย่าง การเขียนโปรแกรม recursion
using System;public class TestMethodReCursion{ public int Fac(int n) { int result; if (n == 1 n == 0) //ถ้า n มีค่าเท่ากับ 1 หรือ 0 ก็ใช้แสดง 1 เนื่องจากว่า 1! หรือ 0! = 0 { result = 1; } else { result = n * Fac(n - 1); //ถ้า n มีค่าอื่นๆที่ไม่ใช้ 1 ก็ให้ นำ n คูณกับ ค่าของ n-1 ไปเรื่อยๆ } return result; } public static void Main() { TestMethodReCursion a = new TestMethodReCursion(); Console.WriteLine("5! = {0}",a.Fac(5)); Console.WriteLine("3! = {0}", a.Fac(3)); Console.WriteLine("10! = {0}", a.Fac(10)); Console.ReadLine(); }}
ผลลัพธ์จะได้ดังรูป
การส่งค่า Parameter ต่างๆ
ปกติแล้วการส่งค่า parameter ต่างๆของเมธอดจะเป็น Passed by Value เสมอ แต่ถ้าเราต้องการให้ส่งค่าแบบอื่นๆก็ใส่ keyword ลงไปเช่น ref เป็นการส่งค่าแบบ reference type จะต้องกำหนดค่าเริ่มต้นให้กับตัวแปรที่ต้องการส่งด้วยparams เป็นการส่งค่า แบบ array หรือสามารถส่งได้หลายๆค่าout เป็นการส่งค่าแบบ out จะคล้ายๆกับ ref แต่ไม่ต้องกำหนดค่าเริ่มต้นให้กับตัวแปรที่ต้องการส่ง
ตัวอย่างการส่งค่าแบบ value
using System;public class TestValueParameter{ public int AddMethod(int i,int j) { return i + j; } public static void Main() { TestValueParameter a = new TestValueParameter(); Console.WriteLine(a.AddMethod(3, 4)); Console.ReadLine(); }}ผลลัพธ์จะได้ 7
ตัวอย่างการส่งค่าแบบ Reference
using System;public class TestRefParameter{ public void TestRef(int i,ref int j) { i = 88888; j = 99999; } public static void Main() { int i = 1; int j = 2; TestRefParameter a = new TestRefParameter(); Console.WriteLine("Before call method TestRef i ={0} , j = {1}", i, j); a.TestRef(i, ref j);//เรียกใช้งาน เมธอด TestRef เพื่อส่งค่า i ที่เป็นค่า value และ ค่า j ที่เป็น reference ไป Console.WriteLine("\n"); Console.WriteLine("After call method TestRef i = {0} , j ={1}", i, j); Console.ReadLine(); }}
ผลลัพธ์แสดงดังรูป
คำอธิบายในตัวอย่างนี้ ตัวแปร i จะเป็นการส่งค่าแบบ ปกติคือ แบบ Value ดังนั้นค่าของตัวแปร i จะไม่เปลื่ยนแปลงหลังจากที่เรียกใช้เมธอดTestRef แต่ ตัวแปร j ซึ่งเป็นการส่งค่าแบบ reference ( สังเกต มี ref นำหน้า ) หลังจากที่เรียกใช้เมธอด TestRef ค่าของตัวแปร j จะเปลื่ยนไปเนื่องจากว่าเป็นการส่งแบบ reference
การส่งค่าแบบ out
ในการส่งค่าแบบนี้ จะต้องมี keyword out นำหน้าตัวแปรที่จะส่งด้วย และไม่ต้องกำหนดค่าเริ่มต้นของตัวแปรที่จะส่ง
ตัวอย่าง
using System;public class TestOutParameter{ public void OutMethod(out int value) { value = 20; } public static void Main() { int value; //ไม่ต้องกำหนดค่าเริ่มต้นในกรณีที่ต้องการส่งแบบ out TestOutParameter a = new TestOutParameter(); a.OutMethod(out value); Console.WriteLine("After call OutMethod : "+value); Console.ReadLine(); }}ผลลัพธ์จะได้After call OutMethod : 20 คำอธิบายในตัวอย่างนี้เป็นการส่งแบบ out ตัวแปร value ที่อยู่ในเมธอด Main()ไม่ต้องกำหนดค่าเริ่มต้นใดๆ ในเมธอด OutMethod จะกำหนดค่าให้กับ ตัวแปล value เอง
การส่งค่าแบบ paramsในการส่งค่าแบบนี้ จะใช้ keyword params นำหน้าตัวแปรของ parameterที่รับค่าเข้ามา
ตัวอย่าง
using System;public class TestParamsMethod{ public void TestParams(params int[] a)//กำหนดเมธอดรับค่าแบบ params { Console.WriteLine("total number is {0}", a.Length); for (int i = 0; i < a.Length; i++) Console.WriteLine("member is {0}",a[i]); } public static void Main() { TestParamsMethod test = new TestParamsMethod(); int[] a = { 1, 3, 5, 7, 9, 8, 5, 6, 99 }; int[] b = { 1, 2 }; int c = 100; test.TestParams(a); Console.WriteLine("\n"); test.TestParams(b); Console.WriteLine("\n"); test.TestParams(c); Console.ReadLine(); }}
ผลลัพธ์จะได้ดังรูป
คำอธิบาย ในตัวอย่างนี้ มีเมธอด TestParams ซึ่งกำหนดให้ parameter รับค่าแบบ params เข้ามาแล้วแสดงผล ในเมธอด Main()จะมีตัวแปร array 2 ตัวคือ a และ b ซึ่งแต่ละตัวจะมีสามาชิกไม่เท่ากัน และ ตัวแปร c ซึ่งมีสมาชิกหนึ่งตัว แล้วมีการเรียกใช้งาน เมธอด TestParams สังเกตุว่าตอนเรียกใช้งานเมธอดที่รับค่าแบบ params เราไม่ต้องกำหนด keyword params ในตอนที่เรียกใช้ เราสามารถส่งค่าแบบอาร์เรย์ที่มีสมาชิกกี่ตัวก็ได้
การใช้งานเมธอด Overloadเมธอด overload คือเมธอดที่มีชื่อเหมือนกันแต่มีการรับค่า parameter ต่างกัน โดยค่า parameter ต่างกันได่แก่ ชื่อของ parameter,ชนิดของ parameter และ จำนวน parameter ที่รับเข้ามาแต่สำหรับ return_type ของเมธอดถ้าต่างกันไม่ถือว่าเป็น overload เช่นpublic void Test(){}public void Test(int a){}public void Test(double a){}public void Test(int a,int b){}
ตัวอย่างการใช้งาน method overload
using System;public class TestOverload{ public void AddValue() { Console.WriteLine("Call AddValue() : {0}", (3 + 4)); } public void AddValue(int x, int y) { Console.WriteLine("Call AddValue(int x,int y) : {0}", x + y); } public void AddValue(double x, double y) { Console.WriteLine("Call AddValue(double x,double y) : {0}", x + y); } public static void Main() { TestOverload a = new TestOverload(); a.AddValue(); a.AddValue(10, 20); a.AddValue(5.5, 6.7); Console.ReadLine(); }}ผลลัพธ์แสดงดังรูป
ในตัวอย่างนี้มีการกำหนดเมธอด AddValue ที่เป็น overload กันคือเมธอดแรกไม่มีค่า parameter ใดๆ เมธอดที่สอง รับค่า parameter ที่เป็น int เมธอด ที่สาม รับค่า parameter ที่เป็น double
การใช้งาน Propertiesproperties คือเมธอดที่สามารถกำหนดค่าและreturn ค่าได้เลยในตัวเอง สังเกตว่าถ้าเราสร้างเมธอดที่กำหนดค่าและแสดงค่าเราต้องต้องสร้างเมธอดสองเมธอด้วยกันคือprivate int number;
public void SetVAlue(int value){ number = value;}
public int GetValue(){return number;}
แต่ใน properties เราสามารถกำหนดเพียงแค่ 1 properties ได้ดังนี้
private int number;
public int Number{
get{ return number;}
set{number = value;}
}
ตัวอย่างการใช้งาน properties
using System;public class TestProperties{ private int number; public TestProperties() { this.number = 0; } public TestProperties(int number) { this.number = number; } public int Number //สร้าง Properties ที่ชื่่อ Number { get { return number; } set { number = value; } }}public class TestMainClass{ public static void Main() { TestProperties t = new TestProperties(); Console.WriteLine(t.Number); //แสดงค่าของ Properties t.Number = 9999; //กำหนดค่าของ Properties Console.WriteLine(t.Number); //แสดงค่าของ Properties Console.WriteLine(); TestProperties u = new TestProperties(100); Console.WriteLine(u.Number); u.Number = 100000; Console.WriteLine(u.Number); Console.ReadLine(); }}
ผลลัพธ์จะได้09999
100100000
การใช้งาน static methodstatic method คือเมธอดที่กำหนด keyword static นำหน้าโดยสิ่งสำคัญเกี่ยวกับ static method คือ
static method จะไม่สามารถใช้ this ใน static method ได้static method สามารถเรียนผ่านเมธอดที่เป็น static ด้วยกันได้static method จะใช้กับตัวแปรที่เป็น static เท่านั้นstatic method ไม่สามารถใช้กับ instance ของคลาสที่สร้างโดยใช้ new
รูปแบบเช่นpublic static void Test(){}
ตัวอย่างการใช้งาน static method
using System;public class TestStaticMethod{ private static int value; private int number; //ตัวแปรนี้ ไม่สามารถใช้กับเมธอด ShowValue ได้เนื่องจากว่าเมธอด ShowValue กำหนดเป็น static public static void ShowValue() //static method { value = 10; Console.WriteLine("value is {0}", value); } public static void Main() { ShowValue(); //สามารถเรียกใช้งาน ShowValue() ได้เลยเนื่องจากว่า Main() เป็น static เมธอดอยู่แล้วสามารถเรียกใช้ static เมธอดตัวอื่นๆได้ Console.ReadLine(); }}
ผลลัพธ์จะได้value is 10
การใช้งาน Static properties
static properties จะคล้ายๆกับ static method คือสามารถเรียกใช้ตัวแปรที่เป็น static ด้วยกันได้
ตัวอย่าง
using System;public class TestStaticProperties{ private static int number; public TestStaticProperties() { } public static int Number { get { return number; } set { number = value; } } public static void Main() { TestStaticProperties.Number = 3456; //กำหนดค่าให้กับ static properties Console.WriteLine(TestStaticProperties.Number); //แสดงค่าของ static properties Console.ReadLine(); }}
ผลลัพธ์จะได้3456

No comments:

Post a Comment