简体   繁体   中英

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. 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.

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. 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).

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

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. My professor has implemented a GUI interface for us. This GUI interface creates a search bar that calls doAutoComplete whenever the user types a letter into the search bar. In LexiconTrie, I am supposed to write getCompletions to return matches("completions") found in LexiconTrie dict (which is created and stored privately in SpellCheck).

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.

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 .

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

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