我如何调用在主类java的另一个类中的方法

我正在使用selenium,我对Java很新。 我有三个类ATSmoke()是主类。 我有所有我的方法名称在另一个类别Profile()和Schedule()中的Excel工作表。 现在我用POI库来获取单元格值(即方法名称)。 在这里,我陷入了如何在另一个类Profile()中调用这些方法(edit_contact_info)。 如果他们在同一class,我可以使用相同的class级名称来引用。 但不能为另一个class级做。 另外还有另外一个名为ATTestDriver的类,其中我有所有的实用方法,如selectwebdriver,浏览器等。

公共类ATSmoke {

public static void main(String[] args){ Profile profileDriver = new Profile(Browsers.CHROME); XSSFWorkbook srcBook = null; try { srcBook = new XSSFWorkbook("./TestData/Testcase_data_v1.xlsx"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } XSSFSheet sourceSheet = srcBook.getSheet("Testcases"); int rowCount = sourceSheet.getLastRowNum(); for (int i=1; i<=rowCount; i++){ int rownum=i; XSSFRow testcaserow=sourceSheet.getRow(rownum); XSSFCell testcase_Name= testcaserow.getCell(1); String flagState=testcaserow.getCell(2).getStringCellValue(); if (flagState.equals("yes")) { if (testcase_Name != null) { try { Method myMethod = ATSmoke.class.getMethod(testcase_Name.getStringCellValue()); myMethod.invoke(new ATSmoke()); } catch (NoSuchMethodException | SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(""); } } } try { srcBook.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

公共类configuration文件扩展ATTestDriver {

 public Profile(Browsers browser) { super(browser); } public void edit_contact_info() { WebElement pageopened =this.waitForElement(By.cssSelector(".qualifications pbb")); System.out.println("you have " +pageopened.getText()); driver.findElement(By.cssSelector("contact-information button")).click(); } 

}

您可以使用javareflection来dynamic执行该方法。

 try { Class<?> c = Class.forName(args[0]); Object t = c.newInstance(); Method[] allMethods = c.getDeclaredMethods(); for (Method m : allMethods) { String mname = m.getName(); if (!mname.startsWith("test") || (m.getGenericReturnType() != boolean.class)) { continue; } Type[] pType = m.getGenericParameterTypes(); if ((pType.length != 1) || Locale.class.isAssignableFrom(pType[0].getClass())) { continue; } out.format("invoking %s()%n", mname); try { m.setAccessible(true); Object o = m.invoke(t, new Locale(args[1], args[2], args[3])); out.format("%s() returned %b%n", mname, (Boolean) o); // Handle any exceptions thrown by method to be invoked. } catch (InvocationTargetException x) { Throwable cause = x.getCause(); err.format("invocation of %s failed: %s%n", mname, cause.getMessage()); } } // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (InstantiationException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } 

来源:[ https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html%5D [1 ]

要在另一个类中调用一个方法,你首先必须实例化它:

 MyClass myClass = new MyClass(); myclass.mymethod(); 

或者在你的具体情况下:

  Profile profile = new Profile(browser); profile.edit_contact_info();