Java 面向对象编程示例
Test13 类
package com.oop;
public class Test13 {
public static void main(String[] args) {
JavaClass javaClass = new JavaClass(5);
javaClass.push(new JavaStudent(1, "张三", 18, true));
javaClass.push(new JavaStudent(2, "李四", 19, false));
javaClass.push(new JavaStudent(3, "王五", 20, true));
javaClass.push(new JavaStudent(4, "赵六", 21, false));
javaClass.push(new JavaStudent(5, "田七", 22, true));
System.out.println(javaClass.push(new JavaStudent(6, "小明", 22, true)));
System.out.println(javaClass.push(new JavaStudent(7, "田七", 22, true)));
System.out.println(javaClass.push(new JavaStudent(8, "田七", 22, true)));
System.out.println(javaClass.push(new JavaStudent(9, "黑盒", 22, true)));
System.out.println(javaClass.getJavaClass());
System.out.println("====================================");
JavaStudent student = javaClass.find(5)[0];
System.out.println(student.getStudent());
System.out.println("====================================");
javaClass.update(new JavaStudent(5, "牛逼王", 22, true));
System.out.println(student.getStudent());
System.out.println("====================================");
javaClass.delete(3);
System.out.println(javaClass.getJavaClass());
}
}
笔者在 Test13
类中创建了一个 JavaClass
对象,并向其中添加了多个 JavaStudent
实例。通过调用 push
方法,学生对象被添加到班级中。当班级容量不足时,push
方法会自动扩展数组的长度。接着,笔者展示了如何查找、更新和删除学生信息,并通过 getJavaClass
方法打印出当前班级的所有学生信息。
JavaClass 类
package com.oop;
import java.util.Arrays;
public class JavaClass {
private JavaStudent[] students = null;
private int actualLength = 0;
public JavaClass() {
this.students = new JavaStudent[5];
}
public JavaClass(int length) {
this.students = new JavaStudent[length > 0 ? length : 5];
}
public int push(JavaStudent student) {
if (actualLength >= students.length) {
int newLength = students.length * 2;
students = Arrays.copyOf(students, newLength);
}
students[actualLength++] = student;
return actualLength;
}
public JavaStudent[] find(int sid) {
for (JavaStudent student : students) {
if (student != null && student.getSid() == sid) {
return new JavaStudent[]{student};
}
}
return null;
}
public int update(JavaStudent student) {
JavaStudent existingStudent = find(student.getSid())[0];
if (existingStudent != null) {
existingStudent.setName(student.getName());
existingStudent.setAge(student.getAge());
existingStudent.setIsEmployed(student.getIsEmployed());
return existingStudent.getSid();
}
return 0;
}
public int delete(int sid) {
for (int i = 0; i < actualLength; i++) {
if (students[i].getSid() == sid) {
for (int j = i; j < actualLength - 1; j++) {
students[j] = students[j + 1];
}
students[actualLength - 1] = null;
actualLength--;
return sid;
}
}
return 0;
}
public String getJavaClass() {
StringBuilder stringData = new StringBuilder();
for (int i = 0; i < actualLength; i++) {
stringData.append(students[i].getStudent());
}
return "[" + stringData.toString() + "]";
}
}
在 JavaClass
类中,笔者管理了一个 JavaStudent
数组,用于存储学生对象。构造方法允许初始化数组的长度,默认为5。当添加学生时,如果数组已满,push
方法会将数组长度加倍,以容纳更多学生。find
方法通过学生ID查找对应的学生对象,update
方法用于更新学生信息,delete
方法则用于删除指定ID的学生。getJavaClass
方法返回当前班级所有学生的信息。
优化点:
- 将
javaClass
数组重命名为students
,提高变量名的可读性。 - 在
find
方法中增加空值检查,避免潜在的空指针异常。 - 使用
StringBuilder
替代字符串拼接,提高getJavaClass
方法的性能。
JavaStudent 类
package com.oop;
public class JavaStudent {
private int sid;
private String name;
private int age;
private boolean isEmployed;
public JavaStudent() {
}
public JavaStudent(int sid, String name, int age, boolean isEmployed) {
this.sid = sid;
this.name = name;
this.age = age;
this.isEmployed = isEmployed;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getSid() {
return sid;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setIsEmployed(boolean isEmployed) {
this.isEmployed = isEmployed;
}
public boolean getIsEmployed() {
return isEmployed;
}
public String getStudent() {
return "{\n"
+ "\t\"sid\": " + sid + ",\n"
+ "\t\"name\": \"" + name + "\",\n"
+ "\t\"age\": " + age + ",\n"
+ "\t\"isEmployed\": " + isEmployed + "\n"
+ "}\n";
}
}
JavaStudent
类代表一个学生对象,包含学生ID、姓名、年龄和就业状态等属性。提供了相应的构造方法、getter 和 setter 方法,以及一个 getStudent
方法,用于返回学生信息的字符串表示。
优化点:
- 移除了多余的逗号,确保生成的JSON格式正确。
- 确保
getStudent
方法输出的字符串格式规范,便于阅读和调试。
注意事项
在使用这些类时,确保正确处理可能的 null
值,特别是在调用 find
方法后,返回值可能为 null
。在实际应用中,建议增加更多的错误处理和输入验证,以提高程序的健壮性。