简体   繁体   中英

Creating Dynamic References in java

I have two classes.

package utilities;

public class PostCaller {

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getRequest() {
        return request;
    }
    public void setRequest(String request) {
        this.request = request;
    }

    String request  = null;
    String url      = null;

}

public class TestLoader {

    public static void main(String[] args) throws Exception {
        Object obj = Class.forName("utilities.PostCaller").newInstance();
        System.out.println(obj.getClass().getName()); // This prints utilities.PostCaller
    }
}

I want to create a new Instatnce of the PostCaller and call it's method from the TestLoader class. Here, I have a question of creating a reference of that utilities.PostCaller class. That means, I want to create a reference variable whose name is dyanamically given by a String (eg: utilities.PostCaller). Here although I have created a new Instance of the utilities.PostCaller, I cannot call it's methods since I don't have created a reference of it.

As I understood you just wanna test your PostCaller...I If true, EasyMock library can help ypu in this case. You can search via google a lot of examples, here is one http://tersesystems.com/2007/06/25/easymock-examples .

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