简体   繁体   English

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

[英]Static and non-static method in one class with the same name JAVA

I know it is impossible to override a method in one class. 我知道不可能在一个类中重写一个方法。 But is there a way to use a non-static method as static? 但是,有没有办法使用非静态方法作为静态方法呢? For example I have a method which is adding numbers. 例如,我有一个加数字的方法。 I want this method to be usefull with an object and also without it. 我希望此方法在没有对象的情况下有用。 Is it possible to do something like that without creating another method? 是否可以在不创建其他方法的情况下执行类似的操作?

EDIT: What I mean is, if I make a method static I will need it to take arguments, and if I create an object with variables already set it will be very uncomfortable to call function on my object with same arguments again. 编辑:我的意思是,如果我将一个方法设为静态,我将需要它接受参数,并且如果我创建了一个已经设置了变量的对象,那么再次对具有相同参数的对象调用函数将非常不舒服。

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 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);
       t1.count();
    }

}

I know the code is incorrect but i wanted to show what I want to do. 我知道代码不正确,但是我想展示自己想做的事情。

I want this method to be usefull with an object and also without it. 我希望此方法在没有对象的情况下有用。 Is it possible to do something like that without creating another method? 是否可以在不创建其他方法的情况下执行类似的操作?

You will have to create another method, but you can make the non-static method call the static method, so that you do not duplicate the code and if you want to change the logic in the future you only need to do it in one place. 您将不得不创建另一个方法,但是您可以使非静态方法调用静态方法,这样您就不会重复代码,并且如果您以后想要更改逻辑,则只需在一个地方进行。

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());
    }
}

But is there a way to use a non-static method as static? 但是,有没有办法使用非静态方法作为静态方法呢?

No, it's not possible. 不,不可能。

If you need this method to be used in static and non-static context, then make it static . 如果需要在静态和非静态上下文中使用此方法,请使其为static The opposite configuration , however, is not possible. 但是,相反的配置是不可能的。

Make it static, then it can be used with object and without it. 将其设置为静态,然后可以与对象一起使用,也可以不使用它。

 public class MyTest() {
     public static int add() {
         System.out.println("hello");
     }
 }

MyTest.add(); //prints hello

MyTest myobject = new MyTest();
myobject.add(); //prints hello

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM