如何对非静态方法进行静态引用?

huangapple go评论82阅读模式
英文:

How do i make a static reference to a non-static method?

问题

import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    ArrayList<String> original = new ArrayList<>(Arrays.asList("afd", "asdf", "aavdd", "sajf", "adnf", "afd", "fjfn"));
    String find = "afd";
    String replaceWith = "asd";
    System.out.println(replaceIt(original, find, replaceWith));
  }

  public ArrayList<String> replaceIt(ArrayList<String> original, String find, String replaceWith) {
    ArrayList<String> newList = new ArrayList<String>();
    for (int y = 0; y < original.size(); y++) {
      if (!original.get(y).equals(find))
        newList.add(original.get(y));
      else
        newList.add(replaceWith);
    }

    original = newList;
    return original;
  }
}

如何调用 replaceIt 方法?我很困惑,我需要使它打印出该函数的输出。我很困惑,请有人帮帮我。

英文:
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    ArrayList&lt;String&gt; original = new ArrayList&lt;&gt;(Arrays.asList(&quot;afd&quot;, &quot;asdf&quot;, &quot;aavdd&quot;, &quot;sajf&quot;, &quot;adnf&quot;, &quot;afd&quot;, &quot;fjfn&quot;));
    String find = &quot;afd&quot;;
    String replaceWith = &quot;asd&quot;;
    System.out.println(replaceIt(original, find, replaceWith));

  }
  public ArrayList&lt;String&gt; replaceIt(ArrayList&lt;String&gt; original, String find, String replaceWith){
      ArrayList&lt;String&gt; newList = new ArrayList&lt;String&gt;();
      for(int y = 0; y&lt;original.size(); i++){
        if(!original.get(y).equals(find))
          newList.set(y, original.get(y));
        newList.set(y, replaceWith);
      }
      
      original = newList;
      return original;
  }
}

How do i call the replaceIt method? I'm confused and I need to make it so it prints the output of that function. I'm so confused somebody please help.

答案1

得分: 0

* `public ArrayList<String> replaceIt()` 是一个实例方法仅从 `Main` 实例/对象中调用
* `public static void main``Main` 类的静态方法静态方法无法直接访问实例方法和实例变量

因此要从静态 main 方法调用 `replaceIt()` 方法请将 `replaceIt()` 声明为静态方法

    public static ArrayList<String> replaceIt(/*参数*/){
        // 你的代码在这里
    }
英文:
  • public ArrayList&lt;String&gt; replaceIt() is an instance method, it is only called from Main instances/objects.
  • public static void main is a static method of class Main, static methods can’t access instance methods and instance variables directly.

Therefor, to call replaceIt() method from static main method, make replaceIt() static.

public static ArrayList&lt;String&gt; replaceIt(/*arguments*/){
    //your code goes here
}

答案2

得分: 0

Main中使用非静态方法有点奇怪。这并不像你要构造Main的新实例一样(比如,你构造Person类的新实例的方式)。

这种类型的通用辅助/工具函数通常放在名为Utils.javaHelpers.java的类中,并且在这些文件中声明为静态方法。这样你就可以调用:Utils.replaceIt(original, find, replaceWith));

英文:

Having non-static method in Main is a little odd. It's not like you are going to construct new instances of Main (say, the same way you would construct new instances of a Person class).

These type of generic helpers/utils functions usually go into some sort Utils.java or Helpers.java class, and are declared as static in these files. This way you would be able to invoke: Utils.replaceIt(original, find, replaceWith));

答案3

得分: 0

因为replaceIt()是一个实例方法,您只需要创建Main类的一个实例来调用它 -

public static void main(String[] args) {  
    ...  
    Main m = new Main();  
    System.out.println(m.replaceIt(original, find, replaceWith));  
}
英文:

Because replaceIt() is an instance method, you simply need to create an instance of the Main class to call it -

public static void main(String[] args) {  
    ...  
    Main m = new Main();  
    System.out.println(m.replaceIt(original, find, replaceWith));  
}

huangapple
  • 本文由 发表于 2020年9月11日 11:28:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63840398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定