Skip to content

Instantly share code, notes, and snippets.

@ruyut
Created January 11, 2022 07:07
Show Gist options
  • Select an option

  • Save ruyut/2eeaf379ba778180b198b7d3c95abcb1 to your computer and use it in GitHub Desktop.

Select an option

Save ruyut/2eeaf379ba778180b198b7d3c95abcb1 to your computer and use it in GitHub Desktop.
Java 鏈式賦值
package com.ruyut.test;
public class ChainDto {
private int id;
private String name;
public ChainDto setId(int id) {
this.id = id;
return this;
}
public ChainDto setName(String name) {
this.name = name;
return this;
}
}
package com.ruyut.test;
public class Dto {
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
package com.ruyut.test;
import java.util.ArrayList;
import java.util.List;
public class Test {
public void CreateBySetter() {
Dto dto = new Dto();
dto.setId(0);
dto.setName("test");
}
public List<Dto> GetDtos() {
List<Dto> dtos = new ArrayList<>();
Dto dto = new Dto();
dto.setId(0);
dto.setName("test");
dtos.add(dto);
return dtos;
}
public void CreateByChain() {
ChainDto dto = new ChainDto().setId(0).setName("test");
}
public List<ChainDto> GetChainDtos() {
List<ChainDto> dtos = new ArrayList<>();
dtos.add(new ChainDto().setId(0).setName("test"));
return dtos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment