简体   繁体   English

如何在另一个类的函数中访问一个类的变量?

[英]How do I access a variable of one class in the function of another class?

I'm having a class Main (which has public static void main(String []args) )and another class MyDocument . 我有一个Main类(具有公共静态void main(String [] args) )和另一个类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. 我要从MyDocument类中存在的函子AlphabetOccurrence()访问Main类中存在的可变text 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 new String field to hold text : 您应该更改MyDocument类以添加新的String字段来保存text

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

    }
}

you could pass variable text as a parameter in your function 您可以在函数中传递变量文本作为参数

public int alphabetOccurrence(char letter, String text){
    String text2 = text;
    // use text variable here...
}

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

相关问题 如何从另一个类访问一个变量? - How Do I Access One Variable From Another Class? 如何在一个Java类中更改另一个Java类中的变量,然后从第三个Java类访问该更改的变量? - How do I change a variable in one java class from another, then access that changed variable from a third java class? 我如何从另一个类访问对象变量? - How do I gain access to an objects variable from another class? 我如何从另一个类访问另一个类的void方法的变量 - How do i access variable of void method of other class from another class 如何访问另一个类中的函数? - How can I access a function in another class? 如何从一个类到同一包的另一个类访问一个变量 - How to access one variable from one class to another of the same package 如何从另一个 class 访问一个写在一个 class 中的 ArrayList? - How do I access an ArrayList written in one class from another class? Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class? 如何在另一个类中使用静态类中的变量? - How do I use a variable from a static class in another class? 如何在 JavaFX 中从一个类控制器文件访问另一个变量? - How to access a variable from one class controller file to another in JavaFX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM