ในรูปนี้เป็นเพียงการประกาศ ตัวแปร(Member variable) และเมธอด (Method) ของคลาสเท่านั้น ยังมีการประกาศ Property ที่เป็นการกำหนดคุณสมบัติให้กับตัวแปรอีก ซึ่งผมจะพูดถึงต่อไปนะครับ
ใน Objective-C นั้นเราสามารถประกาศตัวแปรได้ทั้ง 2 แบบ คือแบบ Strong type และแบบ Weak type สำหรับการประกาศแบบ Strong type ก็ประกาศแบบปกติครับ โดยขึ้นต้นด้วยชื่อ class และตามด้วย * (หมายถึงการเป็น pointer) และตามด้วยชื่อของ object ที่เราจะตั้ง แต่การประกาศแบบ Weak type นั้นจะใช้คำว่า id นำหน้าโดย “ไม่ต้องมี *” เพราะการประกาศ id นั้นจะเป็นการประกาศตัวแปรที่เป็น pointer ไปโดยอัตโนมัติ ซึ่งจะใช้บ่อยอยู่พอสมควรในกรณีที่เราไม่รู้ว่า object นั้นเป็น type อะไร ดังนี้ครับ
การประกาศ attributes
คุณลักษณะของออปเจ็ค คือตัวแปรหรือค่าคงที่ซึ่งประกาศภายในออปเจ็ค โดยมีตัวอย่างการประกาศคือ
[modifier] dataType attributeName;
- Modifier คือ คีย์เวิร์ดของภาษาจาวาที่อธิบายคุณสมบัติต่างๆ ของตัวแปรหรือค่าคงที่
- dataType คือ ชนิดข้อมูลซึ่งอาจเป็นชนิดข้อมูลพื้นฐานหรือชนิดคลาส
- attributeName คือ ชื่อของคุณลักษณะ
- dataType คือ ชนิดข้อมูลซึ่งอาจเป็นชนิดข้อมูลพื้นฐานหรือชนิดคลาส
- attributeName คือ ชื่อของคุณลักษณะ
การประกาศ methods
ภาษาจาวากำหนดรูปแบบของการประกาศเมธอดที่อยู่ในคลาสไว้ดังนี้
[modifier] return_type methodName ([argument]) {
[method body]
}
- Return_type คือ ชนิดข้อมูลของค่าที่จะมีการส่งกลับ
- methodName คือ ชื่อของเมธอด
- Arguments คือ ตัวแปรที่ใช้ในการรับข้อมูลที่ออปเจ็คส่งมาให้
- Method body คือ คำสั่งต่างๆ ของภาษาจาวาที่อยู่ในเมธอด
การประกาศ object
คำสั่งที่ใช้ในการสร้างออปเจ็คจะมีรูปแบบ ดังนี้
objectName = new ClassName
([arguments]);
- objectName คือชื่อของออปเจ็ค
- new คือคีย์เวิร์ดของภาษาจาวาเพื่อใช้ในการสร้าง
ออปเจ็ค
- ClassName คือชื่อของคลาส
- Arguments คือค่าที่ต้องการส่งผ่านในการเรียก
Contructor
การเรียกใช้ methods
qobjectName คือชื่อของ object
qmethodName คือชื่อ method ของ object นั้น
qarguments คือค่าที่ต้องการส่งผ่านไปให้กับ method ของ object นั้น โดยที่จะต้องมีชนิดข้อมูลและจำนวน argument ให้สอดคล้องกับที่ประกาศใน method ของ object นั้น
qตัวอย่าง
วิธีเรียกใช้ method แบบต่าง ๆ
:: ผลลัพธ์คือ 8
:: เรียก method ใน static ตัว method ที่ถูกเรียกต้องเป็น static ด้วย
:: Constructor ของ TAirPlane จะไม่ถูกเรียกมาทำงาน
:: Constructor ของ TAirPlane จะถูกเรียกมาทำงาน
แบบที่ 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();
}
}
การเรียกใช้ Class Attribute
ในการใช้งาน Class เมื่อเราต้องการที่จะอ้างอิง Attribute ที่อยู่ภายใน Class สามารถทำได้ โดยใช้ Pointer พิเศษที่ชื่อว่า$this ชี้ไปที่ชื่อของ Attribute ที่เราต้องการรูปแบบ
การเรียกใช้ Class Operation
qการเรียกใช้งาน Class Operation จะทำได้ก็ต่อมีการเรียกใช้งานผ่าน instance ที่เราสร้างขึ้นมาจาก Class
Example: Class Attribute & Operation
ไม่มีความคิดเห็น:
แสดงความคิดเห็น