封装性的体现,需要权限修饰符来配合的。
java规定的4个权限(从小到大排序):private、确省、protected、public
4种权限可以用来修饰及类的内部结构:属性、方法、构造器、内部类
具体的,4种权限都可以用来修饰类内部结构,属性、方法、构造器、内部类
修饰类的话,只能使用:缺省、publ ic
类:Customerpackage 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 = der = gender;this.age = age;this.phone = ail = 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) {der = 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() {ail = 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) {//没有找到指定的元素,返回nullif (index < 0 || index >= total) {return null;}//找到指定的元素则返回return customers[index];}//获取客户的数量public int getTotal() {return total;}
}类:CMUtilitypackage 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
本文发布于:2024-02-02 15:55:23,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170686052444849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |