简体   繁体   English

是否可以在不传递方法参数的情况下访问其他 class 方法

[英]Is it possible to access other class method without passing method parameter

I have scenario where i want to access another class method which is returning something.我有一个场景,我想访问另一个 class 方法,该方法正在返回一些东西。

The method in another class is expecting parameter另一个 class 中的方法是期望参数

Example:例子:

public class Class1()
{
    public Response postResponse(String getURL,DataTable dataTable)
    {
        /*..
        my post request code here
        ..*/
        return postData;
    }
}

public class Class2()
{
    public void readPostResponse()
    {
        /*..
         here i want to access Class1.postResponse method and I don't want to pass the 
         parameter.. 
        ..*/
    }
}

Please let me know how to achieve this.请让我知道如何实现这一目标。

You have three choices:你有三个选择:

  • pass parameters.传递参数。 This is preferred way.这是首选方式。 Because parameters of method contains some info that can be used in method因为方法的参数包含一些可以在方法中使用的信息
  • mock or stub your parameters if it is possible.如果可能的话,模拟或存根你的参数。 It looks like you are using Selenium, but Selenium starts a real browser看起来你正在使用 Selenium,但是 Selenium 启动了一个真正的浏览器
  • pass null parameters.It is not really preferred way because your method can work incorrectly without necessary data传递 null 参数。这不是真正的首选方式,因为您的方法在没有必要数据的情况下可能无法正常工作

I started to write this before @StepUp posted his answer.在@StepUp 发布他的答案之前,我开始写这篇文章。 I am going to cover the way I think it should be done based on what I understood by "I don't want to pass the parameter"我将根据我对“我不想传递参数”的理解来介绍我认为应该完成的方式

It sounds you want to create a default case.听起来您想创建一个默认案例。 This example is arbitrary since your code is not really specific.这个例子是任意的,因为你的代码不是很具体。 In general terms, when talking about functions, a default case for a function is a method with the same name, with no parameter list.一般来说,在谈论函数时,function 的默认情况是具有相同名称的方法,没有参数列表。 For example,例如,

public class Class1 
{
    /**
     * Default case
     */
    public Response postResponse() 
    {
        // Some code here
    }

    /**
     * Specific case (the ellipsis means some parameter list) 
     */
    public Response postResponse(...)
    {
        // Some code here
    }
}

In your case, you may want to create a default step definition that you can invoke independently in a specific scenario, or you may want to call it from the same scenario with or without parameters specified in your data table.在您的情况下,您可能希望创建一个可以在特定场景中独立调用的默认步骤定义,或者您可能希望在数据表中指定或不指定参数的情况下从相同的场景中调用它。 You can still take the same approach.你仍然可以采取同样的方法。 I want to mention that having a default case, doesn't mean that you absolutely need to pass null parameters.我想提一下,有一个默认情况,并不意味着你绝对需要传递 null 参数。 99% of the time, this is a bad idea. 99% 的情况下,这是一个坏主意。 All you need to do is to pass some default values.您需要做的就是传递一些默认值。

public class Class1 
{
    /**
     * Default case
     */
    public Response postResponse() 
    {
        String url = "..."; // maybe a base URL here?
        Object col1 = ...;
        Object col2 = ...;
        handlePostResponse(url, col1, col2);
        // Do other stuff?
        return response;
    }

    /**
     * Specific case 
     */
    public Response postResponse(String url, DataTable dataTable)
    {
        // Iterate through data table and call handlePostResponse for each row
        return response;
    }


    // This method wraps how to handle post response. The ellipsis might be the elements of the data table
    private void handlePostResponse(String url, Object col1, Object col2) 
    {
        // Do something
    }
}

Now that you have this, you can call your method in Class 2 that needs to invoke the default case in Class 1.现在您有了这个,您可以在 Class 2 中调用您的方法,该方法需要在 Class 1 中调用默认情况。

public class Class2 
{
    public void readPostResponse()
    {
        Class1 clazz1 = new Class1();
        Response resp = clazz1.postResponse(); // calling no parameters (default) case (you may not need to get the response object (ignore it altogether)
        // Do more stuff?
    }
}

Remember that every time you pass null to a function or return null from a function, you may have to do some sort of input or output validation to ensure the object about to be used to invoke other methods is not null. Remember that every time you pass null to a function or return null from a function, you may have to do some sort of input or output validation to ensure the object about to be used to invoke other methods is not null. Not doing so will result in Null Pointer Exceptions.不这样做会导致 Null 指针异常。

One last note : I am using Object merely for illustration.最后一点:我使用Object只是为了说明。 In your case, it will be whatever type of object each column represent.在您的情况下,它将是每列代表的任何类型的 object。 Most likely, it will be String , but it could also be a number wrapper (ie Integer ) or some custom data type you might've created for your scenario(s).最有可能的是String ,但也可能是数字包装器(即Integer )或您可能为您的场景创建的一些自定义数据类型。

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

相关问题 在将类型为 class 的属性作为参数传递给方法时,是否可以在没有设置器的情况下访问它? - While passing an attribute typed class as a parameter to a method, is it possible to access it without setters? 传递方法作为参数 - 这可能吗? - Passing method as a parameter - Is this possible? 从同一个类中的其他方法访问ArrayList而不传递参数的方法? - Way to access ArrayList from other method in same class without passing argument? 如何在不作为方法参数传递的情况下访问对象的 state? - How to access an object's state without passing as parameter in method? 及格课程 <?> cls作为方法参数? - Passing Class<?> cls as a method parameter? 将类作为参数传递的通用方法 - Generic method passing class as a parameter 将类作为方法中的参数传递,并在If语句中使用此参数 - Passing a class as a parameter in a method and using this parameter in an If statement 将子类传递给具有抽象类参数的方法 - Passing a subclass to a method with an abstract class parameter 将超类作为参数传递给期望子类的方法 - Passing superclass as parameter to method expecting sub class 我的方法(正在被另一个类调用)如何从该类获取变量而不将其作为参数传递? - How can my method, which is being called by another class, obtain a variable from that class without passing it as a parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM