java增删改查和案例

这篇博客探讨了Java中的封装概念,通过权限修饰符(private、缺省、protected、public)展示了封装的实现。内容包括增删改查操作的案例分析,详细讲解了如何封装类以及如何调用封装的类和方法。此外,还涵盖了import关键字的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

封装性的体现,需要权限修饰符来配合的。

java规定的4个权限(从小到大排序):private、确省、protected、public

请添加图片描述

4种权限可以用来修饰及类的内部结构:属性、方法、构造器、内部类

具体的,4种权限都可以用来修饰类内部结构,属性、方法、构造器、内部类

修饰类的话,只能使用:缺省、publ ic

增删改查

类:Customer

package twe.wad;

public class Customer {
   
    private String name;//客户姓名
    private char gender;//性别
    private int age;//年龄
    private String phone;//电话号码

    public Customer(){
   

    }

    public Customer(String name, char gender, int age, String phone, String email) {
   
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }

    private String email;//电子邮箱


    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public char getGender() {
   
        return gender;
    }

    public void setGender(char gender) {
   
        this.gender = gender;
    }

    public int getAge() {
   
        return age;
    }

    public void setAge(int age) {
   
        this.age = age;
    }

    public String getPhone() {
   
        return phone;
    }

    public void setPhone(String phone) {
   
        this.phone = phone;
    }

    public String getEmail() {
   
        return email;
    }

    public void setEmail() {
   
        this.email = email;
    }
}


类:CustomerList
package twe.wad;

public class CustomerList {
   
    private Customer[] customers;//用来保存客户对象的数组
    private int total = 0;//记录已保存客户对象的数量

    //用来初始化customers数组的构造器
    //构造器用来指定数组的多度
    public CustomerList(int totalCustomer) {
   
        //数组在构造器中动态初始化
        customers = new Customer[totalCustomer];
    }

    //添加
    public boolean addCustomer(Customer customer) {
   
        //判断成功还是失败(customers数组的长度)
        if (total >= customers.length) {
   
            return false;
        }
//        customers[total]=customer;
//        total++; 或
        customers[total++] = customer;
        return true;
    }

    //修改(指定索引位置的客户信息)
    public boolean replaceCustomer(int index, Customer cust) {
   
        if (index < 0 || index >= total) {
   
            return false;
        }
        customers[index] = cust;
        return true;
    }

    //删除
    public boolean deleteCustomer(int index) {
   
        if (index < 0 || index >= total) {
   
            return false;
        }
        //前面删掉的后面覆盖前
        for (int i = index; i < total - 1; i++) {
   
            customers[i] = customers[i++];
        }
        //最后有数据的元素需要置空
        customers[total - 1] = null;
        total--;
//        customers[--total]=null;
        return true;
    }

    //获取所有的客户信息
    public Customer[] getAllCustomers() {
   
        Customer[] custs = new Customer[total];
        for (int i = 0; i < total; i++) {
   
            custs[i] = customers[i];
        }
        return custs;
    }

    //获取指定索引位置上的客户
    public Customer getCustomer(int index) {
   
        //没有找到指定的元素,返回null
        if (index < 0 || index >= total) {
   
            return null;
        }
        //找到指定的元素则返回
        return customers[index];
    }


    //获取客户的数量
    public int getTotal() {
   
        return total;
    }
}


类:CMUtility

package twe.wad;
import java.util.Scanner;

/**
 CMUtility工具类:
 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
 */
public class CMUtility {
   
    private static Scanner scanner = new Scanner(System.in);
    /**
     用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
     */
    public static char readMenuSelection() {
   
        char c;
        for (; ; ) {
   
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5') {
   
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
    /**
     从键盘读取一个字符,并将其作为方法的返回值。
     */
    public static char readChar() {
   
        String str = readKeyBoard(1, false);
        return str.charAt(0);
    }
    /**
     从键盘读取一个字符,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static char readChar(char defaultValue) {
   
        String str = readKeyBoard(1, true);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
    /**
     从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     */
    public static int readInt() {
   
        int n;
        for (; ; ) {
   
            String str = readKeyBoard(2, false);
            try {
   
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
   
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static int readInt(int defaultValue) {
   
        int n;
        for (; ; ) {
   
            String str = readKeyBoard(2, true);
            if (str.equals("")) {
   
                return defaultValue;
            }

            try {
   
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
   
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
     */
    public static String readString(int limit) {
   
        return readKeyBoard(limit, false);
    }
    /**
     从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static String readString(int limit, String defaultValue) {
   
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
    /**
     用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
     */
    public static char readConfirmSelection() {
   
        char c;
        for (; ; ) {
   
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值