Day18
一、刪除的思考題
思考題:刪除功能可以省略第一步嗎?不可以
有第一步判斷學生信息合法性,如果信息不合法會直接結束返回
如果沒有第一步,將會在第二步判斷是否有該學生query循環匹配查找,數據量大情況,效率很低
二、學生管理系統 --功能實現
StudentManagerSystemImpl類
查詢功能
1.根據性別查詢學生對象數組
步驟
1.驗證學生信息合法性
2.獲取符合條件的人數
3.創建新數組
4.遍歷數據源,把符合條件的學生對象存入新數組中
@Override
public Student[] getStusByName(String name) {
//1.驗證學生信息合法性
if (!StuInfoUtil.isName(name)) {
return null;
}
//2.獲取符合條件的人數
int count = 0;
for (int i = 0; i < size; i++) {
if (stus[i].getName().equals(name)) {
count++;
}
}
if (count == 0) {
return null;
}
//3.創建新數組
Student[] newStus = new Student[count];
//4.遍歷數據源,把符合條件的學生對象存入新數組中
int index = 0;
for (int i = 0; i < size; i++) {
if (stus[i].getName().equals(name)) {
newStus[index++] = stus[i];
}
}
return newStus;
}
后面幾個相似
2.根據性別查詢學生對象數組
getStusBySex判斷時用“==”,不使用equals無法對基元類型char調用equals(char)
3.根據年齡查詢學生對象數組
4.根據班級號查詢學生對象數組
queryMenu
main方法中封裝前端查詢方法(多態)
步驟
- 查詢功能展示
- 聲明一個學生數組為空
- swicth匹配對應功能
- 優化基礎辦法,提示輸入查詢條件,學生系統調用相應方法查詢,根據條件獲取到的學生信息賦值給學生數組(多態)
- 對學生數組條件判斷,查詢成功打印數組(打印數組做成默認方法放在接口中),失敗打印錯誤信息
private static void queryMenu(Scanner scan, StudentManagerSystemImpl sms) {System.out.println("請選擇查詢的條件:");System.out.println("1 - 根據姓名查詢");System.out.println("2 - 根據性別查詢");System.out.println("3 - 根據年齡查詢");System.out.println("4 - 根據班級號查詢");int num = scan.nextInt();//第二種Student[] stus = null;//2switch (num) {case 1:System.out.println("請輸入需要查詢的姓名:");String name = scan.next();//2stus = sms.getStusByName(name);//第一種最基礎的辦法 // String name = scan.next();//1 // Student[] stus = sms.getStusByName(name); // for (Student stu : stus) { // System.out.println(stu); // }break;case 2:System.out.println("請輸入需要查詢的性別:");char sex = scan.next().charAt(0);stus = sms.getStusBySex(sex);break;case 3:System.out.println("請輸入需要查詢的起始年齡(包含):");int start = scan.nextInt();System.out.println("請輸入需要查詢的結束年齡(不包含):");int end = scan.nextInt();stus = sms.getStusByAge(start, end);break;case 4:System.out.println("請輸入需要查詢的班級號:");String classId = scan.next();stus = sms.getStusByClassId(classId);break;}if (stus != null) {//2 //打印數組做成默認方法sms.printStus(stus); // for (Student stu : stus) { // System.out.println(stu); // }} else {System.out.println("查詢錯誤");}}
三、包裝類
理解:
8種基本數據類型對應類
出現原因:
Java為純面向對象語言(萬物皆對象),8種基本數據類型不能new對象,
就破壞Java為純面向對象語言的特征,Java為8種基本數據類型分別匹配
了對應的類,這種類叫做包裝類/封裝類
基本數據類型 包裝類 繼承關系 byte Byte Object.Number.Byte short Short Object.Number.Short int Integer Object.Number.Integer long Long Object.Number.Long float Float Object.Number.Float double Double Object.Number.Double char Character Object.Character boolean Boolean Object.Boolean
注意:
1.int的包裝類的寫法為:Integer
2.char的包裝類的寫法為:Charater
3.其余基本數據類型的包裝類均是基本類型的首字母大寫
裝箱和拆箱
手動裝箱:
基本數據類型 -> 包裝類
int num =100; Integer integer = Integer.valueOf(num);//靜態方法 System.out.println(integer);//String類型的100
手動拆箱:
? 包裝類 -> 基本數據類型
Integer integer = new Integer(100); int num = integer.intValue(); System.out.println(num);//int類型的100
自動裝箱
Window的show View的Navigator查看bin目錄的class文件,再反編譯查看底層
JDK1.5開始的新特性
基本數據類型 -> 包裝類
int num = 100; Integer integer = num;//底層實現:Integer.valueOf(num); System.out.println(integer);
自動拆箱
包裝類 -> 基本數據類型
Integer integer = new Integer(100); int num = integer;//底層實現:integer.intValue(); System.out.println(num);
深入Integer的底層原理
面試題
描述下列代碼的運行結果
Integer integer1 = Integer.valueOf(100);Integer integer2 = Integer.valueOf(100);System.out.println(integer1 == integer2);//引用類型比較內存地址,trueInteger integer3 = Integer.valueOf(200);Integer integer4 = Integer.valueOf(200);System.out.println(integer3 == integer4);//引用類型比較內存地址,false
面試題:描述Integer的valueOf的底層原理
答:判斷輸入的int值是否在-127~128的區間內,如果在就從Integer的緩存類數組中獲取對象
如果不在,就重新new Integer的對象MyInteger MyInteger1 = MyInteger.valueOf(100);
MyInteger MyInteger2 = MyInteger.valueOf(100);
System.out.println(MyInteger1 == MyInteger2);//trueMyInteger MyInteger3 = MyInteger.valueOf(200);
MyInteger MyInteger4 = MyInteger.valueOf(200);
System.out.println(MyInteger3 == MyInteger4);//超過范圍new對象,false
編寫MyInteger
步驟:
定義一個int變量
有參構造獲值
intValue()返回值
重寫toSting使用valueOf()將int值轉換為String字符串再返回
編寫緩存類(靜態)
在緩存類中編寫靜態代碼塊初始化緩存數組
public class MyInteger {private int value;public MyInteger(int value) {super();this.value = value;}public int intValue(){return value;}//編寫MyInteger的valueOf判斷輸入的int值是否在-127~128的區間內,如果在就從Integer的緩存類數組中獲取對象,不在就重新new Integer的對象public static MyInteger valueOf(int i){if (i>=MyIntegerCache.low && i<=MyIntegerCache.hight) {return MyIntegerCache.cache[i + (-MyIntegerCache.low)];}return new MyInteger(i);}@Overridepublic String toString() {return String.valueOf(value);//將int值轉換為String字符串}//緩存類private static class MyIntegerCache {//沒有用到外部類的成員屬性就可制成靜態private static final int low = -128;//低位private static final int hight = 127;//高位private static final MyInteger[] cache;//緩存數組(-127~128的MyInteger的對象)static{//可用代碼塊//初始化緩存數組cache = new MyInteger[hight - low +1];int j = low;for (int i = 0; i < cache.length; i++) {cache[i] = new MyInteger(j++);}}} }
測試
Integer integer = new Integer(100);System.out.println(integer);//100int num1 = integer.intValue();System.out.println(num1);//100MyInteger myInteger = new MyInteger(100);System.out.println(myInteger);//直接打印類的全路徑com.qf.package_class.MyInteger@15db9742//MyInteger中重寫toSting方法轉化value值之后,100int num2 = myInteger.intValue(); System.out.println(nm2);//100
四學生管理系統 --功能實現
StudentManagerSystemImpl類(多態,包裝類)
修改功能
注意:
用Object原因:ps:sex自動裝箱將char裝箱成Character,再將Character向上轉型成Object(多態),方法里面又要向下轉型
步驟:
判斷學生信息的合法性
判斷是否有該學生
判斷修改類型
switch判斷修改類型,對應修改功能
? 優化:利用實現類中定義的靜態常量,提高代碼可讀性(程序定死的可以用)
對應功能:判斷修改的數據的合法性
? 修改數據
@Override public int update(String classId, String id, int type, Object val) {//用Object原因:ps:sex自動裝箱將char裝箱成Character,再將Character向上轉型成Object(多態),方法里面又要向下轉型//1.判斷學生信息的合法性if (!StuInfoUtil.isClassId(classId) && !StuInfoUtil.isId(id)) {return -1;}//2.判斷是否有該學生int index = query(classId, id);if (index == -1) {return -2;}//3.判斷修改類型switch (type) {// 利用實現類中定義的靜態常量,提高代碼可讀性(程序定死的可以用)case NAME:String nameVal = (String)val; // 外部傳進來的val處理,前端可以屏蔽掉 // if (val instanceof String) { // String name = (String)val; // }//判斷修改的數據的合法性if (!StuInfoUtil.isName(nameVal)) {return -3;}//修改數據stus[index].setName(nameVal);break;case SEX:char sexVal = (Character) val;//強轉成Character類型,再自動拆箱成char類型//判斷修改的數據的合法性if (!StuInfoUtil.isSex(sexVal)) {return -3;}//修改數據stus[index].setSex(sexVal);break;case AGE:int ageVal = (Integer) val;//強轉成Integer類型,再自動拆箱成int類型//判斷修改的數據的合法性if (!StuInfoUtil.isAge(ageVal)) {return -3;}//修改數據stus[index].setAge(ageVal);break;case CLASS_ID:String classIdVal = (String)val;//判斷目標班級上是否有重復學生(即修改班級號對應班級是否已經存在學生 ps:2041 001 -> 2042 001)if (query(classIdVal, id) != -1) {return -4;}//判斷修改的數據的合法性if (!StuInfoUtil.isClassId(classIdVal)) {return -3;}//修改數據stus[index].setClassId(classIdVal);break;case ID:String idVal = (String)val;//判斷目標學號上是否有重復學生(即修改學號對應學號是否已經存在學生 ps:2041 001 -> 2041 002)if (query(classId, idVal) != -1) {return -5;}//判斷修改的數據的合法性if (!StuInfoUtil.isId(idVal)) {return -3;}//修改數據stus[index].setId(idVal);break;}return 1;}
updateMenu
main方法中封裝前端查詢方法(多態)
步驟:
- 提示輸入班級號、學號
- 查詢功能展示
- 聲明一個Object為空
- swicth匹配對應功能
- 優化基礎辦法,提示輸入查詢條件,學生系統調用相應方法查詢,根據條件獲取到的學生信息賦值給Object(多態,向上轉型)
- 獲取修改狀態碼:學生系統調用添加方法返回修改狀態碼
- 根據修改狀態碼進行條件判斷修改的情況息
private static void updateMenu(Scanner scan, StudentManagerSystemImpl sms) {System.out.println("請輸入需要修改學生的班級號:");String classId = scan.next();System.out.println("請輸入需要修改學生的學號:");String id = scan.next();System.out.println("請選擇需要修改的類型:");System.out.println("1 - 修改姓名");System.out.println("2 - 修改性別");System.out.println("3 - 修改年齡");System.out.println("4 - 修改班級號");System.out.println("5 - 修改學號");int type = scan.nextInt();System.out.println("請輸入修改的值");Object val = null;switch (type) {case 1:case 4:case 5:// String val = scan.next();val = scan.next();//String -> Objectbreak;case 2:val = scan.next().charAt(0);//char -> Character -> Objectbreak;case 3:val = scan.nextInt();//int -> Integer -> Objectbreak;default:System.out.println("修改失敗 -- 修改類型錯誤");return;}int updateCode = sms.update(classId, id, type, val);if (updateCode == -1) {System.out.println("修改失敗 -- 學生信息不合法");}else if (updateCode == -2) {System.out.println("修改失敗 -- 沒有該學生"); }else if (updateCode == -3) {System.out.println("修改失敗 -- 修改值的信息不合法"); }else if (updateCode == -4) {System.out.println("修改失敗 -- 目標班級上有學生"); }else if (updateCode == -5) {System.out.println("修改失敗 -- 目標學號上有學生"); }else if (updateCode == 1) {System.out.println("修改成功"); }}
總結
1.學生管理系統 – 刪除的思考題
2.學生管理系統 – 查詢功能
3.包裝類
概念
出現原因
手動裝箱、手動拆箱
自動裝箱、自動拆行
深入Integer的底層原理 – 重要!!!!4.學生管理系統 – 修改功能