我是Java新手并尝试开发测试自动化框架。我一直在关注教程。我正在努力理解一些代码。如果有人可以逐步解释代码是如何工作或任何引用的话,那真的很棒。谢谢你提前!
@Test
NewPostPage.CreatePost("This is the test post title").WithBody("Hi, this is the body").publish();
public class NewPostPage {
public static CreatePostCommand CreatePost(String title) {
return new CreatePostCommand(title);
}
public class CreatePostCommand {
private final String title;
private String body;
public CreatePostCommand(String title){
this.title=title;
}
public CreatePostCommand WithBody(String body){
this.body=body;
return this;
答案 0 :(得分:2)
我将解释代码,因为这就是你的要求。
这是一个静态方法,这意味着它是一个类级方法,而不是实例级方法。您可以使用classname后跟方法名称来调用它。
NewPostPage.CreatePost()
由
定义public static CreatePostCommand CreatePost(String title)
它返回带有适当参数的CreatePostCommand
对象。
return new CreatePostCommand(title);
CreatePostCommand
的构造函数接受一个String。
public CreatePostCommand(String title){
this.title=title;
}
然后,WithBody
方法有一个Builder模式。 Builder模式的目的是在一行上将多个WithX
调用链接在一起,而不是为setX
和setY
方法使用多行。每个WithX
调用都会返回它正在构建的对象。
return this;
现在,我猜你遗漏了publish
方法?但是你所拥有的就等同于此。
CreatePostCommand postCmd = NewPostPage.CreatePost("This is the test post title");
postCmd = postCmd.WithBody("Hi, this is the body");
postCmd.publish();