2、比较运算符
@Test
public void test13() {
System.out.print("10 == 10 >>> " + (10 == 10) + "\t"); // == 等于,比较对象是否相等
System.out.println("10 == 20 >>> " + (10 == 20));
System.out.print("10 != 10 >>> " + (10 != 10) + "\t"); // != 不等于,比较对象是否不等
System.out.println("10 != 20 >>> " + (10 != 20));
System.out.print("10 > 20 >>> " + (10 > 20) + "\t"); // > 大于,返回x大于y的结果
System.out.println("10 < 20 >>> " + (10 < 20)); // < 小于,返回x小于y的结果
System.out.print("10 <= 10 >>> " + (10 <= 10) + "\t"); // <= 小于等于,返回x小于等于y的结果
System.out.print("10 <= 11 >>> " + (10 <= 11) + "\t");
System.out.println("10 <= 9 >>> " + (10 <= 9));
System.out.print("10 >= 10 >>> " + (10 >= 10) + "\t"); // >= 大于等于,返回x大于等于y的结果
System.out.print("10 >= 11 >>> " + (10 >= 11) + "\t");
System.out.println("10 >= 9 >>> " + (10 >= 9));
// 输出值为True或False
}
3、逻辑运算符
(1)、
逻辑与&
短路与&&
@Test
public void test01() {
int a = 10;
int b = 20;
int c = 30;
// 逻辑与 &
// (逻辑)短路与 &&
System.out.println("a < b && a < c >>> " + (a < b && a < c));
// &&符号两端表达式返回结果同时为true的时候则整体返回值为true
System.out.println("a > b && a > c >>> " + (a > b && a > c));
// &&符号两端表达式返回有一个为false则整体返回值为false 当第一个表达式返回结果为false的时候则第二个表达式不再计算 直接整体返回false
System.out.println("a < b & a < c >>> " + (a < b & a < c));
// &符号两端表达式返回结果同时为true的时候则整体返回值为true
System.out.println("a > b & a > c >>> " + (a > b & a > c));
// &符号两端表达式返回有一个为false则整体返回值为false,当第一个表达式返回结果为false的时候第二个表达式仍然需要计算
}
(2)
逻辑或|
短路或||
@Test
public void test02() {
int a = 10;
int b = 20;
int c = 30;
// 逻辑或 |
// (逻辑)短路或 ||
System.out.println("a < b || a < c >>> " + (a < b || a < c));
// ||符号两端表达式返回结果有一个为true的时候则整体返回值为true 当第一个表达式返回结果为true的时候则第二个表达式不再计算 直接整体返回true
System.out.println("a > b || a > c >>> " + (a > b || a > c));
// ||符号两端表达式返回均为为false则整体返回值为false
System.out.println("a < b | a < c >>> " + (a < b | a < c));
// |符号两端表达式返回结果有一个为true的时候则整体返回值为true 当第一个表达式返回结果为true的时候第二个表达式仍然要做计算
System.out.println("a > b | a > c >>> " + (a > b | a > c));
// |符号两端表达式返回均为为false则整体返回值为false
}
(3)
逻辑非!
@Test
public void test03() {
// 逻辑非 !输出相反的结果
System.out.println("!true => " + !true);
System.out.println("!false => " + !false);
System.out.println("!!true => " + !!true);
System.out.println("!!false => " + !!false);
}
4、位运算符
@Test
public void test04() {
// &按位与运算
System.out.println(5 & 3);
// |按位或运算
System.out.println(5 | 3);
// ^按位异或运算 二进制,相同输出0,不同输出1
// 原数据根据参考数据得到一个新数据,再次异或该参考数据会得到原数据 可以用来加密
System.out.println(5 ^ 3);
System.out.println(6 ^ 3);
// ~按位取反 二进制中最高位为符号位,0正1负
System.out.println(~ 3);
}
5、左移与右移
@Test
public void test05() {
//右移相当于除以2的n次幂
//无符号右移 >>> 也叫逻辑右移若该数为正,则最高位补0,负数同样补0 无符号右移一般正数运算时使用
System.out.println(8 >>> 2);
//右移 >> 若该数为正,则最高位补0,负数补1
System.out.println(8 >> 2);
System.out.println(-4 >> 2);
System.out.println(4 >> 3);
System.out.println(-4 >> 1);
//左移相当于乘以2的n次幂
//最低位补0
System.out.println(2 << 2);
System.out.println(-2 << 2);
}
九、判断与循环
1、判断
(1)if语句
if(判断内容){判断内容为true时需要执行的内容}
@Test
public void test01() {
boolean rain = true;
if (rain) {
System.out.println("不上课");
}
System.out.println("结束");
}
(2)if-else语句
if(判断内容){判断内容为true时需要执行的内容} else{判断结果为false时需要执行的内容}
@Test
public void test02() {
boolean rain = true;
if (rain) {
System.out.println("今天不上课");
} else {
System.out.println("上课");
}
System.out.println("结束");
}
(3)if-else-if语句
if(判断的内容){判断内容为true时需要执行的内容} else if(判断的内容){判断内容为true时需要执行的内容} else if(判断的内容){判断内容为true时需要执行的内容}
@Test
public void test07() {
Random random = new Random();
int i = random.nextInt(7);
System.out.println(i);
if (i == 1) {
System.out.println("今天是星期一");
} else if (i == 2) {
System.out.println("今天是星期二");
} else if (i == 3) {
System.out.println("今天是星期三");
} else if (i == 4) {
System.out.println("今天是星期四");
} else if (i == 5) {
System.out.println("今天是星期五");
} else if (i == 6) {
System.out.println("今天是星期六");
} else if (i == 0) {
System.out.println("今天是星期日");
}
}
(4)if语句的简写
判断 ? "判断结果为true时需要执行的内容":"判断结果为false时需要执行的内容"
@Test
public void test04() {
boolean rain = true;
String weather = rain ? "不上课" : "上课";
System.out.println("今天" + weather);
System.out.println("结束");
}
(5)switch-case语句
@Test
public void test08() {
Random random = new Random();
int i = random.nextInt(7);
System.out.println(i);
switch (i) {
case 1:
System.out.println("今天是星期一");
break;
case 2:
System.out.println("今天是星期二");
break;
case 3:
System.out.println("今天是星期三");
break;
case 4:
System.out.println("今天是星期四");
break;
case 5:
System.out.println("今天是星期五");
break;
case 6:
System.out.println("今天是星期六");
break;
default:
System.out.println("今天是星期日");
break;
}
}
2、循环
(1)while循环
@Test
public void test01() {
int i = 0;
while (i < 10) {
if (i % 2 == 0) {
System.out.println(i);
}
}
i++;
}
continue为跳过但继续循环
@Test
public void test04() {
int i = 0;
while (i < 10) {
if (i % 2 != 0) {
i++;
continue;
}
System.out.println(i++);
}
}
break为打断循环
@Test
public void test05() {
int i = 1;
while (i <= 5) {
System.out.println("输出第" + i++ + "次");
if (i == 3) {
System.out.println("技能被打断");
break;
}
}
}
(2)for循环
@Test
public void test07() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println("第" + (i + 1) + "次");
}
}
(3)双层for循环
@Test
public void test12() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print((j + 1) + "*" + (i + 1) + "=" + (j + 1) * (i + 1) + "\t");
}
System.out.print("\n");
}
}
break在多层循环中可以用来打断指定循环
@Test
public void test10() {
a1: for (int i = 0; i < 5; i++) {
System.out.println("第" + (i + 1) + "次输出");
a2: for (int j = 0; j < 5; j++) {
if (i == 2 && j == 2) {
System.out.println("被眩晕");
break a1;
}
System.out.println("第" + (i + 1) + "次输出的第" + (j + 1) + "刀");
}
}
}
十、数组
1、数组的声明、赋值与读取
相同类型的元素组成的集合称为数组
1>三种声明方法
(1)
//声明一个整数数组为a,并赋值给它三个值 int[] arr = { 10, 20, 30 }; //读取数组下标为0位置上元素的值 System.out.println(arr[0]);
(2)
//声明一个整数数组啊,并给他一个长度为6(这样声明时候必须带上长度) int[] a = new int[6]; //给数组下标为2位置上的元素赋值 a[2] = 20; //读取数组下标为2位置上元素的值 System.out.println(a[2]);
(3)
//{}中的为数组中元素的值 不是数组的长度 int[] arr = new int[] { 1, 2, 3 }; //读取数组下标为2位置上元素的值 System.out.println(arr[2]);
数组读取时,注意下标从0开始,而不是从1开始
读取的位置没有赋值时会读取到该数组数据类型的默认值
.length属性获取数组长度
(4)可变数组的声明
int... 声明一个数据类型为int的可变数组
public static void printArrays(int... array) {
if (array == null) {
System.out.println("数组为null");
return;
}
System.out.print("[");
for (int i = 0; i < array.length; i++) {
if (i < array.length-1) {
System.out.print(array[i] + ",");
} else {
System.out.print(array[i] + "]");
}
}
}
2>读取
(1)利用for循环遍历数组输出
@Test
public void test05() {
String[] a0 = new String[4];
a0[0] = "北风卷地白草折";
a0[1] = "胡天八月即飞雪";
a0[2] = "忽如一夜春风来";
a0[3] = "千树万树梨花开";
for (int i = 0; i < a0.length; i++) {
System.out.println(a0[i]);
}
}
(2)利用toString将数组转化成字符串输出
@Test
public void test06() {
String[] a0 = new String[4];
a0[0] = "北风卷地白草折";
a0[1] = "胡天八月即飞雪";
a0[2] = "忽如一夜春风来";
a0[3] = "千树万树梨花开";
System.out.println(Arrays.toString(a0));
}
(3)利用for循环输出指定的元素
@Test
public void test07() {
int[] arr0 = new int[] { 1, 6, 7, 65, 45, 78, 34 };
System.out.print("偶数:");
for (int i = 0; i < arr0.length;) {
if (arr0[i] % 2 == 0) {
System.out.print(arr0[i] + "\0");
}
i++;
}
System.out.print("\n奇数:");
for (int j = 0; j < arr0.length;) {
if (arr0[j] % 2 == 1) {
System.out.print(arr0[j] + "\0");
}
j++;
}
}
(4)增强for循环
@Test
public void test14() {
String[] array = {"我", "爱", "你"};
for (String string : array) {
System.out.println(string);
}
}
2、数组的合并
(1)数组的简单合并
@Test
public void test02() {
int[] arr0 = { 1, 3, 5, 7, 9 }; // 数组一
int[] arr1 = { 0, 2, 4, 6, 8 }; // 数组二
int z = arr0.length + arr1.length; // 新数组长度为两数组长度之和
int[] arr2 = new int[z]; // 定义新数组
for (int i = 0; i < arr0.length; i++) { // 将数组一的元素按下标赋值给新数组的同样下标
arr2[i] = arr0[i];
}
for (int j = arr0.length; j < z; j++) { // 新数组的前几个下标(数组一长度)已占用,所以从后便开始
arr2[j] = arr1[j - arr0.length]; // 新数组的后几个下标(j)等于数组二的(j-长度)下标,赋值
}
/**
* 第二遍for循环可以
* for(int j = 0;j < arr1.length;j++){
* arr2[j + arr0.length] =arr1[j];
* }
*/
System.out.println(Arrays.toString(arr2));
}
(2)隔位组合数组
1>数组长度一致
@Test
public void test03() {
int[] arr0 = { 0, 2, 4, 6, 8 };// 数组一
int[] arr1 = { 1, 3, 5, 7, 9 };// 数组二
int z = arr0.length + arr1.length;// 新数组长度为两数组长度之和
int[] arr2 = new int[z];// 定义新数组
for (int i = 0; i < arr0.length; i++) {
arr2[2 * i] = arr0[i];
arr2[2 * i + 1] = arr1[i];
}
System.out.println(Arrays.toString(arr2));
}
隔位组合数组——思路
按照相同下标将旧数组插入新数组,(旧数组长度相同)
数组一
原下标 | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
插入后 | 0 | 2 | 4 | 6 | 8 |
新数组下标即为2 * i
数组二
原下标 | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
插入后 | 1 | 3 | 5 | 7 | 9 |
新数组下标即为2 * i + 1
2>数组长度不一致
@Test
public void test04() {
int[] arr0 = { 0, 2 };
int[] arr1 = { 1, 3, 5, 7, 9 };
// 按照下标相同的位置依次将以上两个数组中的元素插入新的数组
// 思路:
// 1、声明一个新的数组长度为两个数组长度的和
// 2、将第一个数组循环赋值给新数组 注意:下标规律为2n
// 3、讲第二个数组循环赋值给新数组 注意:下标规律为2n+1 第二个数组长度如果与第一个数组长度不一致的时候怎么办
// 4、当第一个数组复制完成之后第二个数组依次赋值即可
int[] arr2 = new int[arr0.length + arr1.length];
// 循环次数问两个中长度最大的那个长度
int max = arr0.length > arr1.length ? arr0.length : arr1.length;
// 循环次数问两个中长度最小的那个长度
int min = arr0.length < arr1.length ? arr0.length : arr1.length;
// 返回长度最长的你那个数组
int[] maxArray = arr0.length > arr1.length ? arr0 : arr1;
for (int i = 0; i < max; i++) {
// 获取第一个数组的元素
// int a = arr0[i];
// 获取第一个数组相同下标位置上的元素
// int b = arr1[i];
// 依次赋值
if (i < min) {
arr2[2 * i] = arr0[i];
arr2[2 * i + 1] = arr1[i];
} else {
arr2[i + min] = maxArray[i];
}
}
System.out.println(Arrays.toString(arr2));
}
3、数组排序
(1)数组排序时需要用到元素值的交互
三种交互方法
1>引入中间变量
int a = 10 , b = 20; // 虽然多使用了变量但是容易理解 int c = a; a = b; b = c;
2>利用数学运算符
int a = 10 , b = 20; //借助数学公式 但不推荐 有数据失真的风险 a = a + b; 30 b = a - b; 10 a = a - b; 20
3>利用位运算符
int a = 10 , b = 20; //利用按位异或运算 虽然效率高但不好理解 a = a ^ b; b = a ^ b; a = a ^ b;
(2)数组排序可能需要用到寻找数组最大最小值
获取最大值
@Test
public void test01() {
// 需求: 获取数组中的最大值
int[] arr = { 23, 32, 12, 21, 19, 91 };
// 思路: 找一个和中间变量 将数组中的最后一个位置上的元素存储在该中间比哪里中 然后依次比较数组中所有元素的值
// 获取数组中最后一个元素的值
int max = arr[arr.length - 1];
for (int i = 0; i < arr.length - 1; i++) {
// 获取元素的值
int j = arr[i];
// 和中间变量比较 将最大值赋值给中间变量
max = max > j ? max : j;
}
System.out.println("数组中最大值 >>> " + max);
}
获取最小值
@Test
public void test02() {
// 需求: 获取数组中的最小值
int[] arr = { 23, 32, 12, 21, 19, 91 };
// 思路: 找一个和中间变量 将数组中的最后一个位置上的元素存储在该中间比哪里中 然后依次比较数组中所有元素的值
// 获取数组中最后一个元素的值
int min = arr[arr.length - 1];
for (int i = 0; i < arr.length - 1; i++) {
// 获取元素的值
int j = arr[i];
// 和中间变量比较 将最小值赋值给中间变量
min = min < j ? min : j;
}
System.out.println("数组中最值 >>> " + min);
}
(3)数组的排序方法
冒泡排序、插入排序、选择排序、希尔排序、快速排序、Arrays.sort排序、归并排序、堆排序、计数排序、桶排序、基数排序等
1>Arrays.sort排序
@Test
public void test01() {
int[] arr0 = { 23, 32, 12, 21, 19, 91 };
// 调用java自带的指令排序
System.out.println("排序前:" + Arrays.toString(arr0));
Arrays.sort(arr0);
System.out.println("排序后:" + Arrays.toString(arr0));
}
2>插入排序
@Test
public void test02() {
int[] arr0 = { 23, 32, 12, 21, 19, 91 };
12 21 23 32 19 91
// 插入排序
System.out.println("排序前:" + Arrays.toString(arr0));
for (int i = 1; i < arr0.length; i++) {
int a = arr0[i];
int j;
for (j = i; j > 0 && arr0[j - 1] > a; j--) {
arr0[j] = arr0[j - 1];
}
arr0[j] = a;
}
System.out.println("排序后:" + Arrays.toString(arr0));
}
3>冒泡排序
@Test
public void test03() {
int[] arr0 = { 23, 32, 12, 21, 19, 91 };
// 冒泡排序
System.out.println("排序前:" + Arrays.toString(arr0));
for (int i = 0; i < arr0.length - 1 ; i++) {
for (int j = 1; j < arr0.length - i; j++) {
if (arr0[j - 1] > arr0[j]) {
int a = arr0[j];
arr0[j] = arr0[j - 1];
arr0[j - 1] = a;
}
}
}
System.out.println("排序后:" + Arrays.toString(arr0));
}
4、数组的一些工具类
(1)Copyof拷贝数组
copyOf(原数组,新数组长度) = 数组的拷贝
生成新数组,按下标对应,多的丢掉 少的默认值
@Test
public void test01() {
// copyOf(原数组,新数组长度) = 数组的拷贝
String[] arr0 = {"我", "爱", "你"};
System.out.println(Arrays.toString(arr0));
String[] arr1 = Arrays.copyOf(arr0, 5);
System.out.println(Arrays.toString(arr1));
String[] arr2 = Arrays.copyOf(arr0, 2);
System.out.println(Arrays.toString(arr2));
}
(2)数组的插入覆盖
将数组0的1下标开始截取,从2下标替换数组1,截取三个长度
不生成新数组
注意截取数组0长度时不能溢出,存的时候也不能溢出
@Test
public void test02() {
int[] arr0 = {0, 1, 2, 3, 4};
int[] arr1 = {5, 6, 7, 8, 9};
System.arraycopy(arr0, 1, arr1, 2, 3);
System.out.println(Arrays.toString(arr1));
}
十一、多维数组
1、二维数组的声明
(1)
//声明一个字符串数组,数组名字为name,生产i个数组,每个数组长度为j
String[][] name = new String[i][j];
(2)
String[][] classes = {
{ "java01", "java02", "java03" },
{ "python01", "python02", "python03" },
{ "go01", "go02", "go03" }
};
2、二维数组的循环赋值
(1)利用一个随机数来给所有的元素赋值
@Test
public void test05(){
Random random = new Random();
int[][] classes = new int[5][10];
for(int i = 0; i < classes.length ; i++) {
for (int j = 0; j < classes[i].length; j++) {
classes[i][j] = random.nextInt(10);
System.out.print("\t" + classes[i][j]);
}
System.out.print("\n" );
}
}
(2)拼接一个
@Test
public void test06(){
String[] coursePreFix = {"java", "python","go"};
String[][] classes = new String[3][20];
for(int i = 0; i < classes.length ; i++) {
for (int j = 0; j < classes[i].length; j++) {
if(j < 9) {
classes[i][j] = coursePreFix[i] + "0" +(j + 1) ;
}else {
classes[i][j] = coursePreFix[i] + (j + 1) ;
}
System.out.print("\0" + classes[i][j]);
}
System.out.print("\n" );
}
}
3、二维数组的读取
双层for循环遍历
@Test
public void test03() {
String[][] classes = { { "java01", "java02", "java03" },
{ "python01", "python02", "python03" },
{ "go01", "go02", "go03" } };
for (int i = 0; i < classes.length; i++) {
for (int j = 0; j < classes[i].length; j++) {
System.out.print(classes[i][j] + "\0");
}
System.out.println("\n");
}
}
.toString读取
@Test
public void test07(){
String[] coursePreFix = {"java", "python","go"};
String[][] classes = new String[3][20];
for(int i = 0; i < classes.length ; i++) {
for (int j = 0; j < classes[i].length; j++) {
if(j < 9) {
classes[i][j] = coursePreFix[i] + "0" +(j + 1) ;
}else {
classes[i][j] = coursePreFix[i] + (j + 1) ;
}
}
}
for(int i = 0; i < classes.length ; i++) {
System.out.println(Arrays.toString(classes[i]));
}
}
十二、利用数组进行一些随机操作
1、随机生成幸运中奖者名单
声明一个数组,吧人名名单放入进去,
使用随机数生成幸运者的下标
读取该下标对应的元素对应的人员姓名
@Test
public void test08(){
String[] names = new String[15];
for (int i = 0; i < names.length; i++) {
if(i < 9) {
names[i] = "user" + "0" +(i + 1) ;
}else {
names[i] = "user" + (i + 1) ;
}
}
Random random = new Random();
int luckname = random.nextInt(names.length);
System.out.println("luckname=" + luckname);
System.out.println("中将人姓名:" + names[luckname]);
2、随机一个姓名
@Test
public void test10() {
// 姓氏
String[] surnames = new String[] {"东方","左丘", "欧阳", "皇甫", "上官", "闾丘", "令狐",
"夏侯", "诸葛", "尉迟", "皇甫", "宇文", "鲜于", "西门", "司马", "独孤", "公孙", "慕容", "轩辕",
};
// 名字
String[] names = new String[] {"何", "高", "梁", "郑", "罗", "宋", "谢", "唐", "韩", "曹", "许", "邓", "萧", "冯",
"曾", "程", "蔡", "彭", "潘", "袁", "於", "董", "余", "苏", "叶", "吕", "魏", "蒋",
"田", "杜", "丁", "沈", "姜", "范", "江", "傅", "钟", "卢", "汪", "戴", "崔", "任",
"陆", "廖", "姚", "方", "金", "邱", "夏", "谭", "韦", "贾", "邹", "石", "熊", "孟",
"秦", "阎", "薛", "侯", "雷", "白", "龙", "段", "郝", "孔", "邵", "史", "毛", "常",
"万", "顾", "赖", "武", "康", "贺", "严", "尹", "钱", "施", "牛", "洪", "龚", "李",
"刘", "陈", "杨", "黄", "赵", "周", "吴", "徐", "孙", "朱", "马", "胡", "郭", "林"
};
Random random = new Random();
// 随机姓氏
String surname = surnames[random.nextInt(surnames.length)];
// 随机生成名字,1到2个字
int count = random.nextInt(1,3);
String name = "";
for (int i = 0; i < count; i++) {
name += names[random.nextInt(names.length)];
}
// 拼接姓名
String generateName = surname + name;
System.out.println("我的姓名 >>> " + generateName);
}
3、调用随机人名生成幸运者
注意需要引入依赖
注意没有放在一起的话需要进行导包
@Test
public void test09(){
ChineseNameGenerator instance = ChineseNameGenerator.getInstance();
String[] names = new String[15];
for (int i = 0; i < names.length; i++) {
names[i] = instance.generate();
}
Random random = new Random();
int luckname = random.nextInt(names.length);
System.out.println(Arrays.toString(names));
System.out.println("luckname=" + luckname);
System.out.println("中将人姓名:" + names[luckname]);
}
十三、类和方法
1、方法的概念
方法,就是一段可以被重复利用的代码的封装 方法定义五要素:修饰词,返回值类型,方法名,参数列表,方法体。
public static void fun() {
// public static修饰词 void返回值类型 fun方法名 ()内为参数 {}方法体
};
2、main方法
优先运行main方法 是程序的入口
没有main方法就没有程序入口,就没有Java appliction
public class Demo01 {
public static void main(String[] args) {
// 优先运行main方法 是程序的入口
// 无参方法的调用
fun01();
// 有参数方法的调用,方法需要一个参数的值,需要按照该方法参数的数据类型给其一个真实有效的值
// 有一种特殊的实参,为引用类型
// 实参的数据类型必须与形参的数据类型一直或者是形参的子类型
fun02("橘子");
// 按照参数位置进行传值
fun03(1,"西瓜");
//代码将两个整数相加 将相加结果返回 关键字return,将方法执行结果返回 方法的返回值类型与return后的数据类型有关
long num = fun04(10, 15);
System.out.println(num);
}
public static void fun() {
// public static修饰词 void返回值类型 fun方法名 ()内为参数 {}方法体
};
/**
* 没有参数没有返回值
*/
public static void fun01() {
System.out.println("北风卷地白草折");
};
/**
* 形式参数,定义方法的时候该参数只有一个形式,没有实际值,简称形参
*
* @param food
*/
public static void fun02(String food) {
System.out.println("吃" + food);
}
/**
* 吃了若干个xx食物
*
* @param count 吃的食品数量
* @param food 吃的食品名称
*/
public static void fun03(int count, String food) {
System.out.println("吃了" + count + "个" + food);
};
public static long fun04(int a, int b) {
/**
* 代码将两个整数相加 将相加结果返回 关键字return,将方法执行结果返回 方法的返回值类型与return后的数据类型有关
*/
return a + b;
};
}
3、方法重载
在同一个类中方法参数列表不同的同名方法这种表现形式我们成为方法重载
参数列表:参数的数量 参数的数据类型
数据类型不同指的是同一参数位置的参数数据类型不同
public class Demo02 {
public static void eat() {
System.out.println("吃");
}
//与上一个方法名相同,但是参数数量不同
public static void eat(String food) {
System.out.println("吃" + food);
}
//与上一个方法名相同,但是参数数量不同
public static void eat(int count,String food) {
System.out.println("吃" +count + "个" + food);
}
//与上一个方法名相同,参数数量也相同,但是参数数据类型不同
public static void eat(String food,int count) {
System.out.println("吃" +count + "个" + food);
}
}
4、变量的作用域
-
一个方法中无法读取另一个方法中声明的变量
-
声明在方法中的变量称为局部变量,仅在局部有效
-
声明在类中的变量称之为成员变量
-
当读取的变量在局部代码中没有被声明,则去找成员变量
-
当读取的变量在局部代码中已经被声明之后,则根据就近原则读取局部变量,不需要去找成员变量
-
Static修饰的成员变量(静态成员变量)是可以被该类的所有方法共享的
-
static修饰变量时只能修饰成员变量不能修饰局部变量
-
static修饰的方法称之为静态方法,静态方法只能访问静态成员变量
-
static修饰的方法称之为静态方法,反之称之为非静态方法或普通方法,也就是平时说的方法一般指的是非静态方法
public class Demo03 {
static int a = 30;
int b = 40;
public static void main(String[] args) {
//fun01();
//fun02();
//fun03();
//fun04();
fun05();
fun06();
}
public static void fun01() {
int x = 10;
int y = 20;
System.out.println("方法fun01 变量 x >>>" + x);
System.out.println("方法fun01 变量 y >>>" + y);
}
public static void fun02() {
int x= 20;
System.out.println("方法fun01 变量 x >>>" + x);
//一个方法中无法读取另一个方法中声明的变量
//System.out.println("读取方法fun01 变量 y >>>" + y);
}
public static void fun03() {
//当读取的变量在局部代码中没有被声明,则去找成员变量
System.out.println(a);
}
public static void fun04() {
//当读取的变量在局部代码中已经被声明之后,则根据就近原则读取局部变量,不需要去找成员变量
int a =20;
System.out.println(a);
}
public static void fun05() {
a += 10;
System.out.println("fun05执行后a=" + a);
}
public static void fun06() {
a += 20;
System.out.println("fun06执行后a=" + a);
}
public static void fun07() {
System.out.println("静态成员变量a=" + a);
//静态方法之只能访问静态成员变量
//System.out.println("非静态成员变量b=" + b);
}
public void fun08() {
//非静态方法既能访问静态成员变量也能访问非静态成员变量
System.out.println("静态成员变量a=" + a);
System.out.println("非静态成员变量b=" + b);
}
}
5、方法的封装与调用
首先需要编写好一个方法,如之前的随机姓名生成,并给他一个返回值
public class ChineseNameGenerator {
public static String generate(){
return generateName;
}
}
然后在新的类中打入随机姓名生成方法的类名.方法,进行导包便可使用
public class ChineseNameGeneratorTest {
@Test
public void generate() {
for (int i = 0; i < 10; i++) {
String name = ChineseNameGenerator.generate();
System.out.println(name);
}
}
}
非静态方法可以调用静态方法和非静态方法
静态方法只能调用静态方法
public static void main(String[] args) {
//静态main方法也只能调用静态方法
fun01();
//fun03();
}
public static void fun01() {
System.out.println("我是静态fun01");
}
public static void fun02() {
System.out.println("我是静态fun02");
//静态方法只能调用静态方法
fun01();
//fun03();
}
public void fun03() {
System.out.println("我是非静态fun03");
}
public void fun04() {
System.out.println("我是非静态fun04");
// 非静态方法可以调用静态方法和非静态方法
fun01();
fun03();
}