java概述 转义字符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class ChangeChar { public static void main (String[] args) { System.out.println("北京\t天津\t上海" ); System.out.println("ab\ncd\nef" ); System.out.println("北京\\天津\\\\上海" ); System.out.println("北京\"天津\"上海" ); System.out.println("北京\'天津\'上海" ); System.out.println("北京天津\r上海" ); System.out.println("北京天津\r\n上海" ); System.out.println("书名\t作者\t价格\t销量\n三国\t罗贯中\t120\t1000" ); } }
文档注释 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Comment02 { public static void main (String[] args) { } }
在cmd中输入javadoc -d d:\\temp -author -version Comment02.java
生成文档注释
DOS md d:\\test
创建目录/文件夹
rd d:\\test
删除目录/文件夹
dir :查看当前目录有什么内容
cd : 切换到其他盘下 例:cd \D c:
.. : 切换到上一级目录 例: cd ..
cd \ : 切换到根目录
tree :查看指定的目录下的所有的子级目录 例: tree d:
cls : 清屏
exit :退出DOS
echo :输入内容到文件 例:echo hello > hello.txt
copy: 拷贝
del : 删除文件
move :剪切
变量 加法 1 2 3 4 5 6 7 8 9 10 public class Plus { public static void main (String[] args) { System.out.println(100 +98 ); System.out.println("100" +98 ); System.out.println(100 +3 +"hello" ); System.out.println("hello" +100 +3 ); } }
数据类型
boolean类型不可以0或非0的整数替代false和true,这点和C语言不同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class StringToBasic { public static void main (String[] args) { int n1 = 100 ; float f1 = 1.1F ; double d1 = 4.5 ; boolean b1 = true ; String s1 = n1 + "" ; String s2 = f1 + "" ; String s3 = d1 + "" ; String s4 = b1 + "" ; System.out.println(s1 + " " + s2 + " " + s3 + " " + s4); String s5 = "123" ; int num1 = Integer.parseInt(s5); double num2 = Double.parseDouble(s5); float num3 = Float.parseFloat(s5); long num4 = Long.parseLong(s5); byte num5 = Byte.parseByte(s5); boolean b = Boolean.parseBoolean("true" ); short num6 = Short.parseShort(s5); System.out.println("===================" ); System.out.println(num1); System.out.println(num2); System.out.println(num3); System.out.println(num4); System.out.println(num5); System.out.println(num6); System.out.println(b); System.out.println(s5.charAt(0 )); } }
运算符 算术运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class ArithmeticOperatorExercise { public static void main (String[] args) { int days = 505 ; int weeks = days / 7 ; int leftDays = days % 7 ; System.out.println("还有" +days+"天放假,合" +weeks+"个星期零" +leftDays+"天" ); double huaShi = 234.5 ; double sheShi = 5.0 / 9 * (huaShi - 100 ); System.out.println("华氏温度" + huaShi+ " 对应的摄氏温度=" + sheShi); } }
关系运算符 也称比较运算符,结果都是boolean型。
逻辑运算符 结果都是boolean型
&& 和 & 使用区别
1) &&短路与:如果第一个条件为 false,则第二个条件不会判断,最终结果为 false,效率高 2) & 逻辑与:不管第一个条件是否为 false,第二个条件都要判断,效率
|| 和 | 使用区别
||短路或:如果第一个条件为 true,则第二个条件不会判断,最终结果为 true,效率高
| 逻辑或:不管第一个条件是否为 true,第二个条件都要判断,效率低
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class LogicOperator { public static void main (String[] args) { int x = 5 ; int y = 5 ; if (x++==6 & ++y==6 ){ x = 11 ; } System.out.println("x=" + x + "y=" + y); boolean i = true ; boolean j = false ; short z = 46 ; if ((z++==46 )&&(j=true )) z++; if ((i=false )||(++z==49 )) z++; System.out.println("z=" + z); } }
赋值运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class AssignOperator { public static void main (String[] args) { int n1 = 10 ; n1 += 4 ; System.out.println(n1); n1 /= 3 ; System.out.println(n1); byte b = 3 ; b += 2 ; b++; } }
三元运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class TernaryOperator { public static void main (String[] args) { int a = 10 ; int b = 99 ; int result = a > b ? a++ : b--; System.out.println("result=" + result); System.out.println("a=" + a); System.out.println("b=" + b); } }
运算符优先级 上一行运算符总优先先下一行
键盘输入语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.Scanner;public class Input { public static void main (String[] args) { Scanner myScanner = new Scanner (System.in); System.out.println("请输入名字" ); String name = myScanner.next(); System.out.println("请输入年龄" ); int age = myScanner.nextInt(); System.out.println("请输入薪水" ); double sal = myScanner.nextDouble(); System.out.println("人的信息如下:" ); System.out.println("名字=" + name + " 年龄=" + age + " 薪水=" + sal); } }
进制 二进制转换成八进制
规则:从低位开始,将二进制数每三位一组,转成对应的八进制数即可。
案例:请将 ob11010101 转成八进制 ob11(3)010(2)101(5) => 0325
二进制转换成十六进制
规则:从低位开始,将二进制数每四位一组,转成对应的十六进制数即可。
案例:请将 ob11010101 转成十六进制 ob1101(D)0101(5) = 0xD5
八进制转换成二进制
规则:将八进制数每 1 位,转成对应的一个 3 位的二进制数即可。 案例:请将 0237 转成二进制 02(010)3(011)7(111) = 0b10011111
十六进制转换成二进制
规则:将十六进制数每 1 位,转成对应的 4 位的一个二进制数即可。
案例:请将 0x23B 转成二进制 0x2(0010)3(0011)B(1011) = 0b001000111011
原码、反码、补码
程序控制结构 顺序控制 分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import java.util.Scanner;public class MyTest { public static void main (String[] args) { Scanner myScanner = new Scanner (System.in); System.out.println("请输入月份" ); int month = myScanner.nextInt(); System.out.println("请输入年龄" ); int age = myScanner.nextInt(); if (month >= 4 && month <= 10 ){ if (age >= 18 && age <= 60 ) { System.out.println("票价为60元" ); } else if (age < 18 ){ System.out.println("票价为30元" ); } else { System.out.println("票价为20元" ); } } else { if (age >= 18 && age <= 60 ) { System.out.println("票价为40元" ); } else { System.out.println("票价为20元" ); } } } }
switch语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java.util.Scanner;public class Switch01 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入a-g" ); char c1 = scanner.next().charAt(0 ); switch (c1) { case 'a' : System.out.println("星期一" ); break ; case 'b' : System.out.println("星期二" ); break ; case 'c' : System.out.println("星期三" ); break ; case 'd' : System.out.println("星期四" ); break ; case 'e' : System.out.println("星期五" ); break ; case 'f' : System.out.println("星期六" ); break ; case 'g' : System.out.println("星期七" ); break ; default : System.out.println("错误" ); break ; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 import java.util.Scanner;public class SwitchExercise { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); char c = scanner.next().charAt(0 ); switch (c) { case 'a' : System.out.println('A' ); break ; case 'b' : System.out.println('B' ); break ; case 'c' : System.out.println('C' ); break ; case 'd' : System.out.println('D' ); break ; case 'e' : System.out.println('E' ); break ; default : System.out.println("other" ); break ; } int score = scanner.nextInt(); if (score >= 0 && score <= 100 ) { int s = score / 60 ; switch (s) { case 1 : System.out.println("合格" ); break ; case 0 : System.out.println("不合格" ); break ; default : System.out.println("分数错误" ); break ; } } int month = scanner.nextInt(); switch (month) { case 3 : case 4 : case 5 : System.out.println("春季" ); break ; case 6 : case 7 : case 8 : System.out.println("夏季" ); break ; case 9 : case 10 : case 11 : System.out.println("秋季" ); break ; case 12 : case 1 : case 2 : System.out.println("冬季" ); break ; } } }
switch 和 if 的比较
1) 如果判断的具体数值不多,而且符合 byte、 short 、int、 char、enum[枚举]、String 这 6 种类型。虽然两个语句都可以使用,建议使用 swtich 语句。 2) 其他情况:对区间判断,对结果为 boolean 类型判断,使用 if,if 的使用范围更广。
循环控制 for 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class ForExercise { public static void main (String[] args) { int sum = 0 ; int count = 0 ; int start = 1 ; int end = 100 ; for (int i = start; i < end; i++) { if (i % 9 == 0 ) { System.out.println(i); count++; sum += i; } } System.out.println("count: " + count); System.out.println("sum: " + sum); for (int i = 0 , j = 5 ; i <= 5 && j >= 0 ; i++, j--) { System.out.println(i + " + " + j + " = " + (i + j)); } int n=9 ; for (int i = 0 ; i < n; i++) { System.out.println(i + " + " + (n-i) + " = " + n); } } }
while 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class WhileExercise { public static void main (String[] args) { int start = 1 ; int end = 100 ; int step = 3 ; while (start <= end){ if (start % step == 0 ){ System.out.println(start); } start++; } start = 40 ; end = 200 ; step = 2 ; while (start <= end){ if (start % step == 0 ){ System.out.println(start); } start++; } } }
do-while 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.util.Scanner;public class DoWhileExercise { public static void main (String[] args) { int start = 1 ; int end = 100 ; int sum = 0 ; do { System.out.println(start); sum += start; start++; } while (start <= end); System.out.println(sum); start = 1 ; end = 200 ; int count = 0 ; do { if (start % 5 == 0 && start % 3 != 0 ) { System.out.println(start); count++; } start++; } while (start <= end); System.out.println(count); Scanner scanner = new Scanner (System.in); char c; do { System.out.println("老韩问:还钱吗?y/n" ); c = scanner.next().charAt(0 ); } while (c != 'y' ); } }
多重循环控制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import java.util.Scanner;public class MulForExercise { public static void main (String[] args) { for (int i = 1 ; i <= 9 ; i++) { for (int j = 1 ; j <= i; j++) { System.out.print(j + " * " + i + " = " + (i * j) + "\t" ); } System.out.println(); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public class Stars { public static void main (String[] args) { for (int i = 1 ; i <= 5 ; i++) { for (int k = 1 ; k <= 5 - i; k++) { System.out.print(" " ); } for (int j = 1 ; j <= 2 * i - 1 ; j++) { System.out.print("*" ); } System.out.println(); } int level = 5 ; for (int i = 1 ; i <= level; i++) { for (int k = 1 ; k <= level - i; k++) { System.out.print(" " ); } for (int j = 1 ; j <= 2 * i - 1 ; j++) { if (j == 1 || j == 2 * i - 1 || i == level) { System.out.print("*" ); } else { System.out.print(" " ); } } System.out.println("" ); } } }
break 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.util.Scanner;public class BreakExercise { public static void main (String[] args) { int chances = 3 ; for (int i = 1 ; i <= chances; i++) { Scanner scanner = new Scanner (System.in); System.out.println("输入用户名" ); String username = scanner.next(); System.out.println("输入密码" ); String password = scanner.next(); if (username.equals("麦子落" ) && "666" .equals(password)) { System.out.println("登录成功" ); break ; } System.out.println("你还有" + (chances - i) + "次机会" ); } } }
本章小结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 public class ControlTest { public static void main (String[] args) { double cash = 100000.0 ; int count = 0 ; while (true ) { if (cash > 50000 ) { cash = 0.95 * cash; count++; } else if (cash >= 1000 ) { cash -= 1000 ; count++; } else { break ; } } System.out.println("count:" + count); for (int i = 100 ; i < 1000 ; i++) { int i1 = i / 100 ; int i2 = i % 100 / 10 ; int i3 = i % 10 ; if (i1 * i1 * i1 + i2 * i2 * i2 + i3 * i3 * i3 == i) { System.out.println(i + "是水仙花数" ); } } count = 0 ; for (int i = 1 ; i <= 100 ; i++) { if (i % 5 != 0 ) { count++; System.out.print(i + "\t" ); if (count % 5 == 0 ) { System.out.println("" ); } } } for (char c1 = 'a' ; c1 <= 'z' ; c1++) { System.out.print(c1 + " " ); } System.out.println("" ); for (char c2 = 'Z' ; c2 >= 'A' ; c2--) { System.out.print(c2 + " " ); } System.out.println("" ); double sum = 0 ; for (int i = 1 ; i <= 100 ; i++) { if (i % 2 == 0 ) { sum -= 1.0 / i; } else { sum += 1.0 / i; } } System.out.println("sum = " + sum); int sum6 = 0 ; for (int i = 1 ; i <= 100 ; i++) { for (int j = 1 ; j <= i; j++) { sum6 += j; } } System.out.println("sum6 = " + sum6); } }
数组、排序和查找 数组 数组使用注意事项和细节 1) 数组是多个相同类型数据的组合,实现对这些数据的统一管理 2) 数组中的元素可以是任何数据类型,包括基本类型和引用类型,但是不能混用。 3) 数组创建后,如果没有赋值,有默认值 int 0,short 0, byte 0, long 0, float 0.0,double 0.0,char \u0000,boolean false,String null 4) 使用数组的步骤 1. 声明数组并开辟空间 2 给数组各个元素赋值 3 使用数组 5) 数组的下标是从 0 开始的。 6) 数组下标必须在指定范围内使用,否则报:下标越界异常,比如 韩顺平循序渐进学 Java 零基础 第 148页 int [] arr=new int[5]; 则有效下标为 0-4 7) 数组属引用类型,数组型数据是对象(object)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class ArrayExercise { public static void main (String[] args) { char [] array = new char [26 ]; for (int i = 0 ; i < array.length; i++) { array[i] = (char ) ('A' + i); } for (int i = 0 ; i < array.length; i++) { System.out.print(array[i] + " " ); } System.out.println("" ); int [] array2 = {4 , -1 , 8 , 10 , 23 }; int max = array2[0 ]; int index = 0 ; int sum=0 ; double avg=0.0 ; for (int i = 0 ; i < array2.length; i++) { if (array2[i] > max) { max = array2[i]; index = i; } sum+=array2[i]; } avg=(double )sum/array2.length; System.out.println("最大值为: " + max + " 下标为: " + index); System.out.println("和为: " + sum + " 平均值为: " + avg); } }
数组赋值机制 1) 基本数据类型赋值,这个值就是具体的数据,而且相互不影响。 int n1 = 2; int n2 = n1; 2) 数组在默认情况下是引用传递,赋的值是地址。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class ArrayAssign { public static void main (String[] args) { int n1 = 10 ; int n2 = n1; n2 = 80 ; System.out.println("n1=" + n1); System.out.println("n2=" + n2); int [] arr1 = {1 , 2 , 3 }; int [] arr2 = arr1; arr2[0 ] = 10 ; System.out.println("====arr1的元素====" ); for (int i = 0 ; i < arr1.length; i++) { System.out.println(arr1[i]); } System.out.println("====arr2的元素====" ); for (int i = 0 ; i < arr2.length; i++) { System.out.println(arr2[i]); } } }
数组翻转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class ArrayReverse { public static void main (String[] args) { int arr[] = {11 , 22 , 33 , 44 , 55 , 66 }; int temp = 0 ; int len = arr.length; for (int i = 0 ; i < len / 2 ; i++) { temp = arr[i]; arr[i] = arr[len - i - 1 ]; arr[len - 1 - i] = temp; } for (int i = 0 ; i < len; i++) { System.out.print(arr[i] + " " ); } System.out.println(); int []arr2 = new int [len]; for (int i = len-1 ;i >= 0 ;i--) { arr2[len - 1 - i] = arr[i]; } for (int i = 0 ; i < len; i++) { System.out.print(arr2[i] + " " ); } } }
数组缩减 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import java.util.Scanner;public class ArrayReduce { public static void main (String[] args) { Scanner myScanner = new Scanner (System.in); int [] arr = {1 , 2 , 3 , 4 , 5 }; do { System.out.println("是否进行缩减 y/n" ); char key = myScanner.next().charAt(0 ); if (key == 'n' ) { break ; } if (arr.length == 1 ) { System.out.println("只有一个元素,不再缩减" ); break ; } int [] arrNew = new int [arr.length - 1 ]; for (int i = 0 ; i < arr.length - 1 ; i++) { arrNew[i] = arr[i]; } arr = arrNew; System.out.println("====arr 缩减后元素情况====" ); for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "\t" ); } } while (true ); System.out.println("结束..." ); } }
排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class BubbleSort { public static void main (String[] args) { int arr[] = {24 , 69 , 80 , 57 , 13 , 84 , 10 , 5 }; int len = arr.length; int temp = 0 ; for (int j = len; j > 1 ; j--) { for (int i = 0 ; i < j - 1 ; i++) { if (arr[i] > arr[i + 1 ]) { temp = arr[i]; arr[i] = arr[i + 1 ]; arr[i + 1 ] = temp; } } } for (int i = 0 ; i < len; i++) { System.out.print(arr[i] + " " ); } } }
查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.Scanner;public class SeqSearch { public static void main (String[] args) { String arr[] = {"白毛" , "金毛" , "紫毛" , "青毛" , "黑毛" }; Scanner scanner = new Scanner (System.in); String str = scanner.next(); int index = -1 ; for (int i = 0 ; i < arr.length; i++) { if (arr[i].equals(str)) { System.out.println(str + "找到了,下标为:" + i); index = i; break ; } } if (index == -1 ) { System.out.println(str + "未找到" ); } } }
二维数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class TwoDimensionalArray { public static void main (String[] args) { int arr[][] = {{4 , 6 }, {1 , 4 , 5 , 7 }, {-2 }}; int sum = 0 ; for (int i = 0 ; i < arr.length; i++) { System.out.print("第" + (i + 1 ) + "个一维数组为:" ); for (int j = 0 ; j < arr[i].length; j++) { System.out.print(arr[i][j] + " " ); sum += arr[i][j]; } System.out.println(); } System.out.println("和为:" + sum); int n = 10 ; int yanghui[][] = new int [n][]; for (int i = 0 ; i < n; i++) { yanghui[i] = new int [i + 1 ]; for (int j = 0 ; j < yanghui[i].length; j++) { if (j == 0 || j == yanghui[i].length - 1 ) { yanghui[i][j] = 1 ; } else { yanghui[i][j] = yanghui[i - 1 ][j - 1 ] + yanghui[i - 1 ][j]; } } } for (int i = 0 ; i < yanghui.length; i++) { for (int j = 0 ; j < yanghui[i].length; j++) { System.out.print(yanghui[i][j] + "\t" ); } System.out.println("" ); } } }
本章小结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 public class ASFHomework { public static void main (String[] args) { int arr[] = {10 , 12 , 45 , 90 }; int index = arr.length; int num = 23 ; for (int i = 0 ; i < arr.length; i++) { if (num < arr[i]) { index = i; break ; } } int [] newArr = new int [arr.length + 1 ]; for (int i = 0 , j = 0 ; i < newArr.length; i++) { if (i != index) { newArr[i] = arr[j]; j++; } else { newArr[i] = num; } } for (int i = 0 ; i < newArr.length; i++) { System.out.print(newArr[i] + " " ); } System.out.println("" ); int [] arr2 = new int [10 ]; int sum2 = 0 ; int max2 = 0 ; int index2 = 0 ; for (int i = 0 ; i < arr2.length; i++) { arr2[i] = (int ) (Math.random() * 100 ) + 1 ; sum2 += arr2[i]; if (arr2[i] > max2) { max2 = arr2[i]; index2 = i; } } boolean ifHave8 = false ; for (int i = arr2.length - 1 ; i >= 0 ; i--) { System.out.print(arr2[i] + " " ); if (arr2[i] == 8 ) { ifHave8 = true ; } } System.out.println(); if (ifHave8 == true ) { System.out.println("有8" ); } else { System.out.println("没有8" ); } System.out.println("平均值为:" + (double ) sum2 / arr2.length); System.out.println("最大值为: " + max2 + " 下标为: " + index2); } }
面向对象编程(基础部分) 类与对象 Java 内存的结构分析 1) 栈:一般存放基本数据类型(局部变量) 2) 堆:存放对象(Cat cat , 数组等) 3) 方法区:常量池(常量,比如字符串), 类加载信息
Java 创建对象的流程简单分析 1 2 3 4 5 Person p = new Person (); p.name = “jack”; p.age = 10
1) 先加载 Person 类信息(属性和方法信息, 只会加载一次) 2) 在堆中分配空间, 进行默认初始化(看规则) 3) 把地址赋给 p , p 就指向对象 4) 进行指定初始化, 比如 p.name =”jack” p.age = 1
成员方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Method { public static void main (String[] args) { AA a = new AA (); int num = 9 ; boolean index = a.isOdd(num); if (index) { System.out.println(num + "是奇数" ); } else { System.out.println(num + "是偶数" ); } a.print(4 , 5 , '$' ); } } class AA { public boolean isOdd (int num) { return num % 2 != 0 ; } public void print (int row, int col, char c) { for (int i = 0 ; i < row; i++) { for (int j = 0 ; j < col; j++) { System.out.print(c); } System.out.println(); } } }
方法传参机制 1.基本数据类型,传递的是值(值拷贝),形参的任何改变不影响实参。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class MethodParameter01 { public static void main (String[] args) { int a = 10 ; int b = 20 ; AA obj = new AA (); obj.swap(a, b); System.out.println("main 方法 a=" + a + " b=" + b); } } class AA { public void swap (int a, int b) { System.out.println("\na 和 b 交换前的值\na=" + a + "\tb=" + b); int tmp = a; a = b; b = tmp; System.out.println("\na 和 b 交换后的值\na=" + a + "\tb=" + b); } }
2.引用类型传递的是地址(传递也是值,但是值是地址),可以通过形参影响实参!
3.成员方法返回类型是引用类型应用实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MethodParameter01 { public static void main (String[] args) { Person p1 = new Person (); p1.name = "John" ; p1.age = 18 ; Person p2 = new Person (); p2=p2.copyPerson(p1); System.out.println("p1 " +p1.name+" " +p1.age); System.out.println("p2 " +p2.name+" " +p2.age); System.out.println(p1 == p2); } } class Person { String name; int age; public Person copyPerson (Person p1) { Person p2= new Person (); p2.name = p1.name; p2.age = p1.age; return p2; } }
方法递归调用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class recursion { public static void main (String[] args) { T t1 = new T (); t1.test(4 ); System.out.println(t1.factorial(5 )); } } class T { public void test (int n) { if (n > 2 ) { test(n - 1 ); } System.out.println("n = " + n); } public int factorial (int n) { if (n == 1 ) { return 1 ; } else { return factorial(n - 1 ) * n; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package ClassAndObject;public class RecursionExercise { public static void main (String[] args) { TT t1 = new TT (); System.out.println(t1.fibonacci(5 )); System.out.println(t1.peach(1 )); } } class TT { public int fibonacci (int n) { if (n <= 2 ) { return 1 ; } else { return fibonacci(n - 1 ) + fibonacci(n - 2 ); } } public int peach (int day) { if (day == 10 ) { return 1 ; } else if (day >= 1 && day <= 9 ) { return peach(day + 1 ) * 2 + 2 ; } else { System.out.println("day 在 1-10" ); return -1 ; } } }
迷宫问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 package ClassAndObject;public class MiGong { public static void main (String[] args) { int map[][] = new int [8 ][7 ]; for (int i = 0 ; i < 7 ; i++) { map[0 ][i] = 1 ; map[7 ][i] = 1 ; } for (int i = 0 ; i < 8 ; i++) { map[i][0 ] = 1 ; map[i][6 ] = 1 ; } map[3 ][1 ] = 1 ; map[3 ][2 ] = 1 ; map[2 ][2 ] = 1 ; for (int i = 0 ; i < 8 ; i++) { for (int j = 0 ; j < 7 ; j++) { System.out.print(map[i][j] + " " ); } System.out.println("" ); } TMiGong t1 = new TMiGong (); t1.findWay(map, 1 , 1 ); System.out.println("找路后" ); for (int i = 0 ; i < 8 ; i++) { for (int j = 0 ; j < 7 ; j++) { System.out.print(map[i][j] + " " ); } System.out.println("" ); } } } class TMiGong { public boolean findWay (int [][] map, int i, int j) { if (map[6 ][5 ] == 2 ) { return true ; } else { if (map[i][j] == 0 ) { map[i][j] = 2 ; if (findWay(map, i + 1 , j)) { return true ; } else if (findWay(map, i, j + 1 )) { return true ; } else if (findWay(map, i - 1 , j)) { return true ; } else if (findWay(map, i, j - 1 )) { return true ; } else { map[i][j] = 3 ; return false ; } } else { return false ; } } } }
汉诺塔问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package ClassAndObject;public class HanoiTower { public static void main (String[] args) { Tower tower = new Tower (); tower.move(4 , 'A' , 'B' , 'C' ); } } class Tower { public void move (int num, char a, char b, char c) { if (num == 1 ) { System.out.println(a + "->" + c); } else { move(num - 1 , a, c, b); System.out.println(a + "->" + c); move(num - 1 , b, a, c); } } }
八皇后问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 package ClassAndObject;public class Queen8 { public static void main (String[] args) { Queen8 queen8 = new Queen8 (); queen8.check(0 ); System.out.printf("一共有%d解法" , count); System.out.println(); System.out.printf("一共判断冲突的次数%d次" , judgeCount); } int max = 8 ; int [] array = new int [max]; static int count = 0 ; static int judgeCount = 0 ; private void check (int n) { if (n == max) { print(); return ; } for (int i = 0 ; i < max; i++) { array[n] = i; if (judge(n)) { check(n + 1 ); } } } private boolean judge (int n) { judgeCount++; for (int i = 0 ; i < n; i++) { if (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) { return false ; } } return true ; } private void print () { count++; for (int i = 0 ; i < array.length; i++) { System.out.print(array[i] + " " ); } System.out.println(); } }
重载 1) 方法名:必须相同 2) 形参类型:必须不同(形参类型或个数或顺序,至少有一个不同,参数名无要求) 3) 返回类型:无要求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public class OverLoad00 { public static void main (String[] args) { OverLoadExercise ole = new OverLoadExercise (); ole.m(5 ); ole.m(4 , 6 ); ole.m("hello world" ); System.out.println(ole.max(4 , 10 )); System.out.println(ole.max(4.9 , -10.8 )); System.out.println(ole.max(2.5 , 5.2 , 1.7 )); } } class OverLoadExercise { public void m (int n1) { System.out.println(n1 + "的平方 = " + n1 * n1); } public void m (int n1, int n2) { System.out.println(n1 + " * " + n2 + " = " + n1 * n2); } public void m (String s1) { System.out.println(s1); } public int max (int n1, int n2) { return n1 > n2 ? n1 : n2; } public double max (double n1, double n2) { return n1 > n2 ? n1 : n2; } public double max (double n1, double n2, double n3) { if (n1 > n2) { return n1 > n3 ? n1 : n3; } else { return n2 > n3 ? n2 : n3; } } }
可变参数 java 允许将同一个类中多个同名同功能但参数个数不同的方法,封装成一个方法。
可变参数的实参可以为0个或任意多个。
可变参数的实参可以是数组。
可变参数的本质就是数组。
可变参数可以和普通类型的参数一起放在形参列表,但必须保证可变参数在最后。
一个形参列表中只能出现一个可变参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class VarParameter { public static void main (String[] args) { VarParameterEXercise vpe = new VarParameterEXercise (); System.out.println(vpe.sum(5 , 4 , 3 , 8 , 1 )); int [] arr = {8 , 1 , 2 , 3 }; System.out.println(vpe.sum(arr)); System.out.println(vpe.showScore("Bob" ,60.2 ,87.1 ,88.8 )); } } class VarParameterEXercise { public int sum (int ... nums) { System.out.println("接收的参数个数=" + nums.length); int res = 0 ; for (int i = 0 ; i < nums.length; i++) { res += nums[i]; } return res; } public String showScore (String name, double ... scores) { double totalScore = 0.0 ; for (int i = 0 ; i < scores.length; i++) { totalScore += scores[i]; } return name + " 有 " + scores.length + "门课的成绩总分为=" + totalScore; } }
作用域
构造器 基本语法:
[修饰符] 方法名(形参列表){ 方法体; }
1) 构造器的修饰符可以默认, 也可以是 public protected private 2) 构造器没有返回值 3) 方法名和类名字必须一样 4) 参数列表和成员方法一样的规则 5) 构造器的调用,由系统完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class Constructor { public static void main (String[] args) { ConstructorExercise ce = new ConstructorExercise ("Jack" , 22 ); ConstructorExercise ce2 = new ConstructorExercise (); System.out.println(ce.name + ce.age); System.out.println(ce2.name + ce2.age); } } class ConstructorExercise { String name; int age; public ConstructorExercise () { System.out.println("无参构造器被调用~~" ); age = 18 ; } public ConstructorExercise (String pName, int pAge) { System.out.println("构造器被调用~~" ); name = pName; age = pAge; } } class ConstructorExercise01 {}
this 1) this 关键字可以用来访问本类的属性、方法、构造器 2) this 用于区分当前类的属性和局部变量 3) 访问成员方法的语法:this.方法名(参数列表); 4) 访问构造器语法:this(参数列表); 注意只能在构造器中使用(即只能在构造器中访问另外一个构造器, 必须放在第一 条语句) 5) this 不能在类定义的外部使用,只能在类定义的方法中使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class This { public static void main (String[] args) { ThisPerson p1 = new ThisPerson ("Jack" , 20 ); ThisPerson p2 = new ThisPerson ("Jack" , 20 ); System.out.println(p1.compareTo(p2)); } } class ThisPerson { String name; int age; public ThisPerson (String name, int age) { this .name = name; this .age = age; } public boolean compareTo (ThisPerson other) { return this .name.equals(other.name) && other.age == this .age; } }
本章小结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 package ClassAndObject;import java.util.Random;import java.util.Scanner;public class Homework { public static void main (String[] args) { double [] dArr = {2.0 , 1.8 , 5.4 , 1.7 , 0.2 }; A01 a01 = new A01 (); System.out.println(a01.max(a01.max(dArr))); String[] sArr = {"draw" , "front" , "back" , "raise" }; System.out.println(a01.find("front" , sArr)); Book01 book = new Book01 ("笑傲江湖" , 300 ); book.info(); book.updatePrice(); book.info(); int [] iArr = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; int [] newArr = a01.copyArr(iArr); for (int i = 0 ; i < newArr.length; i++) { System.out.print(newArr[i] + " " ); } System.out.println(); Circle01 circle = new Circle01 (3.5 ); System.out.println("半径为 " + circle.r + " 周长为: " + circle.getGirth() + " 面积为: " + circle.getArea()); Cale cale = new Cale (15 , 3 ); System.out.println("和" + cale.add() + "\t差" + cale.sub() + "\t积" + cale.mul() + "\t商" + cale.div()); PassObject po = new PassObject (); Circle01 c = new Circle01 (1 ); po.printAreas(c, 5 ); Game01 game = new Game01 (); for (int i = 0 ; i < 5 ; i++) { System.out.println("第 " + (i + 1 ) + " 局" ); System.out.println("请输入你的选择:0-石头 1-剪刀 2-布" ); Scanner scanner = new Scanner (System.in); int pSelect = scanner.nextInt(); game.person(pSelect); game.print(); } } } class A01 { public double max (double ... values) { if (values.length == 0 ) { System.out.println("数组为空" ); return 0 ; } else { double max = values[0 ]; for (int i = 1 ; i < values.length; i++) { if (values[i] > max) { max = values[i]; } } return max; } } public int find (String s1, String... values) { for (int i = 0 ; i < values.length; i++) { if (values[i].equals(s1)) { return i; } } return -1 ; } public int [] copyArr(int [] iArr) { int [] newArr = new int [iArr.length]; for (int i = 0 ; i < iArr.length; i++) { newArr[i] = iArr[i]; } return newArr; } } class Book01 { String name; double price; public Book01 (String name, double price) { this .name = name; this .price = price; } public void updatePrice () { if (price > 150 ) { price = 150 ; } else if (price > 100 ) { price = 100 ; } } public void info () { System.out.println("书名=" + this .name + " 价格=" + this .price); } } class Circle01 { double r; public Circle01 (double r) { this .r = r; } public double getGirth () { return Math.PI * 2 * r; } public double getArea () { return Math.PI * r * r; } } class Cale { double num1; double num2; public Cale (double num1, double num2) { this .num1 = num1; this .num2 = num2; } public double add () { return num1 + num2; } public double sub () { return num1 - num2; } public double mul () { return num1 * num2; } public Double div () { if (num2 == 0 ) { System.out.println("num2 不能为0" ); return null ; } else { return num1 / num2; } } } class PassObject { public void printAreas (Circle01 c, int times) { for (int i = 1 ; i <= times; i++) { c.r = i; System.out.println("半径为: " + (double ) i + " 面积为:" + c.getArea()); } System.out.println(c.r); } } class Game01 { int win = 0 ; int lose = 0 ; int draw = 0 ; int pSelect = 0 ; int cSelect = 0 ; public void person (int pSelect) { this .pSelect = pSelect; } public void computer () { Random r = new Random (); this .cSelect = r.nextInt(3 ); } public void judge () { if ((pSelect == 0 && cSelect == 1 ) || (pSelect == 1 && cSelect == 2 ) || (pSelect == 2 && cSelect == 0 )) { System.out.println("你赢了" ); this .win++; } else if (pSelect == cSelect) { System.out.println("平局" ); this .draw++; } else { System.out.println("你输了" ); this .lose++; } } public void print () { computer(); System.out.println("玩家出: " + pSelect + " 电脑出:" + cSelect); judge(); System.out.println("玩家赢: " + win + " 局\t玩家输: " + lose + " 局\t平: " + draw + " 局" ); } }
面向对象编程(中级部分) 包 作用:
区分相同名字的类
当类很多时,可以很好的管理类
控制访问范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.pkg;import java.util.Scanner;import java.util.Arrays;public class PkgDetail { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); int [] arr = {0 , -1 , 1 }; Arrays.sort(args); } }
访问修饰符 java 提供四种访问控制修饰符号,用于控制方法和属性(成员变量)的访问权限(范围):
1) 公开级别:用 public 修饰,对外公开 。 2) 受保护级别:用 protected 修饰,对子类和同一个包中的类公开。 3) 默认级别:没有修饰符号,向同一个包的类公开。 4) 私有级别:用 private 修饰,只有类本身可以访问,不对外公开。
封装 封装(encapsulation)就是把抽象的数据【属性】和对数据的操作【方法】封装在一起,数据被保护在内部,程序的其他部分只有通过被授权的操作【方法】,才能对数据进行操作。
封装的实现步骤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 package intermediate.encap;public class Encap01 { public static void main (String[] args) { Person person = new Person (); person.setName("John" ); person.setAge(300 ); person.setSalary(30000 ); System.out.println(person.info()); Person person2 = new Person ("Smith11111" ,40 ,20000 ); System.out.println(person2.info()); } } class Person { public String name; private int age; private double salary; public Person () { } public Person (String name, int age, double salary) { setName(name); setAge(age); setSalary(salary); } public String getName () { return name; } public void setName (String name) { if (name.length() >= 2 &&name.length()<=6 ) { this .name = name; }else { System.out.println("name length must be between 2 and 6" ); } } public int getAge () { return age; } public void setAge (int age) { if (age >= 1 && age <= 120 ) { this .age = age; } else { System.out.println("error:Age must in 1 to 120" ); this .age = 0 ; } } public double getSalary () { return salary; } public void setSalary (double salary) { this .salary = salary; } public String info () { return "name=" + name + " age=" + age + " salary=" + salary; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 package intermediate.encap;public class AccountTest { public static void main (String[] args) { Account account = new Account (); account.setName("jack" ); account.setBalance(60 ); account.setPassword("123456" ); account.showInfo(); } } class Account { private String name; private double balance; private String password; public Account () { } public Account (String name, double balance, String password) { setName(name); setBalance(balance); setPassword(password); } public String getName () { return name; } public void setName (String name) { if (name.length()>=2 &&name.length()<=4 ){ this .name = name; } else { System.out.println("name must be between 2 and 4 characters" ); this .name ="" ; } } public double getBalance () { return balance; } public void setBalance (double balance) { if (balance>=20 ){ this .balance = balance; } else { System.out.println("balance must >= 20" ); this .balance=0 ; } } public String getPassword () { return password; } public void setPassword (String password) { if (password.length()==6 ){ this .password = password; } else { System.out.println("password must be 6 characters,Default password is 000000" ); this .password= String.valueOf(000000 ); } } public void showInfo () { System.out.println("account information: \nname:" + name + " balance:" + balance + " password:" + password); } }
继承 继承可以解决代码复用,让我们的编程更加靠近人类思维.当多个类存在相同的属性(变量)和方法时,可以从这些类中抽象出父类,在父类中定义这些相同的属性和方法,所有的子类不需要重新定义这些属性和方法,只需要通过 extends 来 声明继承父类即可。
基本语法:
class 子类 extends 父类{
}
(1)子类会自动拥有父类定义的属性和方法
(2)父类又叫超类,基类
(3)子类又叫派生类
细节:
1) 子类继承了所有的属性和方法,非私有的属性和方法可以在子类直接访问, 但是私有属性和方法不能在子类直接访问,要通过父类提供公共的方法去访问 2) 子类必须调用父类的构造器, 完成父类的初始化 3) 当创建子类对象时,不管使用子类的哪个构造器,默认情况下总会去调用父类的无参构造器,如果父类没有提供无参构造器,则必须在子类的构造器中用 super 去指定使用父类的哪个构造器完成对父类的初始化工作,否则,编译不会通过。 4) 如果希望指定去调用父类的某个构造器,则显式的调用一下 : super(参数列表) 5) super 在使用时,必须放在构造器第一行(super 只能在构造器中使用) 6) super() 和 this() 都只能放在构造器第一行,因此这两个方法不能共存在一个构造器 7) java 所有类都是 Object 类的子类, Object 是所有类的基类. 8) 父类构造器的调用不限于直接父类!将一直往上追溯直到 Object 类(顶级父类) 9) 子类最多只能继承一个父类(指直接继承),即 java 中是单继承机制。 思考:如何让 A 类继承 B 类和 C 类? 【A 继承 B, B 继承 C】 10) 不能滥用继承,子类和父类之间必须满足 is-a 的逻辑关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class Extends01 { public static void main (String[] args) { C c = new C (); } } class A { public A () { System.out.println("我是A类" ); } } class B extends A { public B () { System.out.println("我是B类的无参构造" ); } public B (String name) { System.out.println(name + "我是B类的有参构造" ); } } class C extends B { public C () { this ("hello" ); System.out.println("我是c类的无参构造" ); } public C (String name) { super ("hahah" ); System.out.println("我是c类的有参构造" ); } } 我是A类 hahah我是B类的有参构造 我是c类的有参构造 我是c类的无参构造
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 public class Extends02 { public static void main (String[] args) { PC pc = new PC ("intel" , 16 , 500 , "IBM" ); pc.printInfo(); NotePad notePad = new NotePad ("intel" , 8 , 256 , "RED" ); notePad.printInfo(); } } public class Computer { private String cpu; private int memory; private int disk; public Computer () { } public Computer (String cpu, int memory, int disk) { this .cpu = cpu; this .memory = memory; this .disk = disk; } public String getDetails () { return "cpu=" + cpu + ", memory=" + memory + ", disk=" + disk; } public String getCpu () { return cpu; } public void setCpu (String cpu) { this .cpu = cpu; } public int getMemory () { return memory; } public void setMemory (int memory) { this .memory = memory; } public int getDisk () { return disk; } public void setDisk (int disk) { this .disk = disk; } } public class PC extends Computer { private String brand; public PC (String cpu, int memory, int disk, String brand) { super (cpu, memory, disk); this .brand = brand; } public String getBrand () { return brand; } public void setBrand (String brand) { this .brand = brand; } public void printInfo () { System.out.println("PC信息: " ); System.out.println(getDetails() + ", brand=" + brand); } } public class NotePad extends Computer { private String color; public NotePad (String cpu, int memory, int disk, String color) { super (cpu, memory, disk); this .color = color; } public void printInfo () { System.out.println("NotePad信息: " ); System.out.println(getDetails() + ", color=" + color); } public String getColor () { return color; } public void setColor (String color) { this .color = color; } }
super关键字 super 代表父类的引用,用于访问父类的属性、方法、构造器。
1.访问父类的属性,但不能访问父类的private属性
super.属性名;
2.访问父类的方法,不能访问父类的private方法
super.方法名(参数列表);
3.访问父类的构造器
super(参数列表);
super 和 this 的比较
方法重写/覆盖(override) 方法覆盖(重写)就是子类有一个方法,和父类的某个方法的名称、返回类型、参数一样,那么我们就说子类的这个方法覆盖了父类的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 public class TestOverride { public static void main (String[] args) { Person p1 = new Person ("fzy" ,16 ); Student p2 = new Student ("mzl" ,23 ,"0335" ,99.9 ); System.out.println(p1.say()); System.out.println(p2.say()); } } public class Person { private String name; private int age; public Person () { } public Person (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public String say () { return "name: " + name + " age: " + age; } } public class Student extends Person { private String ID; double score; public Student () { } public Student (String name, int age, String ID, double score) { super (name, age); this .ID = ID; this .score = score; } public String getID () { return ID; } public void setID (String ID) { this .ID = ID; } public double getScore () { return score; } public void setScore (double score) { this .score = score; } public String say () { return super .say() + " ID: " + ID + " score: " + score; } }
多态 多态(polymorphic):方法和对象具有多种形态。
1.方法的多态
重写和重载就体现了多态
2.对象的多态
(1)一个对象的编译类型和运行类型可以不一致
(2)编译类型在定义对象时,就确定了,不能改变
(3)运行类型是可以变化的.
(4)编译类型看定义时=号的左边,运行类型看=号的右边
1 2 3 Animal animal=new Dog (): animal = new Cat ();
要点:
多态的前提是:两个对象(类)存在继承关系
多态的向上转型:
1)本质:父类的引用指向了子类的对象
2)语法:父类类型 引用名 = new 子类类型();
3)特点:编译类型看左边,运行类型看右边。
可以调用父类中的所有成员(需遵守访问权限),不能调用子类中特有成员;最终运行效果看子类的具体实现!
1)语法:子类类型 引用名=(子类类型)父类引用;
2)只能强转父类的引用,不能强转父类的对象
3)要求父类的引用必须指向的是当前目标类型的对象
4)当向下转型后,可以调用子类类型中所有的成员
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class PolyDetail02 { public static void main (String[] args) { Base base = new Sub (); System.out.println(base.count); Sub sub = new Sub (); System.out.println(sub.count); } } class Base { int count = 10 ; } class Sub extends Base { int count = 20 ; }
instanceOf 比较操作符,用于判断对象的运行类型
是否为 XX 类型或 XX 类型的子类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class PolyDetail03 { public static void main (String[] args) { BB bb = new BB (); System.out.println(bb instanceof BB); System.out.println(bb instanceof AA); AA aa = new BB (); System.out.println(aa instanceof AA); System.out.println(aa instanceof BB); Object obj = new Object (); System.out.println(obj instanceof AA); String str = "hello" ; System.out.println(str instanceof Object); } } class AA {} class BB extends AA {}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package intermediate.poly;public class PolyExercise { public static void main (String[] args) { Sub s = new Sub (); System.out.println(s.count); s.display(); Base b = s; System.out.println(b == s); System.out.println(b.count); b.display(); } } class Base { int count = 10 ; public void display () { System.out.println(this .count); } } class Sub extends Base { int count = 20 ; public void display () { System.out.println(this .count); } }
动态绑定 1.当调用对象方法时,该方法会和该对象的内存地址/运行类型绑定
2.当调用对象属性时,没有动态绑定机制,哪里声明,哪里使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class DynamicBinding { public static void main (String[] args) { A a = new B (); System.out.println(a.sum()); System.out.println(a.sum1()); } } class A { public int i = 10 ; public int sum () { return getI() + 10 ; } public int sum1 () { return i + 10 ; } public int getI () { return i; } } class B extends A { public int i = 20 ; public int getI () { return i; } }
多态数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 public class PloyArray { public static void main (String[] args) { Person[] persons = new Person [5 ]; persons[0 ] = new Person ("jack" , 20 ); persons[1 ] = new Student ("mary" , 18 , 100 ); persons[2 ] = new Student ("smith" , 19 , 30.1 ); persons[3 ] = new Teacher ("scott" , 30 , 20000 ); persons[4 ] = new Teacher ("king" , 50 , 25000 ); for (int i = 0 ; i < persons.length; i++) { System.out.println(persons[i].say()); if (persons[i] instanceof Student){ ((Student)persons[i]).study(); } else if (persons[i] instanceof Teacher){ ((Teacher) persons[i]).teach(); } else { System.out.println("不是学生或者老师" ); } } } } public class Person { private String name; private int age; public Person (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public String say () { return "name: " + name + " age: " + age; } } public class Student extends Person { double score; public Student (String name, int age, double score) { super (name, age); this .score = score; } public double getScore () { return score; } public void setScore (double score) { this .score = score; } public String say () { return super .say() + " score: " + score; } public void study () { System.out.println("Student " + getName() + " is studying..." ); } } public class Teacher extends Person { double salary; public Teacher (String name, int age, double salary) { super (name, age); this .salary = salary; } public double getScore () { return salary; } public void setScore (double score) { this .salary = score; } public String say () { return super .say() + " salary: " + salary; } public void teach () { System.out.println("Teacher " + getName() + " is teaching..." ); } }
多态参数 方法定义的形参类型为父类类型,实参类型允许为子类类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 package intermediate.poly.polyparameter_;public class PloyParameter { public static void main (String[] args) { Worker tom = new Worker ("tom" , 5000 ); Manager smith = new Manager ("smith" , 10000 , 50000 ); PloyParameter ploy = new PloyParameter (); ploy.showEmpAnnual(tom); ploy.showEmpAnnual(smith); ploy.testWork(tom); ploy.testWork(smith); } public void showEmpAnnual (Employee e) { System.out.println(e.getAnnual()); } public void testWork (Employee e) { if (e instanceof Worker) { ((Worker) e).work(); } else if (e instanceof Manager) { ((Manager) e).manage(); } } } class Employee { private String name; private double salary; public Employee (String name, double salary) { this .name = name; this .salary = salary; } public String getName () { return name; } public void setName (String name) { this .name = name; } public double getSalary () { return salary; } public void setSalary (double salary) { this .salary = salary; } public double getAnnual () { return salary * 12 ; } } class Worker extends Employee { public Worker (String name, double salary) { super (name, salary); } public void work () { System.out.println("Worker" + getName() + "正在工作" ); } public double getAnnual () { return super .getAnnual(); } } class Manager extends Employee { private double bonus; public Manager (String name, double salary, double bonus) { super (name, salary); this .bonus = bonus; } public double getAnnual () { return bonus + super .getAnnual(); } public void manage () { System.out.println("Manager" + getName() + "正在管理工作" ); } }
Object类详解 equals方法 ==与equals的对比
1.==:既可以判断基本类型,又可以判断引用类型
2.==:如果判断基本类型,判断的是值是否相等。示例:int i=10;double d=10.0;
3.==:如果判断引用类型,判断的是地址是否相等,即判定是不是同一个对象
4.equals:是Object类中的方法,只能判断引用类型
5.默认判断的是地址是否相等,子类中往往重写该方法,用于判断内容是否相等。比如Integer,String
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Equals01 { public static void main (String[] args) { Integer x = new Integer (1000 ); Integer y = new Integer (1000 ); System.out.println(x==y); System.out.println(x.equals(y)); String s1 = new String ("hello" ); String s2 = new String ("hello" ); System.out.println(s1==s2); System.out.println(s1.equals(s2)); } }
hashCode 1) 提高具有哈希结构的容器的效率! 2) 两个引用,如果指向的是同一个对象,则哈希值肯定是一样的! 3) 两个引用,如果指向的是不同对象,则哈希值是不一样的 4) 哈希值主要根据地址号来的!但不能完全将哈希值等价于地址。 5) 在集合中 hashCode 如果需要的话,也会重写,。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package intermediate.Object_;public class HashCode_ { public static void main (String[] args) { HC hc = new HC (); HC hc2 = new HC (); HC hc3 = hc; System.out.println(hc.hashCode()); System.out.println(hc2.hashCode()); System.out.println(hc3.hashCode()); } } class HC {} 1324119927 990368553 1324119927
toString 1) 基本介绍 默认返回:全类名+@+哈希值的十六进制,【查看 Object 的 toString 方法】 子类往往重写 toString 方法,用于返回对象的属性信息 2) 重写 toString 方法,打印对象或拼接对象时,都会自动调用该对象的 toString 形式. 3) 当直接输出一个对象时,toString 方法会被默认的调用, 比如 System.out.println(monster); 就会默认调用 monster.toString()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class ToString_ { public static void main (String[] args) { Monster monster = new Monster ("小妖怪" , "巡山的" , 1000 ); System.out.println(monster.toString() + " hashcode=" + monster.hashCode()); System.out.println("==当直接输出一个对象时,toString 方法会被默认的调用==" ); System.out.println(monster); } } class Monster { private String name; private String job; private double sal; public Monster (String name, String job, double sal) { this .name = name; this .job = job; this .sal = sal; } @Override public String toString () { return "Monster{" + "name='" + name + '\t' + ", job='" + job + '\t' + ", sal=" + sal + '}' ; } }
finalize 1) 当对象被回收时,系统自动调用该对象的 finalize 方法。子类可以重写该方法,做一些释放资源的操作。 2) 什么时候被回收:当某个对象没有任何引用时,则 jvm 就认为这个对象是一个垃圾对象,就会使用垃圾回收机制来销毁该对象,在销毁该对象前,会先调用 finalize 方法。 3) 垃圾回收机制的调用,是由系统来决定(即有自己的 GC 算法), 也可以通过 System.gc() 主动触发垃圾回收机制。
断点调试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Debug01 { public static void main (String[] args) { int [] arr = {1 , 10 , -1 }; for (int i = 0 ; i <= arr.length; i++) { System.out.println(arr[i]); } System.out.println("--------------------------------" ); } } 1 10 -1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 at intermediate.debug_.Debug01.main(Debug01.java:7 )
零钱通项目 1.项目需求说明
使用 Java 开发 零钱通项目 , 可以完成收益入账,消费,查看明细,退出系统等功能。
2.项目的界面
3.项目代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;public class OOP_ { boolean loop = true ; Scanner scanner = new Scanner (System.in); String key = "" ; String details = "-----------------零钱通明细------------------" ; double money = 0 ; double balance = 0 ; Date date = null ; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm" ); String note = "" ; public void showMenu () { do { System.out.println("\n================零钱通菜单===============" ); System.out.println("\t\t\t1 零钱通明细" ); System.out.println("\t\t\t2 收益入账" ); System.out.println("\t\t\t3 消费" ); System.out.println("\t\t\t4 退 出" ); System.out.print("请选择(1-4): " ); key = scanner.next(); switch (key) { case "1" : this .detail(); break ; case "2" : this .income(); break ; case "3" : this .pay(); break ; case "4" : this .exit(); break ; default : System.out.println("选择有误,请重新选择" ); } } while (loop); System.out.println("-----退出了零钱通项目-----" ); } public void detail () { System.out.println(details); } public void income () { System.out.print("收益入账金额:" ); money = scanner.nextDouble(); if (money <= 0 ) { System.out.println("收益入账金额 需要 大于 0" ); return ; } balance += money; date = new Date (); details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance; } public void pay () { System.out.print("消费金额:" ); money = scanner.nextDouble(); if (money <= 0 || money > balance) { System.out.println("你的消费金额 应该在 0-" + balance); return ; } System.out.print("消费说明:" ); note = scanner.next(); balance -= money; date = new Date (); details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance; } public void exit () { String choice = "" ; while (true ) { System.out.println("你确定要退出吗? y/n" ); choice = scanner.next(); if ("y" .equals(choice) || "n" .equals(choice)) { break ; } } if (choice.equals("y" )) { loop = false ; } } }
1 2 3 4 5 6 7 public class SmallChangeSys { public static void main (String[] args) { System.out.print("\t\t\t\t欢迎使用" ); new OOP_ ().showMenu(); } }
本章小结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 public class Homework07 { public static void main (String[] args) { Person[] persons = new Person [4 ]; persons[0 ] = new Student ("张三" , '男' , 19 , "00220711" ); persons[1 ] = new Student ("李四" , '女' , 18 , "00220712" ); persons[2 ] = new Teacher ("王五" , '男' , 40 , 12 ); persons[3 ] = new Teacher ("刘六" , '女' , 35 , 10 ); Person tmp = new Person (); for (int i = 0 ; i < persons.length - 1 ; i++) { for (int j = 0 ; j < persons.length - 1 - i; j++) { if (persons[j].getAge() < persons[j + 1 ].getAge()) { tmp = persons[j]; persons[j] = persons[j + 1 ]; persons[j + 1 ] = tmp; } } } Homework07 h = new Homework07 (); for (int i = 0 ; i < persons.length - 1 ; i++) { h.test(persons[i]); System.out.println("-------------------" ); } } public void test (Person p) { if (p instanceof Student) { p.printInfo(); ((Student) p).study(); } else if (p instanceof Teacher) { p.printInfo(); ((Teacher) p).teach(); } } }
Person类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 public class Person { private String name; private char sex; private int age; public Person () { } public Person (String name, char sex, int age) { this .name = name; this .sex = sex; this .age = age; } public String play () { return this .name; } public String getName () { return name; } public void setName (String name) { this .name = name; } public char getSex () { return sex; } public void setSex (char sex) { this .sex = sex; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public void printInfo () { System.out.print("姓名:" + this .name + "\n年龄:" + this .age + "\n性别:" + this .sex + "\n" ); } }
Student类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Student extends Person { private String stu_id; public Student (String name, char sex, int age, String stu_id) { super (name, sex, age); this .stu_id = stu_id; } public String play () { return super .play() + "爱玩足球" ; } public void study () { System.out.println("我会好好学习" ); } public String getStu_id () { return stu_id; } public void setStu_id (String stu_id) { this .stu_id = stu_id; } public void printInfo () { System.out.println("学生的信息" ); super .printInfo(); System.out.println("学号:" + this .stu_id); System.out.println(this .play()); } }
Teacher类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Teacher extends Person { private int work_age; public Teacher (String name, char sex, int age, int work_age) { super (name, sex, age); this .work_age = work_age; } public String play () { return super .play() + "爱玩象棋" ; } public void teach () { System.out.println("我会认真教课" ); } public int getWork_age () { return work_age; } public void setWork_age (int work_age) { this .work_age = work_age; } public void printInfo () { System.out.println("老师的信息" ); super .printInfo(); System.out.println("工龄:" + this .work_age); System.out.println(this .play()); } }
房屋出租系统 界面 1.主菜单
2.新增房源
3.查找房源
4.删除房源
5.修改房源
6.房源列表
7.退出
设计
实现 1 2 3 4 5 6 7 8 public class HouseRentApp { public static void main (String[] args) { new HouseView ().mainMenu(); System.out.println("===你退出房屋出租系统==" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 public class House { private int id; private String name; private String phone; private String address; private int rent; private String state; public House (int id, String name, String phone, String address, int rent, String state) { this .id = id; this .name = name; this .phone = phone; this .address = address; this .rent = rent; this .state = state; } 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 String getPhone () { return phone; } public void setPhone (String phone) { this .phone = phone; } public String getAddress () { return address; } public void setAddress (String address) { this .address = address; } public int getRent () { return rent; } public void setRent (int rent) { this .rent = rent; } public String getState () { return state; } public void setState (String state) { this .state = state; } @Override public String toString () { return id + "\t" + name + "\t" + phone + "\t" + address + "\t" + rent + "\t" + state; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 public class HouseView { private boolean loop = true ; private char key = ' ' ; HouseService houseService = new HouseService (10 ); public void update () { System.out.println("=============修改房屋信息============" ); System.out.println("请选择待修改房屋编号(-1表示退出)" ); int updateId = Utility.readInt(); if (updateId == -1 ) { System.out.println("=============你放弃修改房屋信息============" ); return ; } House house = houseService.findById(updateId); if (house == null ) { System.out.println("=============修改房屋信息编号不存在..============" ); return ; } System.out.print("姓名(" + house.getName() + "): " ); String name = Utility.readString(8 , "" ); if (!"" .equals(name)) { house.setName(name); } System.out.print("电话(" + house.getPhone() + "):" ); String phone = Utility.readString(12 , "" ); if (!"" .equals(phone)) { house.setPhone(phone); } System.out.print("地址(" + house.getAddress() + "): " ); String address = Utility.readString(18 , "" ); if (!"" .equals(address)) { house.setAddress(address); } System.out.print("租金(" + house.getRent() + "):" ); int rent = Utility.readInt(-1 ); if (rent != -1 ) { house.setRent(rent); } System.out.print("状态(" + house.getState() + "):" ); String state = Utility.readString(3 , "" ); if (!"" .equals(state)) { house.setState(state); } System.out.println("=============修改房屋信息成功============" ); } public void findHouse () { System.out.println("=============查找房屋信息============" ); System.out.print("请输入要查找的id: " ); int findId = Utility.readInt(); House house = houseService.findById(findId); if (house != null ) { System.out.println(house); } else { System.out.println("=============查找房屋信息id不存在============" ); } } public void exit () { char c = Utility.readConfirmSelection(); if (c == 'Y' ) { loop = false ; } } public void delHouse () { System.out.println("=============删除房屋信息============" ); System.out.print("请输入待删除房屋的编号(-1退出):" ); int delId = Utility.readInt(); if (delId == -1 ) { System.out.println("=============放弃删除房屋信息============" ); return ; } char choice = Utility.readConfirmSelection(); if (choice == 'Y' ) { if (houseService.del(delId)) { System.out.println("=============删除房屋信息成功============" ); } else { System.out.println("=============房屋编号不存在,删除失败============" ); } } else { System.out.println("=============放弃删除房屋信息============" ); } } public void addHouse () { System.out.println("=============添加房屋============" ); System.out.print("姓名: " ); String name = Utility.readString(8 ); System.out.print("电话: " ); String phone = Utility.readString(12 ); System.out.print("地址: " ); String address = Utility.readString(16 ); System.out.print("月租: " ); int rent = Utility.readInt(); System.out.print("状态: " ); String state = Utility.readString(3 ); House newHouse = new House (0 , name, phone, address, rent, state); if (houseService.add(newHouse)) { System.out.println("=============添加房屋成功============" ); } else { System.out.println("=============添加房屋失败============" ); } } public void listHouses () { System.out.println("=============房屋列表============" ); System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)" ); House[] houses = houseService.list(); for (int i = 0 ; i < houses.length; i++) { if (houses[i] == null ) { break ; } System.out.println(houses[i]); } System.out.println("=============房屋列表显示完毕============" ); } public void mainMenu () { do { System.out.println("\n=============房屋出租系统菜单============" ); System.out.println("\t\t\t1 新 增 房 源" ); System.out.println("\t\t\t2 查 找 房 屋" ); System.out.println("\t\t\t3 删 除 房 屋 信 息" ); System.out.println("\t\t\t4 修 改 房 屋 信 息" ); System.out.println("\t\t\t5 房 屋 列 表" ); System.out.println("\t\t\t6 退 出" ); System.out.print("请输入你的选择(1-6): " ); key = Utility.readChar(); switch (key) { case '1' : addHouse(); break ; case '2' : findHouse(); break ; case '3' : delHouse(); break ; case '4' : update(); break ; case '5' : listHouses(); break ; case '6' : exit(); break ; } } while (loop); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public class HouseService { private House[] houses; private int houseNums = 1 ; private int idCounter = 1 ; public HouseService (int size) { houses = new House [size]; houses[0 ] = new House (1 , "jack" , "112" , "海淀区" , 2000 , "未出租" ); } public House findById (int findId) { for (int i = 0 ; i < houseNums; i++) { if (findId == houses[i].getId()) { return houses[i]; } } return null ; } public boolean del (int delId) { int index = -1 ; for (int i = 0 ; i < houseNums; i++) { if (delId == houses[i].getId()) { index = i; } } if (index == -1 ) { } for (int i = index; i < houseNums - 1 ; i++) { houses[i] = houses[i + 1 ]; } houses[--houseNums] = null ; return true ; } public boolean add (House newHouse) { if (houseNums == houses.length) { System.out.println("数组已满,不能再添加了..." ); return false ; } houses[houseNums++] = newHouse; newHouse.setId(++idCounter); return true ; } public House[] list() { return houses; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 import java.util.*;public class Utility { private static Scanner scanner = new Scanner (System.in); 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 ); } public static char readChar (char defaultValue) { String str = readKeyBoard(1 , true ); return (str.length() == 0 ) ? defaultValue : str.charAt(0 ); } public static int readInt () { int n; for (; ; ) { String str = readKeyBoard(10 , false ); try { n = Integer.parseInt(str); break ; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:" ); } } return n; } public static int readInt (int defaultValue) { int n; for (; ; ) { String str = readKeyBoard(10 , true ); if (str.equals("" )) { return defaultValue; } try { n = Integer.parseInt(str); break ; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:" ); } } return n; } public static String readString (int limit) { return readKeyBoard(limit, false ); } public static String readString (int limit, String defaultValue) { String str = readKeyBoard(limit, true ); return str.equals("" ) ? defaultValue : str; } public static char readConfirmSelection () { System.out.println("请输入你的选择(Y/N): 请小心选择" ); char c; for (; ; ) { String str = readKeyBoard(1 , false ).toUpperCase(); c = str.charAt(0 ); if (c == 'Y' || c == 'N' ) { break ; } else { System.out.print("选择错误,请重新输入:" ); } } return c; } private static String readKeyBoard (int limit, boolean blankReturn) { String line = "" ; while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.length() == 0 ) { if (blankReturn) { return line; } else { continue ; } } if (line.length() < 1 || line.length() > limit) { System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:" ); continue ; } break ; } return line; } }