【问题标题】:Passing arguments, good and bad practice in Java/OOP programming传递参数,Java/OOP 编程中的好坏习惯
【发布时间】:2014-05-20 08:33:22
【问题描述】:

我最近开始用 Java 编写 android 应用程序,我对 Java 完全陌生,我在大学时用 C++ 完成了面向对象编程的基础知识。
我的问题是,通过时有什么好的和坏的做法Java中不同方法的可变数据?例如,我在代码中一直在做的事情是:

String one;
String two;
String three;
String four;

exampleOne(one, two, three, four);

exampleOne(String one, String two, String three, String four) {
      // do something
      exampleTwo(one, two, three, four);
}

exampleTwo(String one, String two, String three, String four) {
      // do something
      exampleThree(one, two, three, four);
}

exampleThree(String one, String two, String three, String four) {
      // do something
}

在我的代码中,我做了这样的事情,我最多传递参数 5 次,这样做是不好的做法吗?什么是更清洁更环保的选择?

【问题讨论】:

    标签: java oop methods arguments


    【解决方案1】:

    如果有很多参数并且它们会被多次传递,我会选择 DTO 对象。

    创建一个封装这些参数并在方法之间传递实例的 Pojo 类。您也可以在 DTO 中添加一些辅助函数/方法,以简化一些处理。

    【讨论】:

    • 好主意,这会让事情变得更干净、更容易
    【解决方案2】:

    这样声明一个类会很有用:

    public class YourClass{
    
        private String one;
        private String two;
        private String three;
        private String four;
    
        public YourClass(String one, String two, String three, String four){
            this.one = one;
            this.two = two;
            this.three = three;
            this.four= four;
        }
    
        public void exampleOne() {
              // do something
              exampleTwo();
        }
    
        public void exampleTwo() {
              // do something
              exampleThree();
        }
    
        public void exampleThree() {
              // do something
        }
    
    }
    

    并使用它:

    YourClass c = new YourClass(one, two, three, four);
    c.exampleOne();
    

    【讨论】:

      【解决方案3】:

      您所做的并没有错,但您可以使用可变参数(如 TAsk 所述)或通过创建一个表示一组参数的小型容器类(如 c 结构,通常称为 bean)来澄清问题。

      这使您可以整理事物并使代码更具可读性。 请注意,在创建类时,由于新的分配,您会引入一些开销,而 c-struct 并非如此,因为它是管理对结构成员的引用的编译器。

      参数像c中一样在堆栈中传递,java中没有引用参数的概念,使用bean可以克服这个限制。

      【讨论】:

      • 很高兴知道,关于多次传递参数的感觉并不是一种非常干净的做事方式
      【解决方案4】:

      嗯,当你想调用具有某些属性的方法时需要传递参数,但对于大量相同类型的参数,你可以使用以下。

      您可以改用VarArgs

      public void Method(String.. str) {
         //Here you will have Array  str[](Of String)  
        if(str!=null)
         for (String s: str) 
          System.out.println(s);//Iterate through Array for More Processing
      
      }
      

      如果你也想传递其他参数

       Method(int i, String... Other) {
          //VarArgs Must be Last
        }
      

      注意: 传递不同类型的参数并使用转换方法将 String 转换为 Double、Int 等。(不建议这样做但可以这样做,因为您需要确定在哪个地方传递了 double、int 等)

      【讨论】:

      • 如果你传递不同类型的参数怎么办?
      • 这可能很方便,但有时不方便,因为您必须以数组方式检索每个元素。
      • @Kent 是的,同意!!但是正如 OP 所说的这种方法,我可以说比 OP 的代码更方便。
      • 检查str != null 是否在没有参数的情况下调用该方法,否则你会得到一个npe (NullPointerException)
      • @TAsk 我并不是说您的回答不方便。这取决于要求。许多框架也使用它,例如当您将参数值传递给 sql 查询,或打印带有某些参数的日志等时。但是在两个方面可能不方便,1)方法调用者必须根据序列顺序仔细准备数组; 2)方法被调用者必须通过索引和(可能)使用类型转换来获取值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多