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

 

התוכנית המלאה

 

כמה מקטעי הקוד המעניינים מודגשים.

 

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

 

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

 

התוכנית נבדקה עם 1.1.3 JDK תחת Win95.

 

/*File Intfc02.java Copyright 1997, R.G.Baldwin
This program illustrates the use of an interface to make 
it possible for a method in a library class to invoke a 
method of an object passed in simply as type Object.

Note that the method in the library class isn't required
to know the true type of objects passed in.  However, it
does require that they be of a type that implements the
interface named Intfc02A.  This makes it possible to
cast the incoming object according to that interface type 
and invoke the methods of the object.

This program produces the following output:

Compare 5 and 6
objA less than objB? true
objA equal to objB? false
objA greater than objB? false

Compare 5 and 5
objA less than objB? false
objA equal to objB? true
objA greater than objB? false

Compare 5 and 4
objA less than objB? false
objA equal to objB? false
objA greater than objB? true  
  
This program was tested using JDK 1.1.3 under Win95.
**********************************************************/

//This class is used to test the ability of the method
// named compare() of the class named LibraryClass to
// properly process objects of the class named UserClass.
// The class named UserClass implements an interface
// required by the method named compare() of the class 
// named LibraryClass to make such processing possible.
class Intfc02{
  public static void main(String[] args){
    System.out.println("Compare 5 and 6");
    new LibraryClass().compare(new UserClass(5),
                                         new UserClass(6));
    System.out.println("\nCompare 5 and 5");
    new LibraryClass().compare(new UserClass(5),
                                         new UserClass(5));
    System.out.println("\nCompare 5 and 4");
    new LibraryClass().compare(new UserClass(5),
                                         new UserClass(4));
  }//end main
}//end class Intfc02
//=======================================================//

//This is a user class whose objects are suitable for 
// processing by an object of the class named LibraryClass
// because this class implements the interface named 
// Intfc02A which is a requirement of the compare() method
// of the class named LibraryClass.

//By defining the methods named LT, EQ, and GT, this
// class makes it possible for the compare() method
// to compare two objects of this class using the methods
// defined in this class.  

//By implementing the interface named Intfc02A, this class
// makes it possible for the method named compare() of the
// class named LibraryClass to receive objects of this type
// simply as type Object (without knowing their true type)
// and then cast them to type Intfc02A for purposes of 
// invoking the methods of this class.

class UserClass implements Intfc02A{
  int data;
  
  UserClass(int data){//constructor
    this.data = data;
  }//end constructor
  //-----------------------------------------------------//
  
  //Define three methods that are declared in the 
  // interface named Intfc02A. These methods can be used
  // to determine if an object of this class is less than,
  // equal to, or greater than another object of this
  // class that is passed in as a parameter to the method.
  
  public boolean LT(Object obj){//test for less than
    if(this.data < ((UserClass)obj).data)return true;
    else return false;
  }//end LT()
  //-----------------------------------------------------//
    
  public boolean GT(Object obj){//test for greater than
    if(this.data  ((UserClass)obj).data)return true;
    else return false;
  }//end GT()
  //-----------------------------------------------------//
    
  public boolean EQ(Object obj){//test for equal to
    if(this.data == ((UserClass)obj).data)return true;
    else return false;
  }//end EQ()
    
}//end UserClass
//=======================================================//

//This class contains a method that compares two objects
// of any class that implements the interface named 
// Intfc02A. 

//The actual comparison methods are defined in the class
// of the incoming objects.  The method of this class
// casts those objects as the interface type and invokes
// the instance methods of the objects.
class LibraryClass{
  void compare(Object objA,Object objB){
    System.out.println("objA less than objB? " 
                             + ((Intfc02A)objA).LT(objB) );
    System.out.println("objA equal to objB? " 
                             + ((Intfc02A)objA).EQ(objB) );
    System.out.println("objA greater than objB? " 
                             + ((Intfc02A)objA).GT(objB) );
  }//end compare() method
}//end LibraryClass
//=======================================================//

 

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