简体   繁体   English

如何在不同的类Java中访问私有变量

[英]How to access private variable in different class Java

I have one class, SpellCheck, that (along with a variety of other things) creates a trie from the file name given in the Interactions window. 我有一个SpellCheck类,它(以及许多其他东西)从“交互”窗口中给定的文件名创建一个特里。 When I enter java SpellCheck small.txt for example, I know that a Lexicon/dictionary is created because it's displayed by other methods in SpellCheck. 例如,当我输入java SpellCheck small.txt时,我知道会创建一个词典/词典,因为它是由SpellCheck中的其他方法显示的。

public class SpellCheck{

 // the dictionary

 private LexiconTrie dict;


 // Constructor;  creates window elements

 public SpellCheck(String[] files) {

  dict = new LexiconTrie(files); 

I need to access the information in that LexiconTrie dict (iteration, roots, nodes, etc.) in my other class LexiconTrie. 我需要在其他类LexiconTrie中访问该LexiconTrie词典中的信息(迭代,根,节点等)。 However, whenever I try to access it (even when using SpellCheck.dict it gives me the error of "dict has private access in SpellCheck" (or something along those lines). 但是,每当我尝试访问它时(即使使用SpellCheck.dict时,它也会给我错误“ dict在SpellCheck中具有私有访问权限”(或类似的说法)。

Am I not fully understanding the private/public class interaction in this case? 在这种情况下,我是否不完全了解私人/公共阶层的互动? (Please let me know if I have not given enough information) ---------EDIT/UPDATE--------------------- (如果我没有提供足够的信息,请告知我)---------编辑/更新---------------------

This is the method in SpellCheck 这是SpellCheck中的方法

public void doAutoComplete() {
  Collection<String> arr = dict.getCompletions(curText.trim().toLowerCase(), num_value);
  String intro = "Up to " + num_value + " completions";
  display(arr, "No matches found", intro);   
}

I need to write the method getCompletions() in LexiconTrie. 我需要在LexiconTrie中编写方法getCompletions getCompletions() My professor has implemented a GUI interface for us. 我的教授为我们实现了GUI界面。 This GUI interface creates a search bar that calls doAutoComplete whenever the user types a letter into the search bar. 该GUI界面创建一个搜索栏,只要用户在搜索栏中键入字母, doAutoComplete就会调用doAutoComplete In LexiconTrie, I am supposed to write getCompletions to return matches("completions") found in LexiconTrie dict (which is created and stored privately in SpellCheck). 在LexiconTrie中,我应该编写getCompletions以返回在LexiconTrie dict (在LexiconTrie dict私下创建和存储)中找到的LexiconTrie dict (“ completions”)。

I saw that as writing the method along these lines: 我在写这些方法时看到了这些:

// Method you have to implement
public Collection<String> getCompletions(String lowerCase, int max) {
  String searchWord = lowerCase;
  int maxReturned = max;
  LinkedList trieMatches = new LinkedList();


  // this is as far as I got, it won't even print the search word. 
 // I get an error from trying to look into dict with .containsWord
  if (dict.containsWord(searchWord)){
    System.out.println(searchWord); 
    //add word/prefix to LinkedList
    //add all children and their children up until max#
  }

  else{
    return null;
  }
}

I need to access the information in that LexiconTrie dict (iteration, roots, nodes, etc.) in my other class LexiconTrie. 我需要在其他类LexiconTrie中访问该LexiconTrie词典中的信息(迭代,根,节点等)。

That seems weird (hard to tell without seeing more code, though). 这似乎很奇怪(尽管很难看到更多代码,但是很难告诉)。

Normally instead of accessing another instance of LexiconTrie, you would want to operate on this . 通常,您想this进行操作,而不是访问LexiconTrie的另一个实例。

So instead of 所以代替

void someMethod(SpellCheck spellCheck){
    spellCheck.dict.something();
}

you would have 你将会拥有

void someMethod(){
    something();
}
// called from spellcheck as
this.dict.someMethod();

create a method for accessing the private variable: 创建用于访问私有变量的方法:

public LexiconTrie getDict(){
  return dict;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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