从Java方法返回多个变量 [英] Return more than one variable from Java method

查看:149
本文介绍了从Java方法返回多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉愚蠢的问题... 有人可以帮我从方法中返回两个变量吗(我读了

Sorry for stupid question... Could anybody help me with returning two variable from method (I read here and tried to recode but without result).

public class FileQualityChecker {
    EnviroVars vars = new EnviroVars();  
    public int n = 0;
    public int z = 0;
    public int m = 0;
    String stringStatus;


    public void stringLenghtCheck(String pathToCheckedFile)
    {
        try{    
    FileInputStream fstream = new          FileInputStream(pathToCheckedFile+"\\"+"Test.dat");
         // Get the object of DataInputStream
         DataInputStream in = new DataInputStream(fstream);
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
         String strLine;
          //Read File Line By Line
         while ((strLine = br.readLine()) != null)   
         {

           if  (
           strLine.length() == vars.strLenghtH ||
           strLine.length() == vars.strLenghtCUoriginal ||
           strLine.length() == vars.strLenghtCEAoriginal ||
           strLine.length() == vars.strLenghtCAoriginal ||
           strLine.length() == vars.strLenghtTrailer ||
           strLine.length() == vars.strLenghtLastRow 

           ) 
            {
               stringStatus = "ok";
               n++;
               z++; 

            }
           else 
           {
                stringStatus = "Fail";
                n++; 
                m++;
                }
           System.out.println (n +" " + strLine.length() +" " + stringStatus);
          }



         //Close the input stream
          in.close();
        }
        catch
        (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
         }

         /*How to return m and z from method to use these vars for writing in the file*/
         return (m, z);


        }

}

我需要在另一个类中将m和z写入文件.谢谢.

I need m and z in another class for write them into file. Thank you.

推荐答案

我遇到这类问题的第一个问题是,这两个结果之间有什么关系?

My first question with these sort of issues is, what's the relationship between these two results ?

如果它们不相关,那是否表示您的方法做了两件事?

If they're unrelated, does that point to your method doing two different things ?

如果它们是相关的(在这种情况下它们似乎是),则将它们包装在一个自定义对象中.这使您有机会稍后在该对象中添加更多结果,并且您可以将行为附加到该对象.

If they're related (they appear to be in this case), then wrap them in a custom object. This gives you the chance to add more results later into this object, and you can attach behaviour to this object.

您的另一种解决方案是将回调对象传递给此方法,因此您根本不返回任何内容,而是您的方法随后在该对象上调用一个方法,例如

Your other solution is to pass a callback object into this method, so you don't return anything at all, but rather your method then calls a method on this object e.g.

// do some processing
callbackObject.furtherMethod(m, n);

这具有遵循OO原理的优点,即使对象为您做事情,而不是要求他们自己做信息.

This has the advantage of following the OO principle of getting objects to do things for you, rather than asking them for info and doing it yourself.

这篇关于从Java方法返回多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆