2 wshger wshger 于 2013.12.20 12:07 提问

求解这段代码是什么意思,表示看不懂,求大神讲解,不知道其中是怎么实现增删改查的

class Clerk {
private String id;
private String name;
private String duty;
private double salary;

public Clerk(String id, String name) {
    this.id = id;
    this.name = name;
}

public void addClerk() {
    // TODO Auto-generated method stub

}

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDuty() {
    return duty;
}
public void setDuty(String duty) {
    this.duty = duty;
}
public double getSalary() {
    return salary;
}
public void setSalary(double salary) {
    this.salary = salary;
}

}
class ClerkManage {
ArrayListal = null;

public ClerkManage() {
    ArrayList<Clerk> al = new ArrayList<Clerk>();  
}

public void addClerk(Clerk clerk){
    al.add(clerk);
}

// if not found return null
public Clerk searchClerk(String id){
    Clerk clerk = null;
    for(Clerk clerkTmp : al) {
        if(id.equals(clerkTmp.getId())) {
            clerk = clerkTmp;
            break;
        }
    }
    return clerk;
}

public void setSalaryOfClerk(String id, double salary){
    Clerk clerk = searchClerk(id);
    if(clerk != null) {
        clerk.setSalary(salary);
    } else {
        System.out.println("找不到员工编号:" + id);
    }
}

public void deleteClerk(String id){
    for(int i = 0, size = al.size(); i < size; i++) {
        if(id.equals(al.get(i).getId())) {
            al.remove(i);
            break;
        }
    }
}

}

2个回答

xinghaiwang197
xinghaiwang197   2013.12.20 14:01

class Clerk是结点
class ClerkManage是链表。
public void addClerk(Clerk clerk){
al.add(clerk);
}是增加一个成员的信息
public void deleteClerk(String id){是删除一个成员
public Clerk searchClerk(String id){是按ID搜索成员

wshger
wshger 谢谢
接近 4 年之前 回复
u013208438
u013208438   2013.12.20 14:03

class Clerk {
//定义4个私有的属性
private String id;
private String name;
private String duty;
private double salary;

/**

  • 带参数的构造方法 */ public Clerk(String id, String name) { this.id = id; this.name = name; } //定义了一个返回为void的方法 public void addClerk() { // TODO Auto-generated method stub

}

//为4个私有属性提供公开的取值和赋值方法,也是就算get和set方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}


class ClerkManage {
ArrayList al = null;//声明了一个变量名为 al 且为空的ArrayList集合

//无参构造方法
public ClerkManage() {
//实例化一个ArrayList
ArrayList al = new ArrayList();

}
/**

  • 添加方法,把传入的Clerk添加到集合中 */ public void addClerk(Clerk clerk){ al.add(clerk); }

// if not found return null
/**
根据传入的id在ArrayList中查找Clerk
*/
public Clerk searchClerk(String id){
Clerk clerk = null;//用来装找到的Clerk
//循环遍历al
for(Clerk clerkTmp : al) {
//判断传入的id 是否在集合里面找的到
if(id.equals(clerkTmp.getId())) {
//把找的的Clerk 赋值给前面用来装的clerk
clerk = clerkTmp;
//跳出循环
break;
}
}
//返回结果
return clerk;
}
/
*
给员工设置工资的方法
*id 员工id
*salary 要设置的工资多少
*/
public void setSalaryOfClerk(String id, double salary){
//查找员工根据id 并返回一个类
Clerk clerk = searchClerk(id);
//判断是否有该员工
if(clerk != null) {
//如果有 设置工资
clerk.setSalary(salary);
} else {
//没找到 提示用户
System.out.println("找不到员工编号:" + id);
}
}
/
*

  • *删除员工 根据id */ public void deleteClerk(String id){ //循环遍历al for(int i = 0, size = al.size(); i < size; i++) { //判断是有存在该id if(id.equals(al.get(i).getId())) { //如果存在该 删除该员工 al.remove(i); //跳出循环 break; } } } }
u010994927
u010994927 楼上注释很到位
接近 4 年之前 回复
wshger
wshger thank you万分感谢!
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片