【问题标题】:Java - Pass on Variables to Method via ConstructorJava - 通过构造函数将变量传递给方法
【发布时间】:2018-04-08 18:20:30
【问题描述】:

是否可以通过构造函数在方法中发送我需要的变量?

我有 2 个文件:

public class Recording {

public int height;
public int diameter;
public int weight;

public Recording (int height, int diameter, int weight) {
    this.height = height;
    this.diameter = diameter;
    this.weight = weight;
    }
}

public class Leergut {

public static void main(String[] args) {

    int height;
    int diameter;
    int weight;
    int code;
    int pfand;

    while (code != -1) {

        Recording r = new Recording(System.in.read(), System.in.read(), System.in.read());
        classify(r);
    } else {
        // to be continued
    }

}
public static int classify(Recording r) {

    if (height == 250 && diameter = 67 && weight == 365) { code = 0; return code;}

    else { code = -1; return code ;}
}

我正在寻找一种将高度、直径和重量(如 Recording 类中声明的)传递给“分类”方法的方法。

或者我是否需要在方法中再次声明变量,以使其工作?

PS:对于那些试图在我的第一个帖子中帮助我的人,我决定发布一个新帖子,因为它可以提供更好的概述。

【问题讨论】:

  • 投了反对票,因为这个问题表明对基本语言特征没有理解。您应该阅读一些面向初学者的 Java 教程或 Java 书籍。

标签: java object methods


【解决方案1】:

您可以在 Recording 中为高度、直径和宽度变量创建 getter 和 setter 方法。然后,在您的分类方法中调用它们。

例子:

// Methods in Recording.java
public int getWidth() {
    return this.width;
}
public int getDiameter()
    return this.diameter;
}
public int getHeight() {
    return this.height;
}

而在分类方法中,只需从 Recording 中获取值:

public static int classify(Recording r) {

    if(r.getHeight() == 250 && r.getDiameter() == 67 && r.getWidth() == 365) {
        // Do whatever
    }
}

【讨论】:

    【解决方案2】:

    这是一个建议

    你必须初始化变量,然后创建3个返回各自值的方法

    public class Recording {
    
         private int height = 0;
         private int diameter = 0;
         private int weight = 0;
    
         public Recording (int height, int diameter, int weight) {
    
             this.height = height;
             this.diameter = diameter;
             this.weight = weight;
        }
    
        public int getHeight()
        {
             return height;
        }
    
        public int getDiameter()
        {
             return diameter;
        }
    
        public int getWeight()
        {
             return weight;
        }
    }
    

    public static int classify(Recording r) {
    
        if (r.getHeight() == 250 && r.getDiameter() == 67 && r.getWeight() == 365) { code = 0; return code;}
    
        else { code = -1; return code ;}
    }
    

    【讨论】:

    • 不需要使用getter和setter,但强烈推荐,因为它们在java中被认为是干净的代码
    • @tung 这就是为什么我说这是一个建议
    【解决方案3】:

    你是说这个吗?

        public static int classify(Recording r) {
    
        if (r.height == 250 && r.diameter = 67 && r.weight == 365) { 
            code = 0; return code;
        }
        else { 
            code = -1; return code ;
        }
    

    您的 Recording 类及其字段是公开的,这意味着您只需调用它们的名称、点和您想要获取的字段即可访问它们。

    【讨论】:

    • 试过了,看起来也很棒!获取“Recording r = new Recording(System.in.read(), System.in.read(), System.in.read());”的 IOException尽管。必须有一种方法可以将 int 值读入对象。
    【解决方案4】:

    为了将变量传递给一个方法,你可以这样:

    public void myMethod(int variable1, boolean variable2) {
        //method body
    }
    

    你可以这样称呼它:

    public static void main(String[] args){
         int a = 2;
         boolean f = false;
         myMethod(a,f);
         // If static do:
         // ClassThatHasTheStaticMethod.myMethod(a,f);
    }
    

    附注:

    while (code != -1) {
        Recording r = new Recording(System.in.read(), System.in.read(), System.in.read());
        classify(r);
    } else {
        // to be continued
    }
    

    你不能在 while 代码块后面加上 else 语句,因为它会导致编译错误。

    【讨论】:

      【解决方案5】:

      尝试了一种新方法。再次声明方法内部的变量(无法弄清楚如何使用“System.in.read()”调用Setter函数

      public static int classify(Recording r) {
      
          int height = System.in.read();
          int diameter = System.in.read();
          int weight = System.in.read();
      
          if (height == 250 && diameter == 67 && weight == 365) {int code = 0; return code;}
      
          else { int code = -1; return code ;}
      }
      

      但是,现在我的对象“Recording r”告诉我变量无法再解析了 和 System.in.read();对于方法中的 3 个变量,抛出某种 IOException。

      【讨论】:

        猜你喜欢
        • 2016-05-23
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 2015-02-22
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多