ไวยากรณ์ภาษาจาวา1.การประกาศ class2.การประกาศ attributes3.การประกาศ methods4.การประกาศ object5.การเรียกใช้ methods6.การเรียกใช้ attributes1.การประกาศ Classคลาสเปรียบเสมือนแม่พิมพ์ วัตถุเป็นผลิตผลที่เกิดจากแม่พิมพ์ ดังนั้น การที่จะสร้างวัตถุได้ จึงจำเป็นต้องอาศัยแม่พิมพ์หรือคลาสนี้ สำหรับการประกาศคลาสเริ่มต้นด้วยคำหลัก Class ตามด้วยชื่อของClass กำหนดขอบเขตด้วย {} และจบด้วยเครื่องหมาย เซมิโคลอน (;)รูปแบบคำสั่งclass ชื่อคลาส{// คำสั่ง// คำสั่ง}ตัวอย่างpublic class person{..}2.การประกาศ attributes (คุณลักษณะ)คุณลักษณะของออปเจ็ค คือตัวแปรหรือค่าคงที่ซึ่งประกาศภายในออปเจ็ค โดยมีตัวอย่างการประกาศคือรูปแบบ[modifier] dataType attributeName;- Modifier คือ คีย์เวิร์ดของภาษาจาวาที่อธิบายคุณสมบัติต่างๆ ของตัวแปรหรือค่าคงที่- dataType คือ ชนิดข้อมูลซึ่งอาจเป็นชนิดข้อมูลพื้นฐานหรือชนิดคลาส- attributeName คือ ชื่อของคุณลักษณะตัวอย่างpublic class person{private String id;private String name;public int number ;}3.การประกาศ methodsภาษาจาวากำหนดรูปแบบของการประกาศเมธอดที่อยู่ในคลาสไว้ดังนี้รูปแบบ[modifier] return_type methodName ([argument]) {[method body]}- Modifier คือ คีย์เวิร์ด ของภาษาจาวาที่ใช้ในการอธิบายระดับการเข้าถึง (Access modifier)- Return_type คือ ชนิดข้อมูลของค่าที่จะมีการส่งกลับ- methodName คือ ชื่อของเมธอด- Arguments คือ ตัวแปรที่ใช้ในการรับข้อมูลที่ออปเจ็คส่งมาให้- Method body คือ คำสั่งต่างๆ ของภาษาจาวาที่อยู่ในเมธอดตัวอย่างpublic class person{private String id;private String name;public int number ;public void setData(String aId,String aName){id = aId;name = aName;}}4.การประกาศ Objectคำสั่งที่ใช้ในการสร้างออปเจ็คจะมีรูปแบบ ดังนี้รูปแบบobjectName = new ClassName ([arguments]);- objectName คือชื่อของออปเจ็ค- new คือคีย์เวิร์ดของภาษาจาวาเพื่อใช้ในการสร้างออปเจ็ค- ClassName คือชื่อของคลาส- Arguments คือค่าที่ต้องการส่งผ่านในการเรียก Contructorตัวอย่างs1 = new Student();5.การเรียกใช้ methodsแบบที่ 1 : เรียกใช้ Constructor และใช้พื้นที่ในหน่วยความจำclass hello1 {public static void main(String args[]) {TAirPlane abc = new TAirPlane();}}แบบที่ 2 : แยกประกาศใช้คลาสและใช้พื้นที่ในหน่วยความจำclass hello2 {public static void main(String args[]) {TAirPlane abc;abc = new TAirPlane();}}แบบที่ 3 : ใช้พื้นที่ในหน่วยความจำ และเป็นการเรียกใช้ constructor ซึ่ง class นี้ ต้องอยู่ใน Directory เดียวกันclass hello3 {public static void main(String args[]) {new TAirPlane();}}แบบที่ 4 : เรียกใช้ method Fly() แต่จะเรียก constructor มาก่อน ถ้า class นั้นมี constructorclass hello4 {public static void main(String args[]) {new TAirPlane().Fly();}}แบบที่ 5 : เมื่อสร้างวัตถุขึ้นมา สามารถเรียกใช้ method อื่น โดย constructor ทำงานเฉพาะครั้งแรกclass hello5 {public static void main(String args[]) {TAirPlane abc = new TAirPlane();abc.Fly();abc.Land();}}แบบที่ 6 : แสดงตัวอย่างการเรียก main และต้องส่ง Array of String เข้าไปclass hello6 {public static void main(String args[]) {TAirPlane abc = new TAirPlane();String a[] = {}; // new String[0];abc.main(a);}}แบบที่ 7 : เรียกใช้ method ภายในคลาสเดียวกันclass hello7 {public static void main(String args[]) {minihello();}static void minihello() {System.out.println("result of mini hello");}}แบบที่ 8 : เรียกใช้ method แบบอ้างชื่อคลาส ในคลาสเดียวกันclass hello8 {public static void main(String args[]) {hello8 x = new hello8();x.minihello();}static void minihello() {System.out.println("result of mini hello");}}แบบที่ 9 : เรียกใช้ method แบบไม่กำหนด method เป็น Static พร้อมรับ และคืนค่า:: ผลลัพธ์คือ 8class hello9 {public static void main(String args[]) {hello9 xx = new hello9();System.out.println(xx.oho(4));}int oho(int x) { return (x * 2); }}แบบที่ 10 : เรียกใช้ method ภายในคลาสเดียวกัน โดย method สามารถรับ และส่งค่าได้:: เรียก method ใน static ตัว method ที่ถูกเรียกต้องเป็น static ด้วยclass hello10 {public static void main(String args[]) {System.out.println(oho(5));}static int oho(int x) {x = x * 2;return x;}}แบบที่ 11 : ใช้ extends เพื่อการสืบทอด (Inheritance):: Constructor ของ TAirPlane จะไม่ถูกเรียกมาทำงานclass hello11 extends TAirPlane {public static void main(String args[]) {Fly();Land();}}แบบที่ 12 : ใช้ extends เพื่อการสืบทอด (Inheritance) แบบผ่าน constructor:: Constructor ของ TAirPlane จะถูกเรียกมาทำงานclass hello12 extends TAirPlane {hello12() {Fly();Land();}public static void main(String args[]) {new hello12();}}6.การเรียกใช้ attributes การเรียกใช้แบบที่ 1 syntax:ClassName object = new ClassName().attributeName; example:Person bamboo = new Person().name; การเรียกใช้แบบที่ 2 syntax:ClassName object = new ClassName(); object.attributeName; example:Person bamboo = new Person(); bamboo.name = "bamboolabcode";
ไวยากรณ์ภาษาจาวา
สมัครสมาชิก:
บทความ (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น