如何访问另一个类的函数中一个类的变量? [英] How do I access a variable of one class in the function of another class?
问题描述
我有一个类主(它有 public static void main(String [] args))和另一个类 MyDocument 。
在Main类中有一个变量 text
,我想从函数 alphabetOccurrence (),显示在 MyDocument 类中。我如何做到这一点?我不想使用它作为一个静态变量。并且任何更改只能在函数中完成,其余的代码应该保持不变。
import java.util。*;
class Main {
public static void main(String [] args){
MyDocument document = null;
String text;
text =早上好,早上好亚历山大,你的国家有多少人?他们都有大房子,大车?他们都吃好食物?
char letter ='d';
document = new MyDocument();
document.setDocumentText(text);
System.out.println(Letter+ letter +has occured
+ document.alphabetOccurrence(letter)+times);
}
}
class MyDocument {
private ArrayList< Character> document = new ArrayList();
public MyDocument(){
}
void setDocumentText(String s){
for(int i = 0; idocument.add(s.charAt(i));
}
ArrayList getDocumentText(){
return this.document;
}
public int alphabetOccurrence(char letter){
//在这里使用文本变量..
}
}
解决方案您应该更改
MyDocument
类添加新的String
字段以保存文本
:import java.util.ArrayList;
class MyDocument {
private String text;
private ArrayList< Character> document = new ArrayList();
public MyDocument(){
}
void setDocumentText(String s){
this.text = text;
for(int i = 0; idocument.add(s.charAt(i));
}
ArrayList< Character> getDocumentText(){
return this.document;
}
public int alphabetOccurrence(char letter){
this.text; // do something
}
}
I'm having a class Main (which has public static void main(String []args))and another class MyDocument.
There is a variable
text
present in the Main class which I want to access from a function alphabetOccurrence() present in the MyDocument class. How do I do this? I don't want to use it as a static variable. And any changes can be done only in the function, rest of the code should be untouched.import java.util.*; class Main { public static void main(String[] args) { MyDocument document = null; String text; text = "good morning. Good morning Alexander. How many people are there in your country? Do all of them have big houses, big cars? Do all of them eat good food?"; char letter = 'd'; document = new MyDocument(); document.setDocumentText(text); System.out.println("Letter " + letter + " has occured " + document.alphabetOccurrence(letter) + " times"); } } class MyDocument { private ArrayList<Character> document = new ArrayList(); public MyDocument() { } void setDocumentText(String s) { for (int i = 0; i < s.length(); i++) document.add(s.charAt(i)); } ArrayList getDocumentText() { return this.document; } public int alphabetOccurrence(char letter) { // use text variable here.. } }
解决方案You should change your
MyDocument
class to add newString
field to holdtext
:import java.util.ArrayList; class MyDocument { private String text; private ArrayList<Character> document = new ArrayList(); public MyDocument() { } void setDocumentText(String s) { this.text = text; for (int i = 0; i < s.length(); i++) document.add(s.charAt(i)); } ArrayList<Character> getDocumentText() { return this.document; } public int alphabetOccurrence(char letter) { this.text; //do something } }
这篇关于如何访问另一个类的函数中一个类的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!