ไวยากรณ์ภาษาจาวา
การประกาศ Class
modifier คือ keyword ของภาษาจาวาที่ใช้เป็น access modifie เช่น private, package, protected, public (ลำดับความเข้มงวดที่สุดไล่ลำดับถึงอิสระที่สุด) หรืออธิบายคุณสมบัติอื่นๆ ของคลาส เช่น abstract และ final
class คือ keyword ของภาษาจาวาเพื่อระบุว่าเป็นการประกาศคลาส
ClassName คือชื่อของ class ที่เป็นชื่อ identifier ใดๆ ที่สอดคล้องกับกฎการตั้งชื่อ
class member คือ method หรือ attribute ของ class
ตัวอย่างการประกาศคลาส student สามารถทำได้ดังนี้
public class Student{
}
การประกาศ Attribute
modifier คือ keyword ของภาษาจาวาที่ใช้เป็น access modifier เช่น public หรืออธิบายคุณสมบัติอื่นๆ ของคลาส เช่น abstract และ final
dataType คือชนิดข้อมูลซึ่งอาจเป็นชนิดข้อมูลแบบพื้นฐานหรือชนิดข้อมูลแบบอ้างอิง
attributeName คือชื่อของคุณลักษณะที่เป็นชื่อ identifier ใดๆ ซึ่งสอดคล้องกับกฎการตั้งชื่อ
ตัวอย่าง
public class Student{
public String id;
public String name;
public double gpa;
}
การประกาศ method
modifier คือ keyword ของภาษาจาวาที่ใช้เป็น access modifier เช่น public หรืออธิบายคุณสมบัติอื่นๆ ของคลาส เช่น abstract และ final
return_type คือชนิดข้อมูลของค่าที่ส่งกลับหลังจากเสร็จสิ้นการทำงานของคำสั่งใน method นี้ โดยชนิดข้อมูลของค่าที่ส่งกลับอาจเป็นชนิดข้อมูลแบบพื้นฐาน หรือชนิดข้อมูลแบบอ้างอิง ในกรณีที่ไม่มีการส่งค่าใดๆ กลับจะต้องระบุชนิดข้อมูลเป็น void
methodName คือชื่อของ method ที่เป็นชื่อ identifier ใดๆ ที่สอดคล้องกับกฎการตั้งชื่อ
arguments คือตัวแปรที่ใช้ในการรับข้อมูลที่ object ส่งมาให้ โดยอาจมีมากกว่าหนึ่งตัวแปร หรือไม่มีเลยก็ได้ขึ้นอยู่กับการกำหนด method
method_body คือคำสั่งต่างๆ ของภาษาจาวาที่อยู่ใน method
ตัวอย่าง
การประกาศ object
modifier คือ keyword ของภาษาจาวาที่ใช้เป็น access
modifier เช่น public หรืออธิบายคุณสมบัติอื่นๆ ของคลาส เช่น abstract และ final
ClassName คือชื่อของ class
objectName คือชื่อของ object ที่เป็นชื่อ identifier ใดๆ ที่สอดคล้องกับกฎการตั้งชื่อ
lตัวอย่าง
Student s1;
การเรียกใช้ Method
แบบที่ 1 : เรียกใช้ Constructor และใช้พื้นที่ในหน่วยความจำ
- class hello1 {
public static void main(String args[]) {
TAirPlane abc = new TAirPlane();
}
}
- class hello2 {
public static void main(String args[]) {
TAirPlane abc;
abc = new TAirPlane();
}
}
- class hello3 {
public static void main(String args[]) {
new TAirPlane();
}
}
- class hello4 {
public static void main(String args[]) {
new TAirPlane().Fly();
}
}
- class hello5 {
public static void main(String args[]) {
TAirPlane abc = new TAirPlane();
abc.Fly();
abc.Land();
}
}
- class hello6 {
public static void main(String args[]) {
TAirPlane abc = new TAirPlane();
String a[] = {}; // new String[0];
abc.main(a);
}
}
- class hello7 {
public static void main(String args[]) {
minihello();
}
static void minihello() {
System.out.println("result of mini hello");
}
}
- class hello8 {
public static void main(String args[]) {
hello8 x = new hello8();
x.minihello();
}
static void minihello() {
System.out.println("result of mini hello");
}
}
- class hello9 {
public static void main(String args[]) {
hello9 xx = new hello9();
System.out.println(xx.oho(4));
}
int oho(int x) { return (x * 2); }
}
- class hello10 {
public static void main(String args[]) {
System.out.println(oho(5));
}
static int oho(int x) {
x = x * 2;
return x;
}
}
- class hello11 extends TAirPlane {
public static void main(String args[]) {
Fly();
Land();
}
}
- class hello12 extends TAirPlane {
hello12() {
Fly();
Land();
}
public static void main(String args[]) {
new hello12();
}
}
การเรียกใช้ Attributes
การเรียกใช้แบบที่ 1
syntax:ClassName object = new ClassName().attributeName;
example:Person bamboo = new Person().name;
การเรียกใช้ attribute แบบที่ 2
syntax:ClassName object = new ClassName();
object.attributeName;
example:Person bamboo = new Person();
bamboo.name = "bamboolabcode";
ไม่มีความคิดเห็น:
แสดงความคิดเห็น