我希望此方法在没有对象的情况下有用。是否可以在不创建其他方法的情况下执行类似的操作?
您将不得不创建另一个方法,但是您可以使非静态方法调用静态方法,这样您就不会重复代码,并且如果您以后想更改逻辑,则只需在一个地方进行。
public class Test { private int a; private int b; private int c; public Test(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public String count() { return count(a, b, c); } public static String count(int a1, int b1, int c1) { String solution; solution = Integer.toString(a1 + b1 + c1); return solution; } public static void main(String[] args) { System.out.println(Test.count(1, 2, 3)); Test t1 = new Test(1, 2, 3); System.out.println(t1.count()); }}


