简体   繁体   中英

Passing Object of different classes as an argument to same method

Let's say I have three Classes A,B,C. All three do the same thing, but in a different way, they differ in efficiency. All the method names, variable names inside the three classes are same.

class A{
  public static int method(){
   ......
   return result;
  }
}
class B{
  public static method(){
   ......
   return result;
  }
}
class C{
  public static method(){
   ......
   return result;
  }
}

I have test class, which has a method to test the code in the above three classes. Since this testMethod() is common to all the three classes, is there a way to call this method with objects of classes A,B,C ?

class Test{
   public static int testMethod(Object ABC)
   {
       return ABC.method();
   }

   public static void main(String[] args){
      A a = new A();
      SOP(testMethod(a));
      B b = new B();
      SOP(testMethod(b));
      C c = new C();
      SOP(testMethod(c));
   }
}

The only approach I can think of is creating three different methods for each of the classes, like this.

class Test{
   public static int testMethodA(A a)
   {
       return a.method();
   }

   public static int testMethodB(B b)
   {
       return b.method();
   }

   public static int testMethodC(C c)
   {
       return c.method();
   }

   public main() 
   {
       //call to each of the three methods
       ............
   }

What is the best approach to this scenario? Basically I want to have only one method that can test all three classes.

Create an interface with the common method for all classes. Then, make each class implement this interface. In your test code, use the interface as parameter type and pass an instance of each class to the method. Note that when you do this, the method to test should not be static.

In code:

public interface MyInterface {
    //automatically public
    int method();
}

public class A implements MyInterface {
    @Override //important
    //not static
    public int method() {
        /* your implementation goes here*/
        return ...;
    }
}

public class B implements MyInterface {
    @Override //important to check method override at compile time
    public int method() {
        /* your implementation goes here*/
        return ...;
    }
}

//define any other class...

Then the test:

public class Test {
    //using plain naive console app
    public static void main(String[] args) {
        MyInterface myInterfaceA = new A();
        testMethod(myInterfaceA);
        MyInterface myInterfaceB = new B();
        testMethod(myInterfaceB);
        //and more...
    }

    public static void testMethod(MyInterface myInterface) {
        myInterface.method();
    }
}

Or if you prefer to use JUnit:

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

public class MyInterfaceTest {
    MyInterface myInterface;

    @Test
    public void methodUsingAImplementation() {
        myInterface = new A();
        //code more human-readable and easier to check where the code fails
        assertThat(myInterface.method(), equalTo(<expectedValue>));
    }
    //similar test cases for other implementations
}

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