您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

一类同名JAVA中的静态和非静态方法

一类同名JAVA中的静态和非静态方法

我希望此方法在没有对象的情况下有用。是否可以在不创建其他方法的情况下执行类似的操作?

您将不得不创建另一个方法,但是您可以使非静态方法调用静态方法,这样您就不会重复代码,并且如果您以后想更改逻辑,则只需在一个地方进行。

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());
    }
}
java 2022/1/1 18:26:28 有445人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶