面向对象的增删改查[小白篇]
程序员文章站
2022-03-10 18:22:26
面向对象的增删改查大家好,我是一名刚入门的程序猿,从今往后我会在这里给大家分享我每天所需的知识,也用于记录我的成长.作业题目为:定义一个数组,对其增删改查这里以管理学生为例子Student代码片Studentpackage com.zj;/** 学生类* */public class Student { private int id; private String name; private int ega; public int getId()...
面向对象的增删改查
大家好,我是一名刚入门的程序猿,从今往后我会在这里给大家分享我每天所需的知识,也用于记录我的成长.
作业题目为:定义一个数组,对其增删改查
这里以管理学生为例子
Student代码片
Student
package com.zj;
/*
* 学生类
* */
public class Student {
private int id;
private String name;
private int ega;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEga() {
return ega;
}
public void setEga(int ega) {
this.ega = ega;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", ega=" + ega +
'}';
}
public Student(int id, String name, int ega) {
this.id = id;
this.name = name;
this.ega = ega;
}
}
StudentManager
package com.zj;
/*
* 管理学生类
* */
public class StudentManager {
Student [] stus=new Student[30]; //固定班级的嘘学生数
static int currCount=0;//当前的人数
/*
* 添加学生
* */
public void add(Student student){
stus[currCount]=student;
currCount++;
}
/*
* 查所有
* */
public void queryALL(){
for (int i = 0; i <currCount ; i++) {
System.out.println(stus[i]);
}
}
/*
* 根据id去查询一个学生的信息
* */
public void byid(int id){
for (int i = 0; i <currCount ; i++) {
if (stus[i].getId()==id){
System.out.println(stus[i]);
}
}
}
/*
* 删除
* */
public void delete(int id){
for (int i = 0; i <currCount ; i++) {
if (stus[i].getId()==id){
for (int j = i; j <stus.length-1; j++) {
if (stus[j]!=null){
stus[j]=stus[j+1];
}
}
currCount--;
}
}
}
/*
* 修改学生信息 首先根据id查找到相对应的学生信息
*
* */
public void byid2(int id,Student student){
for (int i = 0; i <currCount ; i++) {
if (stus[i].getId()==id){
stus[i]=student;
}
}
}
}
StudentTest
package com.zj;
public class StudentZSGC {
public static void main(String[] args) {
StudentManager sm= new StudentManager();
Student s1=new Student(1,"张三",18);
Student s2=new Student(2,"李四",18);
Student s3=new Student(3,"王五",18);
Student s4=new Student(4,"杨六",18);
sm.add(s1);
sm.add(s2);
sm.add(s3);
sm.add(s4);
sm.queryALL();//查所有
System.out.println("================== 添加");
sm.byid(2);
sm.delete(2);
System.out.println("================== 删除");
sm.queryALL();//查所有
System.out.println("================== 更新");
Student student1=new Student(5,"小波",20);
sm.byid2(1,student1);
sm.queryALL();//查所有
}
}
D:\jdk\bin\java.exe "-javaagent:D:\idae\IntelliJ IDEA 2020.1.1\lib\idea_rt.jar=52080:D:\idae\IntelliJ IDEA 2020.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\jdk\jre\lib\charsets.jar;D:\jdk\jre\lib\deploy.jar;D:\jdk\jre\lib\ext\access-bridge-64.jar;D:\jdk\jre\lib\ext\cldrdata.jar;D:\jdk\jre\lib\ext\dnsns.jar;D:\jdk\jre\lib\ext\jaccess.jar;D:\jdk\jre\lib\ext\jfxrt.jar;D:\jdk\jre\lib\ext\localedata.jar;D:\jdk\jre\lib\ext\nashorn.jar;D:\jdk\jre\lib\ext\sunec.jar;D:\jdk\jre\lib\ext\sunjce_provider.jar;D:\jdk\jre\lib\ext\sunmscapi.jar;D:\jdk\jre\lib\ext\sunpkcs11.jar;D:\jdk\jre\lib\ext\zipfs.jar;D:\jdk\jre\lib\javaws.jar;D:\jdk\jre\lib\jce.jar;D:\jdk\jre\lib\jfr.jar;D:\jdk\jre\lib\jfxswt.jar;D:\jdk\jre\lib\jsse.jar;D:\jdk\jre\lib\management-agent.jar;D:\jdk\jre\lib\plugin.jar;D:\jdk\jre\lib\resources.jar;D:\jdk\jre\lib\rt.jar;C:\Users\欣欣\IdeaProjects\Student\out\production\Student com.zj.StudentZSGC
Student{id=1, name='张三', ega=18}
Student{id=2, name='李四', ega=18}
Student{id=3, name='王五', ega=18}
Student{id=4, name='杨六', ega=18}
================== 添加
Student{id=2, name='李四', ega=18}
================== 删除
Student{id=1, name='张三', ega=18}
Student{id=3, name='王五', ega=18}
Student{id=4, name='杨六', ega=18}
================== 更新
Student{id=5, name='小波', ega=20}
Student{id=3, name='王五', ega=18}
Student{id=4, name='杨六', ega=18}
Process finished with exit code 0
这些就是全部的代码, 我会定期更新一些所学的JAVA知识,欢迎大家多多指导;
本文地址:https://blog.csdn.net/qq_44303639/article/details/107347388
上一篇: 回到未来:Smalltalk 编程系统
下一篇: 向车站学习处理高并发高峰流量