[英]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
类,并至少定义public
或protected
方法。 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
班, public
或protected
方法不是在不同的项目进入/包即使默认类的方法都是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.