Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中是否存在对同一行代码的多次调用?_Java_Methods_Refactoring_Local Variables - Fatal编程技术网

在Java中是否存在对同一行代码的多次调用?

在Java中是否存在对同一行代码的多次调用?,java,methods,refactoring,local-variables,Java,Methods,Refactoring,Local Variables,我希望能够做到的是只编写一次多行代码,并将其复制粘贴到多个方法中 我可以简单地复制粘贴,但这会使数字翻倍 大量的代码行,使其更难阅读 我可以创建一个方法 它包含许多行代码,然后调用该方法 每当我需要这些线;然而,这意味着我不能 使用我声明的局部变量 A methodA(Object object) { doC(); // I cannot do additional things with the local variables declared in C } B method

我希望能够做到的是只编写一次多行代码,并将其复制粘贴到多个方法中

  • 我可以简单地复制粘贴,但这会使数字翻倍 大量的代码行,使其更难阅读
  • 我可以创建一个方法 它包含许多行代码,然后调用该方法 每当我需要这些线;然而,这意味着我不能 使用我声明的局部变量

    A methodA(Object object) {
        doC();
        // I cannot do additional things with the local variables declared in C
    }
    
    B methodB(Object object) {
        doC();
    }
    
    doC() {
        // Delcare and use a bunch of local variables
    }
    
  • 我的具体问题是,我需要从它调用的方法访问局部变量。如果有一个类似于一个充满常量的类,而不是一个可以从任何地方访问的充满代码行的类,那就太好了

    A methodA(Object object) {
        B b = extractedMethod(object);
        A a = new A(b);
        Tools.staticMethod(a, file); // file cannot be resolved
        return A;
    }
    
    B extractedMethod(Object object) {
        // A lot of code...
        final File file = getSelectedFile();
        // More code...
        return B;
    }
    
    目的
    最初,只有methodA存在,该方法检索一个URI并将其包装在一个自定义对象中,然后在调用一个方法来更新一些进度图标后返回该URI。然而,我现在想创建一个methodB,它只返回那个URI,因为我不需要将它包装在自定义对象中。当我只需要从原始methodA中删除三行代码就可以使它工作时,我认为创建一个新类是非常无关的。本质上,我是在寻找某种类似于代码模板的功能,但在编译时,这样我就可以键入“xyz”来替换我想要替换的“很多行”(大约20行)。我现在明白了代码复制的危害。

    如果您想访问局部变量以进行只读,请将其值作为参数传递。如果还需要写访问,请将它们转换为实例变量(当前类中的任意一个),或者最好创建一个新类。

    如果要访问局部变量以进行只读,请将其值作为参数传递。如果还需要写访问权限,请将它们转换为实例变量-当前类中的任意一个,或者最好创建一个新类。

    方法1

    class ABC{
    // a , b , c are variables of any type
    
    A methodA(Object object) {
        doC();
        // I cannot do additional things with the local variables declared in C
      // use a b c here 
    }
    
    B methodB(Object object) {
        doC();
      // use a b c here 
    }
    
    doC() {
        // Delcare and use a bunch of local variables
      // use a b c here 
    }}
    
    方法2

    class C {
    
     // a, b ,c 
     // list<Object> abcList = new arrayList<Object>;
    
    doC() {
            // Delcare and use a bunch of local variables
          // use a b c here 
    
    // abcList.add(a);
    // abcList.add(b);
    // abcList.add(c);
    
    A methodA(Object object, abcList);
    B methodB(Object object, abcList)
    
    }
    
    }
    
    class AB {
    
     A methodA(Object object, List<Object> abcList) {
            doC();
            // I cannot do additional things with the local variables declared in C
          // use a b c here 
          // get value of a,b,c from abcList;
        }
    
        B methodB(Object object, List<Object> abcList) {
            doC();
          // use a b c here 
    // get value of a,b,c from abcList;
        }
    
    }
    
    C类{
    //a、b、c
    //list abcList=新数组列表;
    文件({
    //删除并使用一组局部变量
    //在这里使用b c
    //abcList.添加(a);
    //abcList.添加(b);
    //增加(c);
    A方法A(对象,abcList);
    方法B(对象对象,abcList)
    }
    }
    AB类{
    方法A(对象,列表abcList){
    doC();
    //我不能用C中声明的局部变量做其他事情
    //在这里使用b c
    //从abcList中获取a、b、c的值;
    }
    方法B(对象对象,列表abcList){
    doC();
    //在这里使用b c
    //从abcList中获取a、b、c的值;
    }
    }
    
    方法1

    class ABC{
    // a , b , c are variables of any type
    
    A methodA(Object object) {
        doC();
        // I cannot do additional things with the local variables declared in C
      // use a b c here 
    }
    
    B methodB(Object object) {
        doC();
      // use a b c here 
    }
    
    doC() {
        // Delcare and use a bunch of local variables
      // use a b c here 
    }}
    
    方法2

    class C {
    
     // a, b ,c 
     // list<Object> abcList = new arrayList<Object>;
    
    doC() {
            // Delcare and use a bunch of local variables
          // use a b c here 
    
    // abcList.add(a);
    // abcList.add(b);
    // abcList.add(c);
    
    A methodA(Object object, abcList);
    B methodB(Object object, abcList)
    
    }
    
    }
    
    class AB {
    
     A methodA(Object object, List<Object> abcList) {
            doC();
            // I cannot do additional things with the local variables declared in C
          // use a b c here 
          // get value of a,b,c from abcList;
        }
    
        B methodB(Object object, List<Object> abcList) {
            doC();
          // use a b c here 
    // get value of a,b,c from abcList;
        }
    
    }
    
    C类{
    //a、b、c
    //list abcList=新数组列表;
    文件({
    //删除并使用一组局部变量
    //在这里使用b c
    //abcList.添加(a);
    //abcList.添加(b);
    //增加(c);
    A方法A(对象,abcList);
    方法B(对象对象,abcList)
    }
    }
    AB类{
    方法A(对象,列表abcList){
    doC();
    //我不能用C中声明的局部变量做其他事情
    //在这里使用b c
    //从abcList中获取a、b、c的值;
    }
    方法B(对象对象,列表abcList){
    doC();
    //在这里使用b c
    //从abcList中获取a、b、c的值;
    }
    }
    
    在过程编程的范例中(以及在OOP中),这样的事情不仅是不可能的,而且是错误的。 如果您需要访问代码块外部的局部变量,这意味着您以错误的方式构造了整个代码块;事实上,有两种可能性:

  • 要在局部变量上完成的操作在逻辑上是包含此类变量的块的一部分。在这种情况下,只需扩展它
  • 您需要包含局部变量的块之外的局部变量的值,以便每次执行不同的操作(我知道这是您的情况)。在这种情况下,变量在逻辑上是包含它们的块的执行结果的一部分。一种干净、正确的处理方法是封装所述代码块,并返回一个结构化数据类型,其中包含您需要在其外部访问的所有值
    在过程编程的范例中(在OOP中也是如此),这样的事情不仅是不可能的,而且是错误的。 如果您需要访问代码块外部的局部变量,这意味着您以错误的方式构造了整个代码块;事实上,有两种可能性:

  • 要在局部变量上完成的操作在逻辑上是包含此类变量的块的一部分。在这种情况下,只需扩展它
  • 您需要包含局部变量的块之外的局部变量的值,以便每次执行不同的操作(我知道这是您的情况)。在这种情况下,变量在逻辑上是包含它们的块的执行结果的一部分。一种干净、正确的处理方法是封装所述代码块,并返回一个结构化数据类型,其中包含您需要在其外部访问的所有值
    “我想做的是只编写一次多行代码,然后复制并粘贴到多个方法中。”-不,这是一个糟糕的计划。你到底为什么要这么做?为什么不在需要时简单地调用该方法呢?听起来你想创建一个带有字段的类,而不是一个带有局部变量的方法。使用继承。调用super以获得通用功能,并在重写的方法中使用局部变量。还要注意“//大量代码…”不是一个好做法。你应该把你的方法分成几个小单位。你的设计听起来很糟糕。我不明白为什么要投反对票。这个问题很明确,尽管它的目的有些争议。最好提供一种重新设计的方法。”我想做的是只编写一次多行代码,然后将其复制粘贴到m中