如何在一个类中使用变量/在另一类中使用方法?

问题描述 投票:-2回答:2

谁能帮我,如何将一个方法中一个类内的变量传递给另一类,以便我可以继续使用它?我的意思是例如下面图片中的代码中的timeValue。非常感谢,即使我努力尝试,我也无法使其正常工作。

package chattie;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class ChatReader {

    public List<String> read(File file) {
        // public List<String> timeValue;
        File path = file;
        List<String> timeValue = null;

        try (Stream<String> lines = Files.lines(Paths.get(String.valueOf(path)))) {
            String content = lines.collect(Collectors.joining(System.lineSeparator()));
            String deleter = content.replaceAll("Time:", "").replaceAll("Name:", "").replaceAll("Message:", "");
            String line = deleter;
            String[] completeArray = line.split("\n");

            int l = completeArray.length;
            String[] arrayOnlyValues = new String[l];

            for (int i = 0; i < completeArray.length; ++i) {
                arrayOnlyValues[i] = completeArray[i];
            }

            List<String> arrayList = new ArrayList<String>(Arrays.asList(completeArray));
            arrayList.removeAll(Arrays.asList("", null));

            List<String> list1 = arrayList;
            int[] t = {0};
            timeValue = list1.stream()
                    .filter(x -> t[0]++ % 3 == 0)
                    .collect(Collectors.toList());
            System.out.println(timeValue);

            int[] n = {2};
            List<String> list2 = arrayList;
            List<String> nameValue = list2.stream()
                    .filter(x -> n[0]++ % 3 == 0)
                    .collect(Collectors.toList());
            System.out.println(nameValue);

            int[] m = {1};
            List<String> list3 = arrayList;
            List<String> messageValue = list3.stream()
                    .filter(x -> m[0]++ % 3 == 0)
                    .collect(Collectors.toList());
            System.out.println(messageValue);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
java class variables
2个回答
0
投票

您可以从方法中返回timeValue

public List<String> read(File file) {
    List<String> timeValue = null;

    ...  // instantiate timeValue

    return timeValue;
}

然后从另一个类/方法中调用它:

class foo {
    public void getTimeValue() {
        List<String> timeValue = ChatReader().read(file);
    } 
}

0
投票

Hii将value声明为类的成员,而不是method()的成员。您的代码应如下所示:

  class aaa    {
        String value="good luck";
         String txt="Best of luck";
       void mthod(){
                               }
                 }
 class bbb    {
        aaa obj=new aaa();
         bbb(){//constructor
         System.out.println(obj.value);
 }
     public static void main(String args[]){
    bbb obj2=new bbb();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.