18. การประกาศคลาส
โปรแกรมภาษาจาวาแต-ละโปรแกรมจะประกอบไปด$วยคลาสอย-างน$อยหน1ง
คลาส โดยม'ร+ปแบบการประกาศด+งน'I
[modifier] class Classname {
[class member]
}
• modifier ค/อคยเวรด (keyword) ของภาษาจาวาทใช$ในการอธบายระดบการเข$าถ1ง
(access modifier)
• class ค/อคยเวรดของภาษาจาวาเพ/อระบว-าเปนการประกาศคลาส
• Classname ค/อช/อคลาส
• class member ค/อเมธอดหร/อคณลกษณะ
ต+วอย@าง
public class Student {
}
19. การประกาศค1ณล+กษณะ
ค1ณล+กษณะของอ7อบเจกต9คอต+วแปรหรอค@าคงท'ซSOงประกาศภายในอ7อบ
O
เจกต9 โดยม'ร+ปแบบการประกาศด+งน'I
[modifier] dataType attributeName;
• modifier คอค'ย9เวร9ดของภาษาจาวาท'Oอธบายค1ณสมบ+ตต@างๆของต+วแปรหรอค@า
คงท'O
• dataType คอชนดข0อมRลซSOงอาจเปYนชนดข0อมRลพIนฐานหรอชนดคลาส
• attributeName คอชOอของค1ณล+กษณะ
ต+วอย@าง
public class Student {
public String id;
public String name;
public double gpa;
}
20. การประกาศเมธอด
ภาษาจาวาก6าหนดรRปแบบของการประกาศเมธอดท'อยRในคลาสไว0ด+งน'I
O @
[modifier] return_type methodName([arguments]) {
[method_body]
}
• modifier คอค'ย9เวร9ดของภาษาจาวาท'Oใช0อธบายระด+บการเข0าถSง
• return_type คอชนดข0อมRลของค@าท'จะม'การส@งกล+บ
O
• methodName คอชOอของเมธอด
• arguments คอต+วแปรท'Oใช0ในการร+บข0อมRลท'อ7อบเจกต9ส@งมาให0
O
• method_body คอค6าส+Oงต@างๆของภาษาจาวาท'อยR@ในเมธอด
O
21. ตวอย-างโปรแกรม
public class Student {
public String id;
public String name;
public double gpa;
public void setID(String ID) {
id = ID;
}
public void setName(String n) {
name = n;
}
public void setGPA(double GPA) {
gpa = GPA;
}
public void showDetails() {
System.out.println("ID: "+id);
System.out.println("Name: "+name);
System.out.println("GPA: "+gpa);
}
}
22. เมธอดท'ชOอ main()
O
โปรแกรมจาวาประย1กต9 (Java Application) จะเรOมต0นการท6างานในคลาสท'O
ม'เมธอดท'OชOอ main โดยม'รปแบบของเมธอดด+งน'I
R
public static void main(String args[]) {
[method_body]
}
36. ตวอย-างโปรแกรม
class Ball {
public void throwBall() { }
}
class SoccerBall extends Ball {
public void throwBall() {
System.out.println("Throwing soccerball");
}
}
class TennisBall extends Ball {
public void throwBall() {
System.out.println("Throwing tennisball");
}
}
public class TestBall {
public static void main(String args[]) {
Ball b1 = new Ball();
SoccerBall b2 = new SoccerBall();
Ball b3 = new SoccerBall();
}
}
37. คลาสแบบ abstract
คลาสทม modifier เปน abstract หมายความว@าคลาสน+นย+งเปYนคลาสท'O
I
ไม@สมบRรณ9 โดยม'เมธอดแบบ abstract ซSงเปYนเมธอดท'ย+งไม@สมบRรณ9
O O
อย@างน0อยหนSงเมธอดอยR@ในคลาส
O
รRปแบบของเมธอดแบบ abstract
[modifier] abstract return_type methodName([arguments]);
คลาสแบบ abstract ก6าหนดขSIนมาเพOอให0คลาสอOนสบทอด โดยคลาสท'มา
O
สบทอดจะต0องก6าหนดบล7อกค6าส+งในเมธอดท'ย+งไม@สมบRรณ9
O O
เราไม@สามารถสร$างออบเจกตของคลาสแบบ abstract ได$
38. ตวอย-างโปรแกรมแสดงคลาสแบบ abstract
public abstract class Student {
protected String id;
protected String name;
protected double gpa;
public void setID(String ID) {
id = ID;
}
public void setName(String n) {
name = n;
}
public void setGPA(double GPA) {
gpa = GPA;
}
public abstract void showDetails();
}
39. ตวอย-างคลาสท'Oสบทอดมาจากคลาสแบบ abstract
public class FullTimeStudent extends Student {
private int credit;
private final int MAX_YEAR = 4;
public FullTimeStudent(int c) {
credit = c;
}
public void showDetails() {
System.out.println("ID: "+id);
System.out.println("Name: "+name);
System.out.println("GPA: "+gpa);
System.out.println("Credit: "+credit);
}
}
40. อนเตอรเฟส
อนเตอร9เฟส (interface) ม'ล+กษณะคล0ายก+บคลาสแบบ abstract แต@จะ
ประกอบด0วยเมธอดท'ย+งไม@สมบRรณ9เท@าน+น
O I
รRปแบบของอนเตอร9เฟส
[modifier] interface InterfaceName {
[methods();]
}
อนเตอร9เฟสก6าหนดขSIนมาเพOอให0คลาสอOนน6าไปใช0งานโดยใช0ค'ยเวร9ด
9
implements โดยม'รปแบบด+งน'I
R
[modifier] class ClassName implements InterfaceName {
[methods();]
}
42. ตวอย-างอนเตอรเฟส
public interface Student {
public void setID(String ID);
public void setName(String n);
public void setGPA(double GPA);
public void showDetails();
}
43. ตวอย-างคลาสท'O implements อนเตอรเฟส
public class PartTimeStudent implements Student {
private String id;
private String name;
private double gpa;
private int credit;
private final int MAX_YEAR = 8;
public PartTimeStudent(int c) {
credit = c;
}
public void setID(String ID) {
id = ID;
}
public void setName(String n) {
name = n;
}