From 0834f0d403e027616cacffaa7829e1c866942c7d Mon Sep 17 00:00:00 2001
From: "Yang.he" <”yang.he@homepartners.tech“>
Date: Tue, 14 Mar 2023 21:45:36 +0800
Subject: [PATCH 1/4] - [x] done
---
pom.xml | 2 +-
.../github/hcsp/encapsulation/Student.java | 34 +++++++++++++++++++
.../com/github/hcsp/encapsulation/Main.java | 8 +++--
3 files changed, 41 insertions(+), 3 deletions(-)
rename src/{main => test}/java/com/github/hcsp/encapsulation/Main.java (78%)
diff --git a/pom.xml b/pom.xml
index c20b489..38d6c9d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
com.alibaba
fastjson
- 1.2.58
+ 1.2.67_noneautotype2
test
diff --git a/src/main/java/com/github/hcsp/encapsulation/Student.java b/src/main/java/com/github/hcsp/encapsulation/Student.java
index 85d2f55..1ad7b74 100644
--- a/src/main/java/com/github/hcsp/encapsulation/Student.java
+++ b/src/main/java/com/github/hcsp/encapsulation/Student.java
@@ -8,6 +8,40 @@ public class Student {
/** 是否重考。true为重考,falase为非重考。 */
private boolean retakingExam;
+ public boolean isFail() {
+ return fail;
+ }
+
+ public void setFail(boolean fail) {
+ this.fail = fail;
+ }
+
+ private boolean fail;
+
/** 分数 */
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/main/java/com/github/hcsp/encapsulation/Main.java b/src/test/java/com/github/hcsp/encapsulation/Main.java
similarity index 78%
rename from src/main/java/com/github/hcsp/encapsulation/Main.java
rename to src/test/java/com/github/hcsp/encapsulation/Main.java
index 51ce4a1..db8a942 100644
--- a/src/main/java/com/github/hcsp/encapsulation/Main.java
+++ b/src/test/java/com/github/hcsp/encapsulation/Main.java
@@ -26,7 +26,11 @@ public static void main(String[] args) {
student = deserialize(json);
}
// 序列化:将Student类转换成JSON字符串
- public static String serialize(Student student) {}
+ public static String serialize(Student student) {
+ return com.alibaba.fastjson.JSON.toJSONString(student);
+ }
// 反序列化:将JSON字符串转换成Student对象
- public static Student deserialize(String json) {}
+ public static Student deserialize(String json) {
+ return com.alibaba.fastjson.JSON.parseObject(json, Student.class);
+ }
}
From 42134e40d39dcde0d6bd66a5fc3955e2de89e4b4 Mon Sep 17 00:00:00 2001
From: "Yang.he" <”yang.he@homepartners.tech“>
Date: Wed, 15 Mar 2023 20:12:38 +0800
Subject: [PATCH 2/4] - [x] done
---
pom.xml | 7 ++-
.../com/github/hcsp/encapsulation/Main.java | 44 +++++++++++++++++++
.../github/hcsp/encapsulation/Student.java | 7 +--
.../com/github/hcsp/encapsulation/Main.java | 9 +++-
4 files changed, 58 insertions(+), 9 deletions(-)
create mode 100644 src/main/java/com/github/hcsp/encapsulation/Main.java
diff --git a/pom.xml b/pom.xml
index 38d6c9d..5f64ed4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,9 +55,14 @@
com.alibaba
fastjson
- 1.2.67_noneautotype2
+ 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
new file mode 100644
index 0000000..28bb29f
--- /dev/null
+++ b/src/main/java/com/github/hcsp/encapsulation/Main.java
@@ -0,0 +1,44 @@
+package com.github.hcsp.encapsulation;
+
+import com.google.gson.Gson;
+
+import java.lang.reflect.Type;
+
+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) {
+ Gson gson = new Gson();
+ return gson.toJson(student);
+ }
+
+ // 反序列化:将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 1ad7b74..92087e2 100644
--- a/src/main/java/com/github/hcsp/encapsulation/Student.java
+++ b/src/main/java/com/github/hcsp/encapsulation/Student.java
@@ -9,14 +9,9 @@ public class Student {
private boolean retakingExam;
public boolean isFail() {
- return fail;
+ return score < 60;
}
- public void setFail(boolean fail) {
- this.fail = fail;
- }
-
- private boolean fail;
/** 分数 */
private int score;
diff --git a/src/test/java/com/github/hcsp/encapsulation/Main.java b/src/test/java/com/github/hcsp/encapsulation/Main.java
index db8a942..3a9d51c 100644
--- a/src/test/java/com/github/hcsp/encapsulation/Main.java
+++ b/src/test/java/com/github/hcsp/encapsulation/Main.java
@@ -1,5 +1,7 @@
package com.github.hcsp.encapsulation;
+import com.google.gson.Gson;
+
public class Main {
/*
假设你正在为学校开发一个学生分数记录系统
@@ -27,10 +29,13 @@ public static void main(String[] args) {
}
// 序列化:将Student类转换成JSON字符串
public static String serialize(Student student) {
- return com.alibaba.fastjson.JSON.toJSONString(student);
+ Gson gson = new Gson();
+ return gson.toJson(student);
}
+
// 反序列化:将JSON字符串转换成Student对象
public static Student deserialize(String json) {
- return com.alibaba.fastjson.JSON.parseObject(json, Student.class);
+ Gson gson = new Gson();
+ return gson.fromJson(json, Student.class);
}
}
From e2494136f32b32a35df673590d79c832eb23b849 Mon Sep 17 00:00:00 2001
From: "Yang.he" <”yang.he@homepartners.tech“>
Date: Wed, 15 Mar 2023 20:14:36 +0800
Subject: [PATCH 3/4] - [x] done
---
src/main/java/com/github/hcsp/encapsulation/Main.java | 2 --
1 file changed, 2 deletions(-)
diff --git a/src/main/java/com/github/hcsp/encapsulation/Main.java b/src/main/java/com/github/hcsp/encapsulation/Main.java
index 28bb29f..c08562e 100644
--- a/src/main/java/com/github/hcsp/encapsulation/Main.java
+++ b/src/main/java/com/github/hcsp/encapsulation/Main.java
@@ -2,8 +2,6 @@
import com.google.gson.Gson;
-import java.lang.reflect.Type;
-
public class Main {
/*
假设你正在为学校开发一个学生分数记录系统
From da46cb10f5bae178f278ce48ba1ce0186a48b30d Mon Sep 17 00:00:00 2001
From: "Yang.he" <”yang.he@homepartners.tech“>
Date: Wed, 15 Mar 2023 20:19:15 +0800
Subject: [PATCH 4/4] - [x] done end
---
src/test/java/com/github/hcsp/encapsulation/Main.java | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/src/test/java/com/github/hcsp/encapsulation/Main.java b/src/test/java/com/github/hcsp/encapsulation/Main.java
index 3a9d51c..db8a942 100644
--- a/src/test/java/com/github/hcsp/encapsulation/Main.java
+++ b/src/test/java/com/github/hcsp/encapsulation/Main.java
@@ -1,7 +1,5 @@
package com.github.hcsp.encapsulation;
-import com.google.gson.Gson;
-
public class Main {
/*
假设你正在为学校开发一个学生分数记录系统
@@ -29,13 +27,10 @@ public static void main(String[] args) {
}
// 序列化:将Student类转换成JSON字符串
public static String serialize(Student student) {
- Gson gson = new Gson();
- return gson.toJson(student);
+ return com.alibaba.fastjson.JSON.toJSONString(student);
}
-
// 反序列化:将JSON字符串转换成Student对象
public static Student deserialize(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, Student.class);
+ return com.alibaba.fastjson.JSON.parseObject(json, Student.class);
}
}