diff --git a/pom.xml b/pom.xml index c20b489..5f64ed4 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,11 @@ 1.2.58 test + + com.google.code.gson + gson + 2.9.0 + org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/com/github/hcsp/encapsulation/Main.java b/src/main/java/com/github/hcsp/encapsulation/Main.java index 51ce4a1..c08562e 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Main.java +++ b/src/main/java/com/github/hcsp/encapsulation/Main.java @@ -1,32 +1,42 @@ package com.github.hcsp.encapsulation; +import com.google.gson.Gson; + public class Main { - /* - 假设你正在为学校开发一个学生分数记录系统 - 你和前端约定的JSON接口格式是: - { - "name": "张三", - "retakingExam": true, - "score": 59, - "fail": true // 是否挂科,如果分数低于60则返回true,代表挂科 - } - 请: - 1. 设计并完成Student类 - 2. 挑选一种你喜欢的JSON类库,完成序列化/反序列化的方法 - */ - public static void main(String[] args) { - Student student = new Student(); - student.setName("张三"); - student.setScore(60); - student.setRetakingExam(true); - String json = serialize(student); + /* + 假设你正在为学校开发一个学生分数记录系统 + 你和前端约定的JSON接口格式是: + { + "name": "张三", + "retakingExam": true, + "score": 59, + "fail": true // 是否挂科,如果分数低于60则返回true,代表挂科 + } + 请: + 1. 设计并完成Student类 + 2. 挑选一种你喜欢的JSON类库,完成序列化/反序列化的方法 + */ + public static void main(String[] args) { + Student student = new Student(); + student.setName("张三"); + student.setScore(60); + student.setRetakingExam(true); + String json = serialize(student); + + System.out.println(json); + + student = deserialize(json); + } - System.out.println(json); + // 序列化:将Student类转换成JSON字符串 + public static String serialize(Student student) { + Gson gson = new Gson(); + return gson.toJson(student); + } - student = deserialize(json); - } - // 序列化:将Student类转换成JSON字符串 - public static String serialize(Student student) {} - // 反序列化:将JSON字符串转换成Student对象 - public static Student deserialize(String json) {} + // 反序列化:将JSON字符串转换成Student对象 + public static Student deserialize(String json) { + Gson gson = new Gson(); + return gson.fromJson(json, Student.class); + } } diff --git a/src/main/java/com/github/hcsp/encapsulation/Student.java b/src/main/java/com/github/hcsp/encapsulation/Student.java index 85d2f55..92087e2 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Student.java +++ b/src/main/java/com/github/hcsp/encapsulation/Student.java @@ -8,6 +8,35 @@ public class Student { /** 是否重考。true为重考,falase为非重考。 */ private boolean retakingExam; + public boolean isFail() { + return score < 60; + } + + /** 分数 */ private int score; + + 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; + } } diff --git a/src/test/java/com/github/hcsp/encapsulation/Main.java b/src/test/java/com/github/hcsp/encapsulation/Main.java new file mode 100644 index 0000000..db8a942 --- /dev/null +++ b/src/test/java/com/github/hcsp/encapsulation/Main.java @@ -0,0 +1,36 @@ +package com.github.hcsp.encapsulation; + +public class Main { + /* + 假设你正在为学校开发一个学生分数记录系统 + 你和前端约定的JSON接口格式是: + { + "name": "张三", + "retakingExam": true, + "score": 59, + "fail": true // 是否挂科,如果分数低于60则返回true,代表挂科 + } + 请: + 1. 设计并完成Student类 + 2. 挑选一种你喜欢的JSON类库,完成序列化/反序列化的方法 + */ + public static void main(String[] args) { + Student student = new Student(); + student.setName("张三"); + student.setScore(60); + student.setRetakingExam(true); + String json = serialize(student); + + System.out.println(json); + + student = deserialize(json); + } + // 序列化:将Student类转换成JSON字符串 + public static String serialize(Student student) { + return com.alibaba.fastjson.JSON.toJSONString(student); + } + // 反序列化:将JSON字符串转换成Student对象 + public static Student deserialize(String json) { + return com.alibaba.fastjson.JSON.parseObject(json, Student.class); + } +}