简体   繁体   中英

java inheritance within one class, deploy variables to two functions

How can I make the variables I define at the top of this class apply to both of the functions within?

specifically dict and url.

Right now eclipse is telling me, with regards to dict.open() that an "identifier is expected after it", but I think this is a red herring because if I move it back inside the getHypernyms method it will work again. I could just copy that code into both methods but that's so stupid and such bad style. There must be a more elegant way to achieve that.

public class MITJavaWordNetInterface 
{

// construct the URL to the Wordnet dictionary directory
String wnhome = System.getenv("WNHOME");
String path = wnhome + File.separator + "dict";
URL url = new URL ("file", null , path );

// construct the dictionary object and open it
IDictionary dict = new Dictionary ( url ) ;
dict.open();


public void getHypernyms( String inut_word ) throws IOException
{   

    // get the synset of 'input_word'
    IIndexWord idxWord = dict . getIndexWord (inut_word, POS . NOUN ) ;
    IWordID wordID = idxWord . getWordIDs () . get (0) ; // 1st meaning
    IWord word = dict . getWord ( wordID ) ;
    ISynset synset = word . getSynset () ;

    // get the hypernyms
    List < ISynsetID > hypernyms =
    synset . getRelatedSynsets ( Pointer . HYPERNYM ) ;

    // print out each h y p e r n y m s id and synonyms
    List < IWord > words ;
    for( ISynsetID sid : hypernyms ) {
    words = dict . getSynset ( sid ) . getWords () ;
    System . out . print ( sid + " {") ;
    for( Iterator < IWord > i = words . iterator () ; i . hasNext () ;) {
    System . out . print ( i . next () . getLemma () ) ;
    if( i . hasNext () )
    System . out . print (", ") ;
    }
    System . out . println ("}") ;
    }


}

public void getStem (String word)
{
 //JWS ws = new JWS("C:/Program Files/WordNet","2.1");  
 WordnetStemmer stem =  new WordnetStemmer( dict );
 System.out.println("test" + stem.findStems(word, null) );
}

}

dict.open(); is not in any method or constructor , and every statement (there are some exceptions with initializations of variables) must be in a method in java.

You should create a constructor for your object, and initialize dict in it:

public class  MITJavaWordNetInterface  {
   //I added private modifier for variables, remove if they're not private
   private String wnhome
   private String path
   private URL url;
   private IDictionary dict;

   //Here is an argumentless constructor:   
   public MITJavaWordNetInterface() { 
       // construct the URL to the Wordnet dictionary directory
       wnhome = System.getenv("WNHOME");
       path = wnhome + File.separator + "dict";
       url = new URL ("file", null , path );
       // construct the dictionary object and open it
      dict = new Dictionary ( url ) ;
      dict.open();
   }
   ///methods
}

If you allow MITJavaWordNetInterface objects, you should create a constructor and do your initializations here:

public class MITJavaWordNetInterface 
{
    String wnhome;
    String path;
    URL url;

    IDictionary dict;

    public MITJavaWordNetInterface() {
        wnhome = System.getenv("WNHOME");
        path = wnhome + File.separator + "dict";
        url = new URL ("file", null, path);
        dict = new Dictionary(url) ;
        dict.open();
    }

    public void getHypernyms(String inut_word) throws IOException {
        ...
    }

    public void getStem(String word) {
        ...
    }
}

and:

public static void main(String[] args) {
    MITJavaWordNetInterface m = new MITJavaWordNetInterface();
    m.getHypernyms(...);
    m.getStem(...);
}

Otherwise, create a static constructor in which to do your initializations:

public class MITJavaWordNetInterface 
{
    static String wnhome;
    static String path;
    static URL url;

    static IDictionary dict;

    static {
        wnhome = System.getenv("WNHOME");
        path = wnhome + File.separator + "dict";
        url = new URL ("file", null, path);
        dict = new Dictionary(url) ;
        dict.open();
    }

    public static void getHypernyms(String inut_word) throws IOException {
        ...
    }

    public static void getStem(String word) {
        ...
    }
}

and:

public static void main(String[] args) {
    MITJavaWordNetInterface.getHypernyms(...);
    MITJavaWordNetInterface.getStem(...);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM