Android / Java - 如何调用单独的 *.java 文件中的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3496540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:06:33  来源:igfitidea点击:

Android / Java - How do I call upon a function in a separate *.java file?

javaandroid

提问by John

I do an import of the full package name / java file, and if I do a <classname>.<method>, SOMETIMES I can get it to access - other times I get a lot of can't use a static in a non staticbunch of talk.

我导入了完整的包名/java 文件,如果我导入了<classname>.<method>,有时我可以访问它 - 其他时候我会得到很多can't use a static in a non static谈话。

I'll admit I'm new to Java, so what do I need to do? Call a class instance first, then call my methods? I'm rather confused by this, as I want to put all of my 'functions' into a FunctionsList.java file, and all of my main Activity (UI) into a MyActivity.java file.

我承认我是 Java 新手,所以我需要做什么?先调用一个类实例,然后调用我的方法?我对此感到很困惑,因为我想将所有“功能”放入 FunctionsList.java 文件中,并将所有主要活动 (UI) 放入 MyActivity.java 文件中。

For example:

例如:

<MyActivity.java>

import com.example.FunctionsList;

private class MyActivity extends Activity {
  FunctionsList.function();
}

9/10 times I get that static/non-static error.

9/10 次我得到静态/非静态错误。

If I put all of my functions into MyActivity.java, I have zero problems! Anyone help me on what I presume is a basic Java newbie issue?

如果我把我所有的函数都放到 MyActivity.java 中,我的问题就为零了!任何人都可以帮助我解决我认为是基本 Java 新手问题的问题?

回答by bporter

Here's an example that will hopefully help you out a little.

这是一个示例,希望能对您有所帮助。

public class MyFunctionClass {

   public String myFunction() {
      return "This is an instance function.";
   }

   public static String myStaticFunction() {
      return "This is a static function.";
   }

}

Then in your activity you have something like this.

然后在你的活动中你会有这样的事情。

public class MyActivity extends Activity {

   @Override
   public void onCreate() {

      // If you want to call your static function, you do not
      // require an instance of a MyFunctionClass object.
      String myStaticString = MyFunctionClass.myStaticFunction();

      // If you want to call your instance function, then you need
      // to create a MyFunctionClass first.
      MyFunctionClass variableName = new MyFunctionClass();
      String myInstanceString = variableName.myFunction();
   }
}

As Jon mentioned, you'll probably save yourself some frustration if you read up on object-oriented programming before diving in. There are some basic things that a new programmer will need to understand before diving in. Good luck!

正如 Jon 所提到的,如果您在深入研究面向对象编程之前先阅读一下,您可能会避免一些挫折。在深入研究之前,新程序员需要了解一些基本知识。祝您好运!

回答by Jon Skeet

If you want to use a non-static method, you have to have an instance of the class to call the method on. If you want to use a static method, you don't need an instance.

如果要使用非静态方法,则必须有一个类的实例来调用该方法。如果要使用静态方法,则不需要实例。

As an example, suppose you tried to call String.length()- what could that return? It's trying to find the length of something, but you haven't specified whichstring you're interested in. The same is true for other instance methods - the results will usually depend on which object you're calling them on, which is why they're instance methods to start with.

举个例子,假设你试图打电话String.length()- 那会返回什么?它试图找到的长度的东西,但是你没有指定哪个字符串你感兴趣这同样适用于其它实例方法真-的结果通常将取决于反对你在打电话给他们,这就是为什么它们是开始的实例方法。

As an aside, I would stronglyrecommend you to learn the basics of Java first, beforetrying to use Android. That way when you get into genuinely tricky problems, you won't have to wonder whether it's part of Android or whether it's a simple Java error. See my answer on a related questionfor more advice about this.

顺便说一句,我强烈建议您尝试使用 Android之前先学习 Java 的基础知识。这样,当您遇到真正棘手的问题时,您就不必怀疑它是 Android 的一部分还是简单的 Java 错误。有关此问题的更多建议,请参阅我对相关问题的回答