简体   繁体   English

在Java中的不同类中使用函数,而无需将其作为方法收集在类中

[英]Use Functions in different classes in Java without collecting it in a class as methods

I have some functions collected as methods in a class called func . 我在一个名为func的类中收集了一些作为方法的func I use those methods quite often in different classes. 我经常在不同的类中使用这些方法。 Is there a way in java to call those functions without creating a new object or calling it like this: func.myFunction(); java中有没有一种方法可以调用这些函数,而无需创建新对象或像这样调用它: func.myFunction(); ?

I want: myFunction(); 我想要: myFunction();

There are two issues: 有两个问题:

  • you can't call non-static methods without having an instance of that class and 如果没有该类的实例,就不能调用非静态方法, 并且
  • you can't directly call static methods without mentioning the class name. 您不能不提及类名就直接调用静态方法。

The first one is easy to solve: if your method doesn't actually use any non-static members (fields or methods) of the class, then simply adding the static keyword enables you to call it without an instance of the class: 第一个很容易解决:如果您的方法实际上并未使用该类的任何非静态成员(字段或方法),则只需添加static关键字即可使您无需类的实例即可调用它:

// in YourClass:
public static void yourMethod() {
  //stuff
}

// somewhere else:
YourClass.yourMethod();

And about the second item: I kind-of lied there, you can do that, but you need a static import: 关于第二项:我有点撒谎了, 可以这样做,但是需要静态导入:

// at the beginning of your .java file
import static yourpackage.YourClass.yourMethod;

// later on in that file:
yourMethod();

Also: there are no functions in Java, only methods. 另外:Java中没有函数,只有方法。

Yes, you can mark the methods as static , and use static imports . 是的,您可以将这些方法标记为static ,并使用static import

Example: 例:

pkg/UtilFunctions.java 包装/ UtilFunctions.java

package pkg;

public class UtilFunctions {

    public static int sum(int i1, int i2) {
        return i1 + i2;
    }
}

Test.java Test.java

import static pkg.UtilFunctions.*;

class Test {
    public static void main(String[] args) {

        int i = sum(5, 7);     // Calls UtilFunctions.sum(...)

    }
}

Make them static like public static int myFunction(); 使它们像public static int myFunction();静态一样public static int myFunction(); and then make static import: import static myclass : 然后进行静态导入: import static myclass

Foo.java:
class Foo {
    public static int method() {return 42;}
    ...
}
Bar.java
import static Foo.*;
...
System.out.println(method());

You can use a static import. 您可以使用静态导入。

For example this: 例如:

import java.lang.Math;
...
System.out.println(Math.abs(-1.4));

and this: 和这个:

import static java.lang.Math;
...
System.out.println(abs(-1.4));

produce the same result. 产生相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java - 从方法返回不同的类并使用类的特定功能 - Java - Return different classes from method and use specific functions of the classes Java - 概括不同的类,类似的方法(不改变子类?) - Java - Generalize different classes, similar methods (without changing the subclasses?) 如何使用具有相同方法但没有父类的 2 个类? - How to use 2 classes having same methods but without a parent class? 如何在没有getter方法的情况下使用java中的数据类? - How to use data classes in java, without getter Methods? MyClass.java中类的收集方法,属性和名称 - Collecting Methods, Attributes and name of Class in MyClass.java 我如何使用JUnit在一个Java类中对测试方法进行单元化而又没有在模块中完成其他类? - How do I use JUnit to unit test methods in one Java class, without having completed other classes in the module? Java抽象使用instanceof从List收集类 - Java abstracting the use of instanceof for collecting classes from List Java垃圾收集引用类 - Java Garbage Collecting Reference Classes 如何在Java中使用来自不同类的方法? - How can I use methods from a different class in Java? 使用类名从不同的类调用方法 - Calling methods from different classes with the class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM