简体   繁体   English

如何在不同的Java项目中使用相同的方法

[英]How do I use same method in different Java projects

I use method A in several different Java projects. 我在几个不同的Java项目中使用方法A。 Is there a way to write the method just once and then call it as required from different Java projects? 有没有一种方法可以只编写一次该方法,然后在不同的Java项目中根据需要调用它?

Create a class with that method. 使用该方法创建一个类。 Create a jar for this class. 为该类创建一个jar。 Use this jar in all your projects. 在所有项目中都使用此jar。

Yes. 是。 Define public class and define at least public or protected method. 定义public类,并至少定义publicprotected方法。 Then import that class in your new project's class and use it. 然后将该类导入新项目的类中并使用它。
default classes with public or protected methods are not accessible in different projects/packages even if default class' methods are public/protected . default班, publicprotected方法不是在不同的项目进入/包即使默认类的方法都是public/protected
So use public class and public or protected methods and at last create a jar with this file and add this jar file in your project to use. 因此,请使用公共类和公共或受保护的方法,最后使用此文件创建一个jar,并将此jar文件添加到您的项目中以供使用。

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

相关问题 如何在java中使用来自不同方法的变量? - How do I use variables from different method in java? 我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇) - How can i use the return value of a private static method (extension of object) in a different method in that same class? (java) 如何在3个不同的Java插件项目的3个不同的osgi包中使用同一对象 - How to Use the same object among 3 different osgi bundles that is 3 different java plugin projects 如何在多个Java项目中使用同一记录器 - How to use the same logger in multiple java projects 我该如何使用不同版本的java - how do I use different version of java 如何处理同一台机器上不同项目的不同JAVA版本? - how to handle different JAVA versions for different projects on the same machine? 不同项目中的相同Java包 - Same java package in different projects JAVA:我如何以不同于创建用户输入的方法使用用户输入? - JAVA: How do i use user inputs in a different method from the one in which it was created? Java 为什么我需要这种方法以及如何使用它 - Java why do I need this method and how do I use it 如何将多个Java项目部署到同一个Azure云服务和存储帐户? - How do I deploy multiple Java projects to the same Azure cloud service and storage account?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM