További interface kérdés

Fórumok

Sziasztok!

Nemrégiben dobtam fel egy olyan témát, ami az interfészekkel foglalkozott, de most tovább kell kérdeznem, mert a JAZZY nevű helyesírás ellenőrző program értelmezése során újra csak előbukkant egy ilyen probléma:

A SpellChecker osztályban található a isCorrect() függvény, ami egy szóról eldönti, hogy helyesen van leírva vagy sem. Ez így néz ki:


  public boolean isCorrect(String word) {
    if (userdictionary.isCorrect(word)) return true;
    for (Enumeration e = dictionaries.elements(); e.hasMoreElements();) {
      SpellDictionary dictionary = (SpellDictionary) e.nextElement();
      if (dictionary.isCorrect(word)) return true;
    }
    return false;
  }

Ahol a userdictionary a SpellChecker egy adattagja és így van deklarálva:


  private SpellDictionary userdictionary;

A SpellDictionary pedig egy interface, ami így néz ki:


import java.util.*;

public interface SpellDictionary {
  public void addWord(String word);
  public boolean isCorrect(String word);
  public List getSuggestions(String sourceWord, int scoreThreshold);
  public List getSuggestions(String sourceWord, int scoreThreshold , int[][] matrix);
}

Kérdés: Hogyan működhet ez, hiszen az interface-ben nincs kód, csak függvénydeklarációk. Mi fut le, amikor ezt írják:


    if (userdictionary.isCorrect(word)) return true;

Hozzászólások

Meg kéne nézni a userdictionary hol kap értéket :)

Valószínűleg valami ilyesmit fogsz találni:

SpellDictionary userDictionary = new ValamilyenLofaszSpellDictionaryImpl();

Ahol ValamilyenLofasz.... az implementalja a SpellDictionary-t.

Erted?

List list1;
List list2;

list1 = new ArrayList();
list2 = new LinkedList();

Eerted.

Nos a userdictionary a következő két helyen kaphat értéket a programban:


  // első eset: 
  public SpellChecker() {
    try {
      userdictionary = new SpellDictionaryHashMap();
    } catch (IOException e) {
      throw new RuntimeException("this exception should never happen because we are using null phonetic file");
    }
  }

// ahol:

  public class SpellDictionaryHashMap extends SpellDictionaryASpell {...}
  public abstract class SpellDictionaryASpell implements SpellDictionary {...}

Tehát valóban, ahogy írtad! Akkor ebben az estben a SpellDictionaryHashMap osztályban lévő isCorrect() függvény fut le?

A másik lehetőség:


  public void setUserDictionary(SpellDictionary dictionary) {
    userdictionary = dictionary;
  }

// amit ebben a konstruktorban hívtak meg:
  public JTextComponentSpellChecker(SpellDictionary dict, SpellDictionary userDict, String title) {
    spellCheck = new SpellChecker(dict);
    mainDict=dict;
    spellCheck.setCache();
    if (userDict != null) {
        spellCheck.setUserDictionary(userDict);
    }
    spellCheck.addSpellCheckListener(this);
    dialogTitle = title;
    messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
    markHandler = new AutoSpellCheckHandler(spellCheck, messages);
  }

// viszont ezt az osztály konstruktort sehol nem hívják meg a programban, hacsak nem egy másik konstruktorban:
  public JTextComponentSpellChecker(SpellDictionary dict) {
    this(dict, null, null);
  }

Tehát ez a második variáció, vak vágányra vezetett. Nem találtuk a nyomát, hogy milyen osztályt kap értékül a userdictionary változó. Akkor nyugtázhatjuk, hogy alapvetően igazad van.

Tehát egy interfésznek deklarált változónak adhatunk értéket olyan osztálynak egy példányával is, ami implementálja az interfészt.
Helyes a végső következtetésem?

Ezt írja a tutorial:

In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

Köszönöm a segítségeteket, azt hiszem most már értem. (Egyszer meg kell tanulni végre!)