ก่อนอื่นเรามาทดลองเรื่องการใช้ไทป์ที่ไม่ตรงกับตัวออบเจ็กต์กันก่อน สร้างโปรเจ็คและพิมพ์โค้ดที่นิยามจักรยานตามนี้เลย
public
class Bicycle {
// the Bicycle class has
three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one
constructor
public Bicycle(int
startCadence, int startSpeed, int startGear) {
gear
= startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has
four methods
public void setCadence(int
newValue) {
cadence = newValue;
}
public void setGear(int
newValue) {
gear
= newValue;
}
public void applyBrake(int
decrement) {
speed -= decrement;
}
public void speedUp(int
increment) {
speed += increment;
}
}
คราวนี้มาลองทำ MountainBike
ซึ่งเป็นสับคลาสของ Bicycle
ลอกโค้ดข้างล่างนี้ไปเลย:
public
class MountainBike extends Bicycle {
// the MountainBike subclass
adds one field
public int seatHeight;
// the MountainBike subclass
has one constructor
public MountainBike(int
startHeight, int startCadence,
int
startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the MountainBike subclass
adds one method
public void setHeight(int
newValue) {
seatHeight = newValue;
}
}
ตกลง MountainBike มีเมธอดกี่เมธอด ตัวแปรกี่ตัวแปร จงแจกแจงอธิบายมาใน text file
public MountainBike myBike =
new MountainBike();
MountainBike
นั้น extend มาจาก Bicycle
ส่วน Bycycle ก็มาจาก Object
(ไม่ได้เขียน extend แต่ว่าเป็นที่่เข้าใจกัน)
ดังนั้น MountainBike คันหนึ่งก็ถือเป็นทั้ง Bicycle และ Object ดังนั้นจึงใช้ในที่ที่สามารถใช้ Bicycle และ Object ได้เลย
จงเขียนโค้ดส่วนนี้ลงใน main ของ MountainBike
สังเกตว่าคอมไพล์ได้หรือไม่ บันทึกผลลงใน text file ถ้าไม่ได้ ต้องบอกสาเหตุด้วย
Object a = new
MountainBike();
ถ้ามีปัญหา ให้เขียนโค้ดแก้ซะ เขียนบอกว่าแก้โค้ดตรงไหนบ้างใน text file
แต่ในทางกลับกัน Bicycle คันหนึ่ง ไม่จำเป็นต้องเป็น MountainBike สิ่งที่โค้ดตีความว่าไม่ใช่ MountainBike จึงนำมาใส่ในตัวแปรที่เป็น MountainBike ไม่ได้
จงเขียนโค้ดส่วนนี้ต่อลงไปใน main แล้่วดูซิว่ามีอะไรเกิดขึ้น
MountainBike myBike = a;
จะสังเกตได้ว่าคอมไพล์เลอร์แจ้ง error ออกมา
เพราะสำหรับคอมไพเลอร์แล้ว a เป็นวัตถุชนิด Object ซึ่ง Object นั้น
ไม่จำเป็นที่จะต้องเป็น MountainBike ดังนั้นจึงใช้ไม่ได้ เราแก้ error
นี้ได้ด้วยการ สั่งเปลี่ยนไทป์เลย
MountainBike myBike =
(MountainBike)a;
Note: เราสามารถตรวจออบเจ็กต์ได้ว่าเป็นชนิดไหนแน่ โดยใช้ โอเปอร์เรเตอร์ instanceof
ตัวอย่างเช่น
if
(a instanceof MountainBike) {
MountainBike myBike = (MountainBike)a;
}
Instance method สำหรับสับคลาส ที่มี signature
(ชื่อเมธอด จำนวนพารามิเตอร์ ไทป์ของพารามิเตอร์) และ return type
เหมือนกับ instance method ที่นิยามใน superclass จะ override
เมธอดของ superclass
ถ้าสับคลาสนิยาม class method ที่มี signature เหมือนกับในซุปเปอร์คลาส เรากล่าวได้ว่า เมธอดของสับคลาสนั้น hide เมธอดใน superclass
public
class Animal {
public static void
testClassMethod() {
System.out.println("The class method in Animal.");
}
public void
testInstanceMethod() {
System.out.println("The instance method in Animal.");
}
}
Animal
ดังต่อไปนี้
public
class Cat extends Animal {
public static void
testClassMethod() {
System.out.println("The class method in Cat.");
}
public void
testInstanceMethod() {
System.out.println("The instance method in Cat.");
}
public static void
main(String[] args) {
Cat
myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
ลองรันดู
ทดลองเปลี่ยน Cat กับ Animal ดูตามที่ต่างๆ ผลจากการรันบอกอะไรเราบ้างเกี่ยวกับการเรียก class method และ instance
method (เขียนอธิบายการทดลองแต่ละอย่างให้ละเอียดชัดเจน) จงเขียนรายงานต่อใน text file
หลังจากนั้น เพิ่มเมธอด a() เข้าไปในคลาส Cat และเปลียนโค้ดของเมนตามนี้
ดูว่าเกิดอะไรขึ้น คิดว่าทำไมถึงเกิดเหตุการณ์เช่นนี้ เขียนรายงานมาใน text file
ต่อไป ลองเพิ่มเมธอด b ให้ Animal ให้เป็นโค้ดดังนี้
public void b(){
System.out.println("The instance method b in Animal.");
}
แล้วลองเรียก myAnimal.b(); ดู แทนการเรียก myAnimal.a();
เกิดอะไรขึ้นจงอธิบาย และให้เหตุผลขอสิ่งที่เกิดขึ้น ใน text file
ถ้าเมธอดของซุปเปอร์คลาสถูก override เราก็ยังเรียกเมธอดที่ถูก override ได้ โดยการใช้คีย์เวิร์ด super
ให้ทำดังต่อไปนี้
public
class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
public
class Subclass extends Superclass {
public void printMethod() {
//overrides printMethod in Superclass
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void
main(String[] args) {
Subclass s = new
Subclass();
s.printMethod();
}
}
cadence มีค่าได้เพียง 1 ถึง 10
ต้องปัดไปยังค่าขอบที่ใกล้ที่สุด
gear มีค่า -1 ถึง 4 ถ้ามีการทำค่าเกิน ต้องปัดไปยังค่าขอบที่ใกล้ที่สุด
speed มีค่าไม่ต่ำกว่า 0 ถ้ามีค่าต่ำกว่า 0 ให้ปัดเป็น 0
มี Method คือ
2.1 public double
value() ให้คืนค่าออกมาเป็นทศนิยม ให้ถูกต้องสองตำแหน่ง
2.2 public Fraction
add(Fraction f) คืนค่าผลบวกของเศษส่่วนสองตัว
2.3 public Fraction
subtract(Fraction f) คืนค่าผลลบ
2.4 public Fraction
multiply(Fraction f) คืนค่าผลคูณ อย่าลืมว่าต้องเป็นเศษส่วนอย่างต่ำ
2.5 public Fraction divide(Fraction f) หากส่วนเป็น 0 ให้ร้องว่า error zero by division ตัวอย่างการเขียน JUnit ของข้อนี้ อยู่นี่
ถ้าใครอยากรู้เพิ่มเรื่อง exception หรื error ที่เราสร้างได้เองในจาวา อ่านตรงนี้
2.6 public boolean
GreaterThan(Fraction f) หาก เศษส่วนตัวที่ต้องการเทียบมากกว่า f ให้ตอบ true เช่น t เป็น 2/3 และ f เป็น ½
จะได้ว่า t.GreaterThan(f)
= true
2.7 public boolean Equals(Fraction f) หาก เศษส่วนตัวที่ต้องการเทียบมีค่าเท่ากับ f ให้ตอบ true