วันจันทร์ที่ 25 มิถุนายน พ.ศ. 2555

java2

 การถ่ายทอดโดยตรงจาก class Dog ด้วยการใช้คำสั่ง extends และ
เรียกใช้ interface ทั้งสามด้วยคำสั่ง implements โดยเราได้กำหนดให้มี method bark() ใน
class Dog แต่เราจะ implement method swim() และ fetch() ใน class MyDog
ส่วนโปรแกรมทดสอบของเรามีดังนี้
225
1: /**
2: Testing multiple inheritance
3: */
4:
5: class MultipleInheritance {
6: public static void main(String[] args) {
7: MyDog red = new MyDog("Red");
9: act1(red); //treat as CanBark
10: act2(red); //treat as CanFetch
11: act3(red); //treat as CanSwim
12: act4(red); //treat as MyDog
13: }
14:
15: public static void act1(CanBark d) {
16: d.bark();
17: }
18:
19: public static void act2(CanFetch d) {
20: d.fetch();
21: }
22:
23: public static void act3(CanSwim d) {
24: d.swim();
25: }
26:
27: public static void act4(Dog d) {
28: d.bark();
29: }
30: }
ผลลัพธ์ที่เราได้จากการ run คือ
Woof Woof
Red is
Red is swiming.
Woof Woof
เราเริ่มต้นด้วย interface BankRate และ class Account
1: /**
2: interface for bank rate
3: */
4:
5: public interface BankRate {
6: double RATE = 5.0;
7: }

 ผลลัพธ์ผลลัพธ์ที่เราได้จากการ run คือ
Woof Woof
Red is fetching.
Red is swiming.
Woof Woof
เราเริ่มต้นด้วย interface BankRate และ class Account
1: /**
2: interface for bank rate
3: */
4:
5: public interface BankRate {
6: double RATE = 5.0;
7: }

1 ความคิดเห็น: