Created
January 11, 2022 07:07
-
-
Save ruyut/2eeaf379ba778180b198b7d3c95abcb1 to your computer and use it in GitHub Desktop.
Java 鏈式賦值
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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