简体   繁体   中英

How to call two methods in same class?

How can I call two methods from the same class over one object? I mean I try to write a class and it's methods to run above code:

volume = Calculate.do_calc().get_volume(a);

I am creating Calculate class and two methods of it. do_calc() and get_volume(a). How should I write this class to run that code.

You must return this; at the end of each method of your class if they are not static . If the methods are static , do it like this:

public class Calculation {
     public static Calculation do_calc () {
           //do your calculation
          return null;
      }

     public static Calculation get_volume(int x) {
           //do your calculation
          return null;
      }
}

Then you can write:

Calculation.do_calc().get_volume(1);

No problem in returning null , as the methods are static and not related to a specific instance of the class. If you don't like it, then return new Calculation();

[Edit]

The first method should return a real object if you need to pass its result to the second method:

public class Calculation {

     int result;

     public static Calculation do_calc () {
           //do your calculation
           Calculation c=new Calculation();
           c.result = theResultOfTheCalculation;
          return c;
      }

     public void get_volume(int x) {
           //do your calculation for example:
           System.out.println(result + x);
      }
}

除非 do_calc() 返回函数 get_volume() 所在的类,否则不应该这样做。

Here is a little sample for you.

public class ChainTest {

        public static void main(String[] args) {
                System.out.println(new ChainTest().do_calc().get_volume(1));
        }

        public ChainTest do_calc() {
                // do something;
                return ChainTest.this;
        }

        public int get_volume(int a) {
                return a;
        }
}

You don't need to write the code in one line. You can call same object methods in different lines.

Calculator calculator = new Calculator();
calculator.do_calc();
calculator.get_volume(a);

In case, if you want static methods

Calculator.do_calc();
Calculator.get_volume(a);

Case 1 If do_calc is static

public class Calculator {

    public static void main(String[] args) {
        System.out.println(Calculator.do_calc().get_volume(1));
    }

    public static Calculator do_calc() {

        Calculator calculator = new Calculator();
        // do something;

        return calculator;
    }

    public float get_volume(int a) {
        return a;
    }
}

Case 2 : If do_calc is not static

public class Calculator {

    public static void main(String[] args) {
        System.out.println(new Calculator().do_calc().get_volume(1));
    }

    public Calculator  do_calc() {

        Calculator calculator = new Calculator();
        // do something;

        return calculator;
    }

    public float get_volume(int a) {
        return a;
    }
}

Case 3 : If both have return type float as you mentioned in comment

public class Calculator {

    public static void main(String[] args) {

        Calculator calculator = new Calculator();
        calculator.do_calc();
        System.out.println(calculator.get_volume(1));
    }

    public float do_calc() {

        // do something;

        return 1f; // return your result
    }

    public float get_volume(int a) {
        // do something;
        return a;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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