» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
תוכנית לדוגמא
דף ראשי  מתחילים  עוד בנושא ממשקים  תוכנית לדוגמא גרסה להדפסה

 

תוכנית לדוגמא

 

התוכנית הבאה ממחישה כמה מן המושגים שהזכרנו.

התוכנית מצהירה על שני ממשקים בשם Constants ו‑MyIntfc. ממשק Constants מגדיר שני קבועים, וממשק MyIntfc מצהיר על מתודות בשם set() ו‑get().

ניתן היה לשלב בין הקבועים והמתודות במסגרת הגדרה של ממשק יחיד.

אולם, שני הממשקים הופרדו על מנת להמחיש את העובדה, שמחלקה אחת עשויה להשתמש בממשק אחד או יותר, תוך שימוש  ברשימת כל הממשקים, כאשר שמותיהם מופרדים בפסיקים.

 

התוכנית מגדירה שתי מחלקות בשם ClassA ו‑ClassB.
כל אחת מן המחלקות משתמשת בשני הממשקים הנ"ל.
פירוש הדבר הוא, שכל מחלקה מגדירה את מתודת
set() ואת מתודת get(),
שהוצהרו בממשק
MyIntfc. כל מחלקה גם עושה שימוש בקבועים שהוגדרו בממשק Constants.

 

חשוב לציין, שבהגדרת שתי המתודות של הממשק, כל מחלקה מגדירה אותן באופן המתאים לה, מבלי להתחשב באופן שבו הן מוגדרות במחלקות אחרות.

 

אחת המחלקות (ClassA) מגדירה גם מתודה נוספת בשם show(), שאינה מוצהרת בממשק.
הדבר נועד להמחיש את העובדה, שמתודה שאינה מוצהרת בממשק, אינה נגישה באמצעות משתנה הצבעה מהטיפוס של הממשק.

 

מתודת main במחלקה השולטת Intfc01 מבצעת סידרה של יצירת אובייקטים, הפעלת מתודות והשׂמות, אשר נועדו להמחיש את המאפיינים שתיארנו קודם.

ההערות והוראות print שבתוכנית אמורות לספק את ההסברים הנחוצים בשלב זה.

 

ממחיש את השימוש בממשק.

 

ההגדרות של הממשקים Constants ו‑MyIntfc נכללות בקבצים בשם Constants.java ו‑MyIntfc.java. המהדר מחייב שאלה יהיו קבצי source נפרדים.

 

הממשק Constants מכיל שני קבועים. הממשק MyIntfc מכיל את ההצהרות על set() ו‑get().

 

ClassA ו‑ClassB משתמשות בגירסאות שונות של set() ו‑get().

 

/*File Intfc01.java Copyright 1997, R.G.Baldwin
Revised 08/12/99
Illustrates use of interface.

The interface definitions for Constants and MyIntfc are 
contained in the files named Constants.java and 
MyIntfc.java.  The compiler requires them to be in separate
source files.

The interface named Constants contains two constants.
The interface named MyIntfc contains declarations for set()
and get().

Different versions of set() and get() are implemented in 
ClassA and ClassB.

The output from running the program is: (manual line breaks
were inserted to make it fit the display format)

Instantiate objA of type ClassA, then set, and show data.
In set() method for classA, using pi from Constants 
  interface: 12.28
In show() method for ClassA, data = 12.28

Assign objA to ref var of type MyIntfc named objAA.
Invoke set() method on objAA to modify the data.
In set() method for classA, using pi from Constants 
  interface: 24.56
Invoke get() method on objAA to display the modified data.
objA data = 24

Instantiate object of type ClassB named objB.  Immediately 
  assign the ref to a type MyIntfc ref var instead of a 
  type ClassB ref var.
Invoke its set() method to store some data.
In set() method for classB, using intConstant from 
  Constants interface: 1375
Invoke its get() method to display the data.
objB data = 1375

Successfully assign objA to objB and display objB
objB data = 24

Invoke set() method on objAA to modify its data.
In set() method for classA, using pi from Constants 
  interface: 98.24
Successfully assign objAA to objB and display objB
objB data = 98

Restore objB to its original type and value.
In set() method for classB, using intConstant from 
  Constants interface: 1375
Successfully assign objB to objAA and display objAA
objAA data = 1375

Attempt to assign objB to objA fails because
  "Explicit cast needed to convert MyIntfc to ClassA."

Attempt to invoke show() method of objAA fails because
"Method show() not found in interface MyIntfc".
End of program.

*/
//=======================================================//
class ClassA implements Constants,MyIntfc{
  double data;
  
  //Define versions of set() and get() appropriate to 
  // ClassA
  public void set(int inData){
    //note use of pi from Constants interface
    data = (double)inData*pi;
    System.out.println(
      "In set() method for classA, using pi from " +
                           "Constants interface: " + data);
  }//end set()
  
  public int get(){
    return (int)data;
  }//end get()
  
  //Define a show method for ClassA not declared in 
  // interface MyIntfc
  void show(){
    System.out.println(
            "In show() method for ClassA, data = " + data);
  }//end show() method
}//end ClassA
//=======================================================//

class ClassB implements Constants,MyIntfc{
  int data;
  
  //Define versions of set() and get() appropriate to 
  // ClassB
  public void set(int inData){
    //note use of intConstant from Constants interface
    data = inData*intConstant;
    System.out.println(
      "In set() method for classB, using intConstant " +
                     " from Constants interface: " + data);
  }//end set()
  
  public int get(){
    return data;
  }//end get()
}//end ClassB
//=======================================================//

class Intfc01{
  public static void main(String[] args){
    System.out.println(
      "Instantiate objA of type ClassA, then set, and " +
                                             "show data.");
    ClassA objA = new ClassA();
    objA.set(2);
    objA.show();    
  
    System.out.println("\nAssign objA to ref var of " +
                              "type MyIntfc named objAA.");
    MyIntfc objAA = objA;
    System.out.println("Invoke set() method on objAA " +
                                    "to modify the data.");
    objAA.set(4);  
    System.out.println("Invoke get() method on objAA to " +
                             "display the modified data.");
    System.out.println("objA data = " + objAA.get());
    
    System.out.println(
      "\nInstantiate object of type ClassB named objB." +
            "  Immediately assign\n the ref to a type " +
            "MyIntfc ref var instead of a type ClassB " +
                                              "ref var.");
    MyIntfc objB = new ClassB();
    System.out.println("Invoke its set() method to " +
                                      "store some data.");
    objB.set(11);
    System.out.println("Invoke its get() method to " +
                                     "display the data.");
    System.out.println("objB data = " + objB.get());

    System.out.println("\nSuccessfully assign objA to " +
                                  "objB and display objB");
    objB = objA;
    System.out.println("objB data = " + objB.get());

    System.out.println("\nInvoke set() method on objAA " +
                                    "to modify its data.");
    objAA.set(16);    
    System.out.println("Successfully assign objAA to " +
                                  "objB and display objB");
    objB = objAA;
    System.out.println("objB data = " + objB.get());
    
    System.out.println("\nRestore objB to its original " +
                                        "type and value.");
    //objB already defined as type MyIntfc
    objB = new ClassB();
    objB.set(11);
    System.out.println("Successfully assign objB to " +
                                "objAA and display objAA");
    objAA = objB;
    System.out.println("objAA data = " + objAA.get());

    System.out.println("\nAttempt to assign objB to " +
      " objA fails because\n  \"Explicit cast needed to " +
                           "convert MyIntfc to ClassA.\"");
    //objA = objB; //statement removed by making it comment
    
    System.out.println(
      "\nAttempt to invoke show() method of objAA fails " +
      "because\n \"Method show() not found in interface " +
                                             "MyIntfc\".");
    //objAA.show();//statement removed by making it 
    // a comment
    
    System.out.println("End of program.");

  }//end main
}//end class Intfc01
//=======================================================//

 

 

 05-01-04 / 20:26  נוצר ע"י רונית רייכמן  בתאריך 
 עוד בנושא ממשקים - הקודםהבא - מהו ממשק, ולשם מה הוא טוב? 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 3