如何在几个java类中使用相同的函数?
我不明白如何在几个java类中使用相同的函数。例如,我有以下功能:如何在几个java类中使用相同的函数?,java,Java,我不明白如何在几个java类中使用相同的函数。例如,我有以下功能: public int plus(int one, int two) { return one + two; } 我如何在其他几个文件类中使用它? 我应该在单独的类中创建它吗?如果您将函数放入一个类中并将其声明为静态 class MathFunctions { public static int plus(int one, int two) { return one + two; } } 您始终可以这样访问
public int plus(int one, int two) {
return one + two;
}
我如何在其他几个文件类中使用它?
我应该在单独的类中创建它吗?如果您将函数放入一个类中并将其声明为静态
class MathFunctions {
public static int plus(int one, int two) {
return one + two;
}
}
您始终可以这样访问它:
Mathfunctions.plus(1, 2);
class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test
{
public int plus(int one, int two)
{
return one + two;
}
}
如果您有一个非静态方法,则必须始终引用您在其中声明该方法的类的实际对象来调用它。如果您将函数放入一个类中并将其声明为静态
class MathFunctions {
public static int plus(int one, int two) {
return one + two;
}
}
您始终可以这样访问它:
Mathfunctions.plus(1, 2);
class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test
{
public int plus(int one, int two)
{
return one + two;
}
}
如果您有一个非静态方法,则必须始终引用您在其中声明该方法的类的实际对象来调用它。您可以创建一个实用程序类,如
public enum Maths {;
public static int plus(int one, int two) {
return one + two;
}
}
您可以创建一个实用程序类,如
public enum Maths {;
public static int plus(int one, int two) {
return one + two;
}
}
如果实现总是相同的1+2,则可以将其转换为静态方法,如下所示:
Mathfunctions.plus(1, 2);
class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test
{
public int plus(int one, int two)
{
return one + two;
}
}
然后你可以像这样调用函数
int result = Util.plus(1,1)
如果实现总是相同的1+2,则可以将其转换为静态方法,如下所示:
Mathfunctions.plus(1, 2);
class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test
{
public int plus(int one, int two)
{
return one + two;
}
}
然后你可以像这样调用函数
int result = Util.plus(1,1)
您应该创建一个类并将该函数添加到其中。然后在另一个类中调用该函数,例如包含main方法的Test类
public class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test {
public static void main(String args[])
{
System.out.println(Util.plus(4,2));
}
}
您应该创建一个类并将该函数添加到其中。然后在另一个类中调用该函数,例如包含main方法的Test类
public class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test {
public static void main(String args[])
{
System.out.println(Util.plus(4,2));
}
}
您创建的此函数必须位于类内部。如果您在同一个包中的另一个类中创建这个类的实例,例如:假设您有这个
public class Blah {
public int plus (int one, int two) {
return one + two;
}
}
然后你就有了一个你想用这些废话的类:
public class otherclass {
public void otherfunc{
int yo,ye,yu;
Blah instanceOfBlah = new Blah ();
yu = instanceOfBlah.plus(yo,ye);
}
}
您可以在任何其他类中使用这种方式来访问plus函数。如果这些其他类属于不同的包,您可能必须导入blah类tho。您创建的此函数必须位于类内部。如果您在同一个包中的另一个类中创建这个类的实例,例如:假设您有这个
public class Blah {
public int plus (int one, int two) {
return one + two;
}
}
然后你就有了一个你想用这些废话的类:
public class otherclass {
public void otherfunc{
int yo,ye,yu;
Blah instanceOfBlah = new Blah ();
yu = instanceOfBlah.plus(yo,ye);
}
}
您可以在任何其他类中使用这种方式来访问plus函数。如果这些其他类属于不同的包,您可能必须导入blah类tho。或者您可以这样做:
Mathfunctions.plus(1, 2);
class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test
{
public int plus(int one, int two)
{
return one + two;
}
}
然后像这样使用它:
int i = new Test().plus(1,2);
或者你可以这样做:
Mathfunctions.plus(1, 2);
class Util{
public static int plus(int one, int two) {
return one + two;
}
}
class Test
{
public int plus(int one, int two)
{
return one + two;
}
}
然后像这样使用它:
int i = new Test().plus(1,2);