java 继承于一个 class,将变量部署到两个函数
java inheritance within one class, deploy variables to two functions
如何使我在此 class 顶部定义的变量应用于其中的两个函数?
特别是 dict 和 url。
现在 eclipse 告诉我,关于 dict.open()
一个 "identifier is expected after it",但我认为这是一个转移注意力的问题,因为如果我将它移回到 getHypernyms
方法中它会再次工作。我可以将该代码复制到这两种方法中,但那太愚蠢了,而且风格太差了。必须有一种更优雅的方法来实现这一点。
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();
不在任何方法或 constructor 中,每个语句(变量初始化有一些例外)必须在 java.[=14 中的方法中=]
您应该为您的对象创建一个构造函数,并在其中初始化 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 static void main(String[] args) {
MITJavaWordNetInterface m = new MITJavaWordNetInterface();
m.getHypernyms(...);
m.getStem(...);
}
否则,创建一个静态构造函数,在其中进行初始化:
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(...);
}
如何使我在此 class 顶部定义的变量应用于其中的两个函数?
特别是 dict 和 url。
现在 eclipse 告诉我,关于 dict.open()
一个 "identifier is expected after it",但我认为这是一个转移注意力的问题,因为如果我将它移回到 getHypernyms
方法中它会再次工作。我可以将该代码复制到这两种方法中,但那太愚蠢了,而且风格太差了。必须有一种更优雅的方法来实现这一点。
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();
不在任何方法或 constructor 中,每个语句(变量初始化有一些例外)必须在 java.[=14 中的方法中=]
您应该为您的对象创建一个构造函数,并在其中初始化 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 static void main(String[] args) {
MITJavaWordNetInterface m = new MITJavaWordNetInterface();
m.getHypernyms(...);
m.getStem(...);
}
否则,创建一个静态构造函数,在其中进行初始化:
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(...);
}