1. 理解“万事万物皆对象”
1.在 Java 语言范畴中,我们都将功能、结构等封装到类中,通过类的实例化,来调用具体的功能结构
Scanner,String 等
文件:File
网络资源:URL
2. 涉及到 Java 语言与前端 Html、后端的数据库交互时,前后端的结构在 Java 层面交互时,都体现为类、对象。
2、内存解析的说明
引用类型的变量,只可能存储两类值:null 或 地址值(含变量的类型)
3. 匿名对象的使用
- 理解:我们创建的对象,没有显式的赋给一个变量名。即为匿名对象
- 特征:匿名对象只能调用一次。
- 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 42 43 44
| package com.atguigu.java;
public class InstanceTest { public static void main(String[] args) { Phone p = new Phone(); p.playGame();
new Phone().playGame(); new Phone().price = 1999; new Phone().showPrice();
PhoneMall mall = new PhoneMall(); mall.show(new Phone());
} }
class PhoneMall{ public void show(Phone phone){ phone.sendEmail(); phone.playGame(); } }
class Phone{ double price;
public void sendEmail(){ System.out.println("发送邮件"); } public void playGame(){ System.out.println("玩游戏"); } public void showPrice(){ System.out.println("价格为" + price); } }
|
4. 自定义工具类的封装
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
| package com.atguigu.java;
public class ArrayUtil { public int getMax(int[] arr){ int maxValue = arr[0]; for (int i = 1; i < arr.length; i++) { if (maxValue < arr[i]) { maxValue = arr[i]; } } return maxValue; } public int getMin(int[] arr){ int minValue = arr[0]; for (int i = 1; i < arr.length; i++) { if (minValue > arr[i]) { minValue = arr[i]; } } return minValue; } public int getSum(int[] arr){ int sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; } public int getAvg(int[] arr){ return getSum(arr) / arr.length; }
public void reverse(int[] arr){ for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = temp; } } public void reverse(String[] arr){ } public int[] copy(int[] arr) { int[] arr1 = new int[arr.length]; for (int i = 0; i < arr1.length; i++) { arr1[i] = arr[i]; } return arr1; } public void sort(int[] arr){ for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public void print(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } System.out.println(); } public int getIndex(int[] arr, int dest){ for (int i = 0; i < arr.length; i++) { if (dest == arr[i]) { return i; } } 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
| package com.atguigu.java;
public class ArrayUtilTest { public static void main(String[] args) { ArrayUtil util = new ArrayUtil(); int[] arr = new int[]{32,34,32,5,3,54,654,-98,0,-53,5};
System.out.println("最大值为" + util.getMax(arr));
System.out.println("排序前:"); util.print(arr); util.sort(arr); System.out.println("排序后:"); util.print(arr);
System.out.println("查找:"); int index = util.getIndex(arr, -5); if(index >= 0){ System.out.println("找到了,索引地址为:" + index); }else{ System.out.println("未找到"); }
util.reverse(arr); } }
|
5. 方法的重载
1. 定义:
在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。
“两同一不同”:同一个类、相同方法名
参数列表不同:参数个数不同,参数类型不同
2. 举例:Arrays 类中重载的 sort() / binarySearch()
3. 判断是否是重载:跟方法的权限修饰符、返回值类型、形参变量名、方法体都没有关系!
4. 在通过对象调用方法时,通过方法名和参数列表确定方法
5. 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 42 43
| package com.atguigu.java1;
public class OverLoadTest { public static void main(String[] args) { OverLoadTest test = new OverLoadTest(); test.getSum(1, 2); test.getSum(1.0, 2.0); }
public void getSum(int i, int j){ System.out.println(1); } public void getSum(double d1, double d2){ System.out.println(2); } public void getSum(String s ,int i){ System.out.println(3); } public void getSum(int i,String s){ System.out.println(4); }
}
|
6. 可变个数形参的方法
- 可变个数形参的格式:数据类型 … 变量名
- 当调用可变个数形参的方法时,传入的参数个数可以是:0 个,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
| package com.atguigu.java1;
import java.util.Arrays;
public class MethodArgsTest { public static void main(String[] args) { MethodArgsTest test = new MethodArgsTest(); test.show("hello world"); test.show(new String[]{"AA", "BB"}); }
public void show(String s){ System.out.println("String s"); } public void show(String ... strings){ System.out.println("String ... strings"); System.out.println(Arrays.toString(strings)); }
}
|
7. 关于变量的赋值
- 基本数据类型,此时赋值的是变量所保存的数据值。
- 引用数据类型,此时赋值的是变量所保存的数据的地址值。
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
| package com.atguigu.java1;
public class ValueTransferTest { public static void main(String[] args) { int m = 10; int n = m; System.out.println(m + "\t"+ n);
n = 20; System.out.println(m + "\t"+ n);
Order o1 = new Order(); o1.orderId = 1001; Order o2 = o1; System.out.println(o1.orderId + "\t" + o2.orderId);
o2.orderId = 1002; System.out.println(o1.orderId + "\t" + o2.orderId); } } class Order{ int orderId; }
|
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
| package com.atguigu.java1;
public class ValueTransferTest1 { public static void main(String[] args) { int m = 10; int n = 20; System.out.println(m + "\t" + n);
ValueTransferTest1 test = new ValueTransferTest1(); test.swap(m, n); System.out.println(m + "\t" + n); }
public void swap(int m, int n){ int temp = m; m = n; n = temp; } }
|
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
| package com.atguigu.java1;
public class ValueTransferTest2 { public static void main(String[] args) { Data data = new Data(); data.m = 10; data.n = 20; System.out.println(data.m + "\t" + data.n);
ValueTransferTest2 test = new ValueTransferTest2(); test.swap(data); System.out.println(data.m + "\t" + data.n); } public void swap(Data data){ int temp = data.m; data.m = data.n; data.n = temp; } } class Data{ int m; int n; }
|
8. 面试题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class test { public static void main(String[] args) { int a = 10; int b = 10; method(a, b); System.out.println("a=" + a); System.out.println("b=" + b); } public static void method(int a, int b){ a = a * 10; b = b * 20; System.out.println(a); System.out.println(b); System.exit(0); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public class test1 { public static void main(String[] args) { int[] arr = new int[]{1, 2, 3}; System.out.println(arr); char[] arr1 = new char[]{'a', 'b', 'c'}; System.out.println(arr1); } }
|
9. 递归
- 递归方法:一个方法体内调用它自身。
- 方法递归包含了一种隐式的循环,它会重复执行某段代码,但这种重复执行无须循环控制。
递归一定要向已知方向递归,否则这种递归就变成了无穷递归,类似于死循环。
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
| package com.atguigu.java1;
public class RecursionTest { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } System.out.println(sum); RecursionTest test = new RecursionTest(); System.out.println(test.getSum1(100)); System.out.println(test.f(10)); System.out.println(test.fibonacci(10)); } public int getSum(int n){ if (n == 1){ return 1; }else { return n + getSum(n - 1); } } public int getSum1(int n){ if (n == 1){ return 1; }else { return n * getSum(n - 1); } } public int f(int n){ if (n == 0){ return 1; }else if(n == 1){ return 4; }else { return 2 * f(n - 1) + f(n - 2); } } public int fibonacci(int n){ if (n == 0){ return 0; }else if(n == 1){ return 1; }else { return fibonacci(n - 1) + fibonacci(n - 2); } } }
|