java继承在一个类中,将变量部署到两个函数

java继承在一个类中,将变量部署到两个函数,java,inheritance,coding-style,Java,Inheritance,Coding Style,如何使我在该类顶部定义的变量应用于其中的两个函数 特别是dict和url 现在eclipse告诉我,关于dict.open()“在它后面应该有一个标识符”,但我认为这是在转移视线,因为如果我把它移回到gethyperynms方法中,它会再次工作。我可以把代码复制到这两种方法中,但这太愚蠢了,而且风格太差了。必须有一种更优雅的方式来实现这一点 public class MITJavaWordNetInterface { // construct the URL to the Wordnet d

如何使我在该类顶部定义的变量应用于其中的两个函数

特别是dict和url

现在eclipse告诉我,关于
dict.open()
“在它后面应该有一个标识符”,但我认为这是在转移视线,因为如果我把它移回到
gethyperynms
方法中,它会再次工作。我可以把代码复制到这两种方法中,但这太愚蠢了,而且风格太差了。必须有一种更优雅的方式来实现这一点

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) );
}

}
公共类MITJavaWordNetInterface
{
//构造Wordnet字典目录的URL
字符串wnhome=System.getenv(“wnhome”);
字符串路径=wnhome+File.separator+“dict”;
URL=新URL(“文件”,null,路径);
//构造dictionary对象并打开它
IDictionary dict=新词典(url);
dict.open();
public void gethypernames(字符串inut_单词)引发IOException
{   
//获取“input\u word”的语法集
IIndexWord idxWord=dict.getIndexWord(inut_单词,POS.noon);
IWordID wordID=idxWord.getwordid().get(0);//第一个含义
IWord=dict.getWord(wordID);
ISynset-synset=word.getSynset();
//获取缩略词
列表缩略词=
getRelatedSynsets(指针、超名称);
//打印出每个h y p e r n y m s id和同义词
列出单词;
for(ISynsetID-sid:hypernyms){
words=dict.getSynset(sid.getWords();
系统输出打印(sid+“{”);
for(Iteratori=words.Iterator();i.hasNext();){
System.out.print(i.next().getLemma());
if(i.hasNext())
系统输出打印(“,”);
}
System.out.println(“}”);
}
}
public void getStem(字符串字)
{
//JWS=新的JWS(“C:/ProgramFiles/WordNet”,“2.1”);
wordnetstem=新的WordnetStemmer(dict);
System.out.println(“test”+stem.findStems(word,null));
}
}
dict.open()
不在任何方法或中,并且每个语句(在初始化变量时存在一些异常)都必须在java中的方法中

您应该为对象创建构造函数,并在其中初始化
dict

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
}

如果允许
MITJavaWordNetInterface
对象,则应创建构造函数,并在此处进行初始化:

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) {
        ...
    }
}
以及:

否则,创建一个静态构造函数,在其中进行初始化:

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) {
        ...
    }
}
以及:


因此,创建一个与类同名的方法?@Yamada_Tarō“与类同名的方法”-这不是一个方法,这是一个构造函数,注意这里没有返回类型,“方法”的名称实际上是类的名称-这是一个构造函数,每次创建这种类型的对象时都会调用它,看来它奏效了。你能解释一下为什么会这样吗necessary@Yamada_Tarō我在答案的开头做了,你放了一个语句
dict.open()不在任何方法中-java不“知道”何时执行此语句,因此会给您一个编译错误
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) {
        ...
    }
}
public static void main(String[] args) {
    MITJavaWordNetInterface.getHypernyms(...);
    MITJavaWordNetInterface.getStem(...);
}