11

  • 1
package com.github.hcsp.encapsulation; Public class Cat {/** private static String name; /** private static int age; /** If the cat is cute, true is cute, false is not cute */ private static Boolean cute; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isCute() { return cute; } public void setCute(boolean cute) { this.cute = cute; }}Copy the code
  • 2
package com.github.hcsp.encapsulation; public final class UserBuilder { private String firstName; private String lastName; private String phoneNumber; private String address; private UserBuilder() { } public static UserBuilder anUser() { return new UserBuilder(); } public UserBuilder withFirstName(String firstName) { this.firstName = firstName; return this; } public UserBuilder withLastName(String lastName) { this.lastName = lastName; return this; } public UserBuilder withPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; return this; } public UserBuilder withAddress(String address) { this.address = address; return this; } public User build() { return new User(firstName, lastName, phoneNumber, address); }}Copy the code
  • 3
package com.github.hcsp.encapsulation; public final class UserBuilder { private String firstName; private String lastName; private String phoneNumber; private String address; // public UserBuilder() { // } public static UserBuilder UserBuilder() { return new UserBuilder(); } public UserBuilder firstName(String firstName) { this.firstName = firstName; return this; } public UserBuilder lastName(String lastName) { this.lastName = lastName; return this; } public UserBuilder phoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; return this; } public UserBuilder address(String address) { this.address = address; return this; } public User build() { return new User(firstName, lastName, phoneNumber, address); }}Copy the code
  • 4 the serialization
  • student.java
package com.github.hcsp.encapsulation; Public class Student {** * name */ private String name; /** * Whether to retest. A. true B. falase C. true D. falase */ private boolean retakingExam; /** * score */ private int score; public boolean isFail() { return fail; } public void setFail(boolean fail) { if (this.score < 60) { this.fail = true; } else { this.fail = false; } } private boolean fail; public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isRetakingExam() { return retakingExam; } public void setRetakingExam(boolean retakingExam) { this.retakingExam = retakingExam; } public int getScore() { return score; } public void setScore(int score) { this.score = score; }}Copy the code
  • Main.java
package com.github.hcsp.encapsulation; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; {"name": "zhang SAN ", "retakingExam": true, "score": 59, "fail": true // If the score is lower than 60, return true, representing failure} Please: 1. 2. Pick a JSON library that you like, Public static void main(String[] args) {Student Student = new Student(); Student. Elegantly-named setName (" zhang "); student.setScore(60); student.setRetakingExam(true); String json = serialize(student); System.out.println(json); student = deserialize(json); System.out.println("student = " + student); } public static String serialize(Student Student) {return json.tojsonString (Student); } // deserialize: Public static Student deserialize(String JSON) {Student resultJson = jsonObject.parseObject (JSON, Student.class); return resultJson; }}Copy the code
  • 4
package com.github.hcsp.encapsulation; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; {"name": "zhang SAN ", "retakingExam": true, "score": 59, "fail": true // If the score is lower than 60, return true, representing failure} Please: 1. 2. Pick a JSON library that you like, Public static void main(String[] args) {Student Student = new Student(); Student. Elegantly-named setName (" zhang "); student.setScore(60); student.setRetakingExam(true); String json = serialize(student); System.out.println(json); student = deserialize(json); System.out.println("student = " + student); } public static String serialize(Student Student) {return json.tojsonString (Student); } // deserialize: Public static Student deserialize(String JSON) {Student resultJson = jsonObject.parseObject (JSON, Student.class); return resultJson; }}Copy the code
  • 5
  • bridge.java
package com.github.blindpirate.extensions; public class Bridge { public static Object newInstance() { return new CaptureSystemOutputExtension(); }}Copy the code
  • main.java
package com.github.hcsp.encapsulation; import com.github.blindpirate.extensions.Bridge; public class Main { public static void main(String[] args) { System.out.println(createCaptureSystemOutputExtension().getClass().getName()); } public static Object createCaptureSystemOutputExtension () {/ / because CaptureSystemOutputExtension package level is private, Therefore, it cannot be created directly // https://github.com/blindpirate/junit5-capture-system-output-extension/blob/4ee3aa0a0d9b2610b482e4571ecc33828c60248a/src/ Main/Java/com/lot/blindpirate/extensions/CaptureSystemOutputExtension java# L44 / / to find a way to get around this, create an instance. Tip: You can create other classes and other methods; you don't have to do it in this class. Good luck! return new Bridge().newInstance(); }}Copy the code