Compare commits
143
Commits
213f041cd3
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7fba6fe89 | ||
|
|
459cac7aae | ||
|
|
3d9c4f40a8 | ||
|
|
9df0a91256 | ||
|
|
2b42607714 | ||
|
|
79ba862cfe | ||
|
|
e028cdf89b | ||
|
|
850d9f9bdf | ||
|
|
77d6bb9105 | ||
|
|
4d356097cf | ||
|
|
ac34e38ae2 | ||
|
|
43cfd79d11 | ||
|
|
3ad7b7c2a2 | ||
|
|
e100986e73 | ||
|
|
f78cc2d201 | ||
|
|
6a48cf31dd | ||
|
|
d69297502e | ||
|
|
6a959c9db2 | ||
|
|
ed3c312e1f | ||
|
|
79cf925b27 | ||
|
|
97843e6d31 | ||
|
|
af2cbc9994 | ||
|
|
2769b710a6 | ||
|
|
196c293950 | ||
|
|
bc989fad33 | ||
|
|
41baaac307 | ||
|
|
a344d77a44 | ||
|
|
494b79dbf5 | ||
|
|
4fc4604068 | ||
|
|
be6aed66ba | ||
|
|
d27881af22 | ||
|
|
b5ff8ffb1f | ||
|
|
d2eda4d4be | ||
|
|
a82f56b8e9 | ||
|
|
47d69fc71b | ||
|
|
1b3e9884f1 | ||
|
|
151098e670 | ||
|
|
c8f77c260c | ||
|
|
e467a9cdde | ||
|
|
a9f1b9684b | ||
|
|
aa01eb5ac7 | ||
|
|
b82dafad8a | ||
|
|
10d159a451 | ||
|
|
28f73f84be | ||
|
|
9e201789e7 | ||
|
|
d824c4d9a2 | ||
|
|
705df33287 | ||
|
|
892f3b3579 | ||
|
|
be0de6bab4 | ||
|
|
36619983b9 | ||
|
|
c0cfc979bb | ||
|
|
854c97c66b | ||
|
|
9f2e3b1b7d | ||
|
|
81c823ec36 | ||
|
|
2cea8f7726 | ||
|
|
678ed5c245 | ||
|
|
01d103aaee | ||
|
|
db28082582 | ||
|
|
aa423c512e | ||
|
|
b02a197786 | ||
|
|
527fb9af6b | ||
|
|
a66409c9e0 | ||
|
|
fbea00ed59 | ||
|
|
fe0884a99e | ||
|
|
25616dcc32 | ||
|
|
75d957d433 | ||
|
|
025c5652e3 | ||
|
|
d3838a1c9e | ||
|
|
bc610b6d32 | ||
|
|
416c5c5d57 | ||
|
|
bf1179fe97 | ||
|
|
688627e75a | ||
|
|
e591833f70 | ||
|
|
89d3a777c4 | ||
|
|
c073c1394d | ||
|
|
645b5b5ab2 | ||
|
|
28fad57350 | ||
|
|
e75cd9ae65 | ||
|
|
a0edab37e5 | ||
|
|
ad62a89970 | ||
|
|
43cf613fd1 | ||
|
|
4416664f45 | ||
|
|
67fadf4a26 | ||
|
|
2c2fc1dd8c | ||
|
|
dfced7ea6e | ||
|
|
cfccb1d055 | ||
|
|
4086ee1dd5 | ||
|
|
7a9bf4dce7 | ||
|
|
426bcd32e7 | ||
|
|
4a854dea2d | ||
|
|
8a4e042a33 | ||
|
|
8f2bedfa28 | ||
|
|
006ef97d55 | ||
|
|
400c469e09 | ||
|
|
f6cdc32d8c | ||
|
|
d6274e5f4f | ||
|
|
5ed43a5133 | ||
|
|
e2b378cc29 | ||
|
|
b121cf4b73 | ||
|
|
b06c50d227 | ||
|
|
fd795f7375 | ||
|
|
f0dd070448 | ||
|
|
7c93cb3e2e | ||
|
|
5d82b02076 | ||
|
|
17d3630c89 | ||
|
|
4973afbd21 | ||
|
|
fe7909e12b | ||
|
|
73e75c5df8 | ||
|
|
ff392bed80 | ||
|
|
71a001b117 | ||
|
|
851cb0c2aa | ||
|
|
efc98db11b | ||
|
|
32d5c327bd | ||
|
|
45eef578c7 | ||
|
|
cbd4e7c7ac | ||
|
|
9aa8a959c7 | ||
|
|
38edc9abe4 | ||
|
|
6b274836bd | ||
|
|
1f07afe423 | ||
|
|
35751fd66b | ||
|
|
b7a15acb83 | ||
|
|
aae9ed021b | ||
|
|
7add132b4b | ||
|
|
39e4c634c6 | ||
|
|
1bfb1d88f7 | ||
|
|
6476d75539 | ||
|
|
24a3a24382 | ||
|
|
4d05f46ccd | ||
|
|
b9409d9816 | ||
|
|
99e2636276 | ||
|
|
f7933d36f9 | ||
|
|
298a8addc0 | ||
|
|
cb9cb703a9 | ||
|
|
d03a5cf953 | ||
|
|
2a73b9ca0f | ||
|
|
25277905ff | ||
|
|
91fbabc2f9 | ||
|
|
245e6900d8 | ||
|
|
d8c95e9628 | ||
|
|
0a01ee2480 | ||
|
|
5e86d66221 | ||
|
|
2a31bbd2c5 | ||
|
|
2fa1c45be1 |
@@ -0,0 +1,39 @@
|
|||||||
|
package com.inmind.method01;
|
||||||
|
/*
|
||||||
|
方法的三种调用方式:
|
||||||
|
1.直接调用 : 方法名(参数列表)
|
||||||
|
|
||||||
|
2.赋值调用:数据类型 变量名 = 方法名(参数列表)
|
||||||
|
|
||||||
|
3.打印输出调用: System.out.println(方法名(参数列表));
|
||||||
|
|
||||||
|
总结:直接调用可以调用任意方法,赋值和打印输出调用只能调用有返回值的方法。
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//直接调用
|
||||||
|
isEqual(1, 2);
|
||||||
|
printN(3);
|
||||||
|
|
||||||
|
//赋值调用
|
||||||
|
boolean result = isEqual(1, 2);
|
||||||
|
//void v = printN(3);不能调用无返回值的方法
|
||||||
|
|
||||||
|
//打印输出调用
|
||||||
|
System.out.println(isEqual(1, 2));
|
||||||
|
//System.out.println(printN(3));不能打印无返回值的方法
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//有返回值的方法
|
||||||
|
public static boolean isEqual(int a,int b) {
|
||||||
|
return a==b;
|
||||||
|
}
|
||||||
|
|
||||||
|
//没有有返回值的方法
|
||||||
|
public static void printN(int n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
System.out.println("helloworld");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.inmind.method01;
|
||||||
|
/**
|
||||||
|
* 方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。
|
||||||
|
* 方法重载:两同一不同
|
||||||
|
* 1.同一类中
|
||||||
|
* 2.方法名相同
|
||||||
|
* 3.参数列表不同(参数列表的个数,数据类型,参数的顺序)
|
||||||
|
*
|
||||||
|
* 总结:方法重载的作用:只要记住一个方法名,就可以调用不同的功能
|
||||||
|
*/
|
||||||
|
public class OverLoadDemo5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义2个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2);
|
||||||
|
//定义3个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2, 3);
|
||||||
|
//定义4个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2, 3, 4);
|
||||||
|
|
||||||
|
System.out.println(1);
|
||||||
|
System.out.println(1.1);
|
||||||
|
System.out.println(true);
|
||||||
|
System.out.println("true");
|
||||||
|
System.out.println(1.5F);
|
||||||
|
System.out.println(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSum(int a,int b) {
|
||||||
|
int sum = a+b;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int getSum(int a,int b,int c) {
|
||||||
|
int sum = a+b+c;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSum(int a,int b,int c,int d) {
|
||||||
|
int sum = a+b+c+d;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.inmind.method01;
|
||||||
|
/*
|
||||||
|
* 方法重载:两同一不同
|
||||||
|
* 1.同一类中
|
||||||
|
* 2.方法名相同
|
||||||
|
* 3.参数列表不同(参数列表的个数,数据类型,参数的顺序)
|
||||||
|
|
||||||
|
class Demo
|
||||||
|
{
|
||||||
|
void show(int a,float b,char c)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
下列哪些方法和指定的方法重载了。
|
||||||
|
a.int show(int x,float y,char z)
|
||||||
|
|
||||||
|
b.void show(float b,int a,char c)
|
||||||
|
|
||||||
|
c.void show(int c,float a,char b)
|
||||||
|
|
||||||
|
d.void show(int a,int b,int c)
|
||||||
|
|
||||||
|
e.double show()
|
||||||
|
*/
|
||||||
|
public class Test06 {
|
||||||
|
|
||||||
|
void show(int a,float b,char c)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//a
|
||||||
|
/*int show(int x,float y,char z){
|
||||||
|
return 1;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//b
|
||||||
|
void show(float b,int a,char c){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//c
|
||||||
|
// void show(int c,float a,char b){}
|
||||||
|
|
||||||
|
void show(int a,int b,int c){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
double show(){
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
/*
|
||||||
|
数组概念: 数组就是存储数据长度固定的容器,保证多个数据的数据类型要一致。
|
||||||
|
|
||||||
|
数组的3种定义格式
|
||||||
|
一.数组的动态初始化
|
||||||
|
数组存储的数据类型[] 数组名字 = new 数组存储的数据类型[长度];
|
||||||
|
数据类型[] 数组名 = new 数据类型[数组长度];
|
||||||
|
|
||||||
|
数组存储的数据类型:当前就是4类8种的基本数据类型,但是可以使用java中任意类型(引用类型)
|
||||||
|
[]:数组
|
||||||
|
数组名:就是标识符的一种,用来操作数组
|
||||||
|
new:java中的关键字,在堆内存中开辟空间
|
||||||
|
数组存储的数据类型:与前面的数据类型保持一致
|
||||||
|
[长度]:决定 数组的长度
|
||||||
|
|
||||||
|
二.数组的静态初始化
|
||||||
|
数据类型[] 数组名 = new 数据类型[]{值1,值2,值3...};
|
||||||
|
这种格式,没有直接给出数组的长度,但是根据传入的数据的数量,来确定
|
||||||
|
|
||||||
|
三.数组的静态初始化简写形式
|
||||||
|
数据类型[] 数组名 = {值1,值2,值3...};
|
||||||
|
这种格式,没有直接给出数组的长度,但是根据传入的数据的数量,来确定
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//一.数组的动态初始化
|
||||||
|
//数组存储的数据类型[] 数组名字 = new 数组存储的数据类型[长度];
|
||||||
|
//请定义一个长度为3的存放整数int的数组
|
||||||
|
int[] arr1 = new int[3];
|
||||||
|
|
||||||
|
//二.数组的静态初始化
|
||||||
|
int[] arr2 = new int[]{1,2,3,4,5};
|
||||||
|
|
||||||
|
//三.数组的静态初始化简写形式
|
||||||
|
int[] arr3 = {1,2,3,4,5};
|
||||||
|
|
||||||
|
String[] arr4 = {"1","2","3"};
|
||||||
|
|
||||||
|
//获取arr1的长度
|
||||||
|
System.out.println(arr1.length);//3
|
||||||
|
//获取arr1第一个空间的值(取值)
|
||||||
|
/*int value = arr1[0];
|
||||||
|
System.out.println(value);*/
|
||||||
|
System.out.println(arr1[0]);
|
||||||
|
//将100设置到arr1的第一个空间(赋值)
|
||||||
|
arr1[0] = 100;
|
||||||
|
//获取arr1第一个空间的值(取值)
|
||||||
|
System.out.println(arr1[0]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义一个整数变量
|
||||||
|
int i = 10;
|
||||||
|
System.out.println(i);
|
||||||
|
//简写形式定义一个整数数组
|
||||||
|
int[] arr = {1, 2, 3};
|
||||||
|
int[] arr1 = {4, 5, 6};
|
||||||
|
System.out.println(arr[1]);
|
||||||
|
System.out.println(arr1[2]);
|
||||||
|
|
||||||
|
System.out.println(arr);
|
||||||
|
System.out.println(arr1);
|
||||||
|
|
||||||
|
arr = arr1;
|
||||||
|
System.out.println(arr);
|
||||||
|
System.out.println(arr1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayIndexOutOfBoundsException
|
||||||
|
* 数组索引越界异常
|
||||||
|
* 注意:数组的长度固定不变的,数组的最大的索引值为arr.length-1
|
||||||
|
* 0~(arr.length-1)
|
||||||
|
*/
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {1, 2, 3};
|
||||||
|
System.out.println(arr[0]);
|
||||||
|
System.out.println(arr[1]);
|
||||||
|
System.out.println(arr[2]);
|
||||||
|
//如果使用错误的索引访问数组的数据,那就会报越界异常java.lang.ArrayIndexOutOfBoundsException
|
||||||
|
//System.out.println(arr[3]);
|
||||||
|
int index = 2;
|
||||||
|
if (index > 0 && index < arr.length) {
|
||||||
|
System.out.println(arr[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序正常结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {1, 2, 3};
|
||||||
|
//arr中保存的是堆内存的地址
|
||||||
|
System.out.println(arr[0]);//1
|
||||||
|
//null可以给引用数据类型进行初始化赋值
|
||||||
|
arr = null;
|
||||||
|
//如果引用数据类型,是null还继续操作,那就java.lang.NullPointerException(空指针异常)
|
||||||
|
//添加一些健壮性代码
|
||||||
|
if (arr != null) {
|
||||||
|
System.out.println(arr[0]);
|
||||||
|
}
|
||||||
|
System.out.println("程序正常结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数组的遍历
|
||||||
|
* 遍历:将数组中的每个数据,一一展示
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};//100个
|
||||||
|
//请将数组中每个值打印输出
|
||||||
|
/*System.out.println(arr[0]);
|
||||||
|
System.out.println(arr[1]);
|
||||||
|
System.out.println(arr[2]);*/
|
||||||
|
//使用for循环来遍历数组(正向for循环)
|
||||||
|
for (int index = 0; index < arr.length; index++) {
|
||||||
|
System.out.println(arr[index]);
|
||||||
|
}
|
||||||
|
System.out.println("---------");
|
||||||
|
|
||||||
|
//使用for循环来遍历数组(反向for循环)
|
||||||
|
for (int i = arr.length - 1; i >= 0; i--) {
|
||||||
|
System.out.println(arr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
/*
|
||||||
|
数组的反转:定义一个数组,将原数组中的数据,倒转保存到另一个数组中
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr1 = {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
//将arr1中的数据,倒着保存到另一个数组中
|
||||||
|
int[] arr2 = new int[arr1.length];
|
||||||
|
|
||||||
|
/*
|
||||||
|
反向遍历,赋值新数组
|
||||||
|
i -->4,3,2,1,0
|
||||||
|
arr2 -->0,1,2,3,4
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (int i = arr1.length - 1; i >= 0; i--) {
|
||||||
|
int temp = arr1[i];
|
||||||
|
arr2[(arr1.length- 1) - i] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//查看arr2数组的内容
|
||||||
|
for (int i = 0; i < arr2.length; i++) {
|
||||||
|
System.out.println(arr2[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
/*
|
||||||
|
数组作为方法参数_传递地址
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义2个整数的数组
|
||||||
|
int[] arr1 = {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
printArr(arr1);
|
||||||
|
|
||||||
|
int[] arr2 = {100,200,300};
|
||||||
|
printArr(arr2);
|
||||||
|
|
||||||
|
String[] strArr = {"a","b"};
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义1个方法,针对整数数组进行遍历
|
||||||
|
public static void printArr(int[] arr){
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
System.out.println(arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
/*
|
||||||
|
数组作为方法返回值_返回地址
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {1,2,3,4,5,6,7};
|
||||||
|
//1.打印原数组
|
||||||
|
printArr(arr);
|
||||||
|
System.out.println("-------------------");
|
||||||
|
//2.调用获取偶数新数组的方法
|
||||||
|
int[] newArr = getOsArray(arr);
|
||||||
|
//3.打印新数组
|
||||||
|
//创建相同长度的数组,只获取偶数保存返回 [2,4,6]
|
||||||
|
printArr(newArr);
|
||||||
|
}
|
||||||
|
//定义一个方法,传入一个数组,将数组中的偶数,封装起来,并将只含有偶数的新数组返回回来
|
||||||
|
public static int[] getOsArray(int[] arr) {
|
||||||
|
//先获取偶数的数量
|
||||||
|
int osCount = getOsCount(arr);
|
||||||
|
|
||||||
|
//动态初始化一个偶数的数量长度整数数组
|
||||||
|
int[] newArr = new int[osCount];
|
||||||
|
int index = 0;//对新数组的索引,手动管理
|
||||||
|
|
||||||
|
//遍历判断是偶数则保存到新数组中
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];//遍历时,每个元素
|
||||||
|
if (temp % 2 == 0) {
|
||||||
|
newArr[index++] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个获取整数数组中偶数的数量
|
||||||
|
public static int getOsCount(int[] arr) {
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];
|
||||||
|
if (temp % 2 == 0) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//方式二
|
||||||
|
public static int[] getOsArray2(int[] arr) {
|
||||||
|
//动态初始化一个整数数组
|
||||||
|
int[] newArr = new int[arr.length];
|
||||||
|
int index = 0;//对新数组的索引,手动管理
|
||||||
|
|
||||||
|
//遍历判断是偶数则保存到新数组中
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];//遍历时,每个元素
|
||||||
|
if (temp % 2 == 0) {
|
||||||
|
newArr[index++] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//方式一
|
||||||
|
public static int[] getOsArray1(int[] arr) {
|
||||||
|
//动态初始化一个整数数组
|
||||||
|
int[] newArr = new int[arr.length];
|
||||||
|
//遍历判断是偶数则保存到新数组中
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];//遍历时,每个元素
|
||||||
|
if (temp % 2 == 0) {
|
||||||
|
newArr[i] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void printArr(int[] arr){
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
System.out.print(arr[i]+"-");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
//数组的操作_获取数组中的最大
|
||||||
|
public class Test06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {11,2,345,55,88,99,543};
|
||||||
|
|
||||||
|
//获取数组中每个元素,依次比较,如果大,就记录,循环结束,那么记录的就是最大值
|
||||||
|
//定义一个最大值的变量,用来记录比较中,较大的值
|
||||||
|
int max = arr[0];//假设第一个空间的值最大
|
||||||
|
//遍历比较
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];//当前遍历得到的数组中的元素之一
|
||||||
|
if (temp < max) {
|
||||||
|
max = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("当前最小值:"+max);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
/*
|
||||||
|
基本数据类型作为方法参数的操作
|
||||||
|
断点:break point
|
||||||
|
断点使用:在要调用的方法的第一行,点上断点
|
||||||
|
F8:向下执行一行代码
|
||||||
|
F7:进入方法中
|
||||||
|
shift+F8:跳出方法
|
||||||
|
*/
|
||||||
|
public class Test10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义2个整数(基本数据类型)
|
||||||
|
int a = 10;
|
||||||
|
int b = 20;
|
||||||
|
//调用一个方法
|
||||||
|
changValue(a,b);
|
||||||
|
System.out.println(a);
|
||||||
|
System.out.println(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void changValue(int a,int b) {
|
||||||
|
a = a+a;
|
||||||
|
b = b+b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
|
||||||
|
public class Test11 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {10, 20};
|
||||||
|
System.out.println(arr);
|
||||||
|
changeValue(arr);
|
||||||
|
System.out.println(arr[0]);
|
||||||
|
System.out.println(arr[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void changeValue(int[] arr) {
|
||||||
|
arr[0] = arr[0] + arr[0];
|
||||||
|
arr[1] = arr[1] + arr[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.inmind.constructor03;
|
||||||
|
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建学生
|
||||||
|
Student s = new Student();
|
||||||
|
System.out.println(s.name);
|
||||||
|
//直接创建学生并携带name属性
|
||||||
|
Student s1 = new Student("张三");
|
||||||
|
System.out.println(s1.name );
|
||||||
|
Student s2 = new Student("李四",18);
|
||||||
|
System.out.println(s2.name);
|
||||||
|
System.out.println(s2.age);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.inmind.constructor03;
|
||||||
|
/*
|
||||||
|
Student():就是学生类的默认无参构造方法
|
||||||
|
|
||||||
|
普通自定义方法:
|
||||||
|
方法修饰符 返回值类型 方法名(参数列表){
|
||||||
|
return;方法体
|
||||||
|
}
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
方法修饰符 构造方法名(参数列表){
|
||||||
|
java方法体
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
注意:当源文件(.java文件),进行编译时,编译器扫描整个类的内容,如果没有发现构造方法,那么它会自动帮你添加一个
|
||||||
|
默认无参构造方法,如果你写了构造方法,编译器就不会自动添加默认无参构造方法
|
||||||
|
|
||||||
|
1.构造方法没有返回值类型
|
||||||
|
2.构造方法必须与类名保持一致
|
||||||
|
3.构造方法可以重载
|
||||||
|
|
||||||
|
构造方法的作用:通过new调用构造方法,创建对象,并且对该对象的属性进行赋值
|
||||||
|
*/
|
||||||
|
public class Student {
|
||||||
|
//成员变量
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
//构造方法
|
||||||
|
//无参构造方法
|
||||||
|
public Student(){
|
||||||
|
System.out.println("学生类的无参构造方法执行了");
|
||||||
|
}
|
||||||
|
|
||||||
|
//有参构造
|
||||||
|
public Student(String name){
|
||||||
|
this.name = name;
|
||||||
|
System.out.println("学生类的有参构造方法执行了");
|
||||||
|
}
|
||||||
|
|
||||||
|
//满参构造&全参构造
|
||||||
|
public Student(String name,int age){
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
System.out.println("学生类的满参构造方法执行了");
|
||||||
|
}
|
||||||
|
|
||||||
|
//成员方法
|
||||||
|
public void study(){
|
||||||
|
System.out.println("学生学习");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.inmind.javabean04;
|
||||||
|
/*
|
||||||
|
* 最贵的书。
|
||||||
|
|
||||||
|
* 定义一个图书Book类。
|
||||||
|
|
||||||
|
- 属性:图书编号,书名,价格,出版日期
|
||||||
|
- 构造方法:
|
||||||
|
- 无参构造方法,满参构造方法
|
||||||
|
- 成员方法:
|
||||||
|
- get/set方法
|
||||||
|
- showBook方法,输出图书信息
|
||||||
|
|
||||||
|
* 定义测试类,使用满参构造方法,创建三(多)个Book对象,判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Book {
|
||||||
|
private String bid;
|
||||||
|
private String bName;
|
||||||
|
private double price;
|
||||||
|
private String date;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String bid, String bName, double price, String date) {
|
||||||
|
this.bid = bid;
|
||||||
|
this.bName = bName;
|
||||||
|
this.price = price;
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBid() {
|
||||||
|
return bid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBid(String bid) {
|
||||||
|
this.bid = bid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getbName() {
|
||||||
|
return bName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setbName(String bName) {
|
||||||
|
this.bName = bName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(String date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void showBook() {
|
||||||
|
System.out.println("图书信息{" +
|
||||||
|
"图书编号='" + bid + '\'' +
|
||||||
|
", 书名='" + bName + '\'' +
|
||||||
|
", 价格=" + price +
|
||||||
|
", 日期='" + date + '\'' +
|
||||||
|
'}');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.javabean04;
|
||||||
|
//标准的javaBean alt+insert
|
||||||
|
public class Dog {
|
||||||
|
//成员变量
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private String brand;
|
||||||
|
|
||||||
|
//构造方法
|
||||||
|
public Dog(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dog(String name, int age, String brand) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.brand = brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dog() {
|
||||||
|
}
|
||||||
|
|
||||||
|
//get/set方法
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBrand() {
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBrand(String brand) {
|
||||||
|
this.brand = brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.javabean04;
|
||||||
|
/*
|
||||||
|
*定义测试类,使用满参构造方法,创建三(多)个Book对象,
|
||||||
|
* 判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Test05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Book book1 = new Book("b001", "java从入门到入土", 99.9, "2020-01-01");
|
||||||
|
Book book2 = new Book("b002", "数据库删库跑路", 69.9, "2023-01-01");
|
||||||
|
Book book3 = new Book("b003", "java编程思想", 199.9, "2025-01-01");
|
||||||
|
//定义一个存放书的数组
|
||||||
|
Book[] bookArr = {book1,book2,book3};
|
||||||
|
//判断哪本书最贵
|
||||||
|
Book maxBook = bookArr[0];
|
||||||
|
//遍历数组,判断谁最贵
|
||||||
|
for (int i = 0; i < bookArr.length; i++) {
|
||||||
|
//获取对应索引的书对象
|
||||||
|
Book temp = bookArr[i];
|
||||||
|
if (temp.getPrice() > maxBook.getPrice()){
|
||||||
|
maxBook = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//调用展示书的方法即可
|
||||||
|
maxBook.showBook();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.inmind.object01;
|
||||||
|
/*
|
||||||
|
2.使用类描述一个事物讲解(学生)
|
||||||
|
面向对象:在java中先有类,才有对象
|
||||||
|
|
||||||
|
如何使用类(class),描述生活中的事物??
|
||||||
|
一类事物是由特征(变量)和行为(方法)组成。
|
||||||
|
----------------------------------------------------
|
||||||
|
面向对象给学生类属性赋值并调用学生类方法
|
||||||
|
|
||||||
|
类与对象的关系:类是抽象(设计图),对象是具体存在的实例(根据类创建的)
|
||||||
|
|
||||||
|
一个类的成员变量和方法,都必须通过该类的对象来操作。
|
||||||
|
|
||||||
|
如何创建出对象???
|
||||||
|
创建对象的格式:
|
||||||
|
类名 对象名 = new 类名();
|
||||||
|
Student s = new Student();
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*
|
||||||
|
创建出一个学生类的对象
|
||||||
|
|
||||||
|
Student:表示创建的对象的类型
|
||||||
|
s:表示对象名,标识符的一种,s中保存的是真正的学生对象的地址
|
||||||
|
new:在堆内存中创建的内容,(成员变量是有默认值)
|
||||||
|
Student():与之前的类型保持一致
|
||||||
|
|
||||||
|
强调:java中的引用数据类型有数组,类,接口
|
||||||
|
*/
|
||||||
|
Student s = new Student();
|
||||||
|
//对象的属性值的获取
|
||||||
|
System.out.println(s.name);
|
||||||
|
System.out.println(s.age);
|
||||||
|
System.out.println(s.id);
|
||||||
|
System.out.println(s.score);
|
||||||
|
System.out.println(s.gender);
|
||||||
|
//对象的属性值的赋值
|
||||||
|
s.name = "张三";
|
||||||
|
s.age = 20;
|
||||||
|
s.id = 1;
|
||||||
|
s.score = 8.8;
|
||||||
|
s.gender = "男";
|
||||||
|
System.out.println(s.name);
|
||||||
|
System.out.println(s.age);
|
||||||
|
System.out.println(s.id);
|
||||||
|
System.out.println(s.score);
|
||||||
|
System.out.println(s.gender);
|
||||||
|
|
||||||
|
//调用对象的行为
|
||||||
|
s.eat("鸡腿饭");
|
||||||
|
s.sleep();
|
||||||
|
s.study("java");
|
||||||
|
|
||||||
|
//再创建一个叫小红的女生的学生对象
|
||||||
|
Student s1 = new Student();
|
||||||
|
s1.name = "小红";
|
||||||
|
s1.eat("麻辣烫");
|
||||||
|
s1.sleep();
|
||||||
|
s1.study("python");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.object01;
|
||||||
|
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student s = new Student();
|
||||||
|
//对象的属性值的获取
|
||||||
|
showStudent(s);
|
||||||
|
//对象的属性值的赋值
|
||||||
|
s.name = "张三";
|
||||||
|
s.age = 20;
|
||||||
|
s.id = 1;
|
||||||
|
s.score = 8.8;
|
||||||
|
s.gender = "男";
|
||||||
|
showStudent(s);
|
||||||
|
|
||||||
|
Student s1 = new Student();
|
||||||
|
s1.name = "李四";
|
||||||
|
showStudent(s1);
|
||||||
|
System.out.println("-----------");
|
||||||
|
s.study("java");
|
||||||
|
s1.study("python");
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个将指定学生对象的属性全部展示的功能方法
|
||||||
|
public static void showStudent(Student s){
|
||||||
|
System.out.println(s.name);
|
||||||
|
System.out.println(s.age);
|
||||||
|
System.out.println(s.id);
|
||||||
|
System.out.println(s.score);
|
||||||
|
System.out.println(s.gender);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.inmind.object01;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学生类
|
||||||
|
|
||||||
|
总结:
|
||||||
|
1.在java中描述一类事物,由特征(成员变量)和行为(成员方法,不要使用static描述)组成
|
||||||
|
2.在类中,并不是都必须要主方法,如果一个类要运行,并得到一效果,那么就必须要主方法
|
||||||
|
在java中,一个类只是用来描述一类事物,就不要主方法
|
||||||
|
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
成员变量:处于成员位置的变量
|
||||||
|
成员位置:类中方法外
|
||||||
|
局部变量:在方法中定义的变量
|
||||||
|
|
||||||
|
|
||||||
|
注意:在方法中,如果使用了成员变量与局部变量同名的变量,符合就近原则,直接使用的是局部变量
|
||||||
|
|
||||||
|
成员变量与局部变量的区别:
|
||||||
|
1.定义的位置不同
|
||||||
|
成员变量:类中方法外
|
||||||
|
局部变量:方法中
|
||||||
|
2.作用范围不同
|
||||||
|
成员变量:整个类中都能用
|
||||||
|
局部变量:只能在定义该变量的方法中
|
||||||
|
3.处于内存的位置不同
|
||||||
|
成员变量:在堆内存中
|
||||||
|
局部变量:在栈内存
|
||||||
|
4.默认值不同:
|
||||||
|
成员变量:有默认值的
|
||||||
|
局部变量:没有默认值
|
||||||
|
5.生命周期不同
|
||||||
|
成员变量:随着对象的出现而出现,随着对象的销毁而销毁
|
||||||
|
局部变量:随着方法的出现而出现,随着方法的销毁而销毁
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Student {
|
||||||
|
//属性(成员变量)
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
int id;
|
||||||
|
double score;
|
||||||
|
String gender;
|
||||||
|
|
||||||
|
public void study(String book) {//book就是一个局部变量
|
||||||
|
String name = "呵呵哒";//局部变量
|
||||||
|
//System.out.println(name+"在学"+book);//此处我们希望是展示成员变量name,但是程序采用了局部变量
|
||||||
|
System.out.println(this.name+"在学"+book);
|
||||||
|
}
|
||||||
|
//行为(注意要定义一类事物的行为时,不要加static)
|
||||||
|
|
||||||
|
public void eat(String food) {
|
||||||
|
System.out.println(name+"在吃"+food);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sleep() {
|
||||||
|
System.out.println(name+"在睡觉");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.private02;
|
||||||
|
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个学生对象
|
||||||
|
Student s = new Student();
|
||||||
|
//其他类无法对private私有化属性进行随意的访问了!!!
|
||||||
|
/*s.name = "张三";
|
||||||
|
s.age = 120;*/
|
||||||
|
System.out.println("学生对象s:"+s);
|
||||||
|
s.setName("张三");
|
||||||
|
s.setAge(21);
|
||||||
|
showStudent(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void showStudent(Student s){
|
||||||
|
/*System.out.println(s.name);
|
||||||
|
System.out.println(s.age);*/
|
||||||
|
System.out.println(s.getName());
|
||||||
|
System.out.println(s.getAge());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.private02;
|
||||||
|
|
||||||
|
/*
|
||||||
|
面向对象之封装的作用:代码的安全性
|
||||||
|
如何封装???
|
||||||
|
封装的步骤
|
||||||
|
1. 使用 private 关键字来修饰成员变量。
|
||||||
|
2. 对需要访问的成员变量,提供对应的一对 getXxx 方法 、setXxx 方法。
|
||||||
|
*/
|
||||||
|
public class Student {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
//注意:编程规范最好要见其名知其意
|
||||||
|
|
||||||
|
//定义一对get/set方法对私有的,允许访问的 属性进行操作
|
||||||
|
public String getName(){
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name){
|
||||||
|
//此处发生了局部变量与成员变量同名的问题
|
||||||
|
//name = name;//此代码的含义:局部变量name = 局部变量name
|
||||||
|
/*
|
||||||
|
this:表示的是该方法的调用者对象,谁调用方法setName,this就是谁
|
||||||
|
注意:在一个类中只要使用到成员变量和成员方法,代码的前面都会默认加上this.
|
||||||
|
|
||||||
|
*/
|
||||||
|
System.out.println("this关键字:"+this);
|
||||||
|
this.name = name;//此代码的含义:成员变量name = 局部变量name
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getAge(){
|
||||||
|
return this.age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age){
|
||||||
|
if (age > 100 || age < 0) {
|
||||||
|
System.out.println("您输入的年龄有误,只能是0~100");
|
||||||
|
//此处直接将方法结束即可
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
/*
|
||||||
|
* 最贵的书。
|
||||||
|
|
||||||
|
* 定义一个图书Book类。
|
||||||
|
|
||||||
|
- 属性:图书编号,书名,价格,出版日期
|
||||||
|
- 构造方法:
|
||||||
|
- 无参构造方法,满参构造方法
|
||||||
|
- 成员方法:
|
||||||
|
- get/set方法
|
||||||
|
- showBook方法,输出图书信息
|
||||||
|
|
||||||
|
* 定义测试类,使用满参构造方法,创建三(多)个Book对象,判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Book {
|
||||||
|
private String bid;
|
||||||
|
private String bName;
|
||||||
|
private double price;
|
||||||
|
private String date;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String bid, String bName, double price, String date) {
|
||||||
|
this.bid = bid;
|
||||||
|
this.bName = bName;
|
||||||
|
this.price = price;
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBid() {
|
||||||
|
return bid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBid(String bid) {
|
||||||
|
this.bid = bid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getbName() {
|
||||||
|
return bName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setbName(String bName) {
|
||||||
|
this.bName = bName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(String date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void showBook() {
|
||||||
|
System.out.println("图书信息{" +
|
||||||
|
"图书编号='" + bid + '\'' +
|
||||||
|
", 书名='" + bName + '\'' +
|
||||||
|
", 价格=" + price +
|
||||||
|
", 日期='" + date + '\'' +
|
||||||
|
'}');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个保存书的容器
|
||||||
|
Book[] arr = new Book[3];
|
||||||
|
arr[0] = new Book();
|
||||||
|
arr[1] = new Book();
|
||||||
|
arr[2] = new Book();
|
||||||
|
|
||||||
|
//Book[]:对象数组,还想再加2本书??不能,数组是长度固定,arr中无法再添加新的书,除非重新new一个新数组
|
||||||
|
//对象数组能直接删除一个书对象??不能,只能修改
|
||||||
|
//为了解决以上的问题,有个更好的容器,ArrayList集合
|
||||||
|
//ArrayList的特点:1.长度可变,2.可以增删改查 3.可以存放任意的引用类型,(基本数据类型不能保存到集合中!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
注意:ArrayList在使用时,将同一种数据类型保存在一个容器,直接指定泛型
|
||||||
|
*/
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个集合容器
|
||||||
|
ArrayList list = new ArrayList<>();//如果泛型不写,默认保存Object类型的数据,任意的引用类型的祖宗
|
||||||
|
list.add(1);
|
||||||
|
list.add(1.1);
|
||||||
|
list.add(true);
|
||||||
|
list.add("hehe");
|
||||||
|
list.add(new Book());
|
||||||
|
|
||||||
|
//创建一个存放字符串的集合容器
|
||||||
|
ArrayList<String> list1 = new ArrayList<>();
|
||||||
|
list1.add("张三");
|
||||||
|
list1.add("李四");
|
||||||
|
System.out.println(list1);//list1保存的是地址,但是由于底层源码实现,修改了输出结果,原本是地址,转为该地址对应的内容了
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
10.常用类-ArrayList-其他常用方法说明
|
||||||
|
添加:
|
||||||
|
void add(int index, E element) 在此列表中的指定位置插入指定的元素
|
||||||
|
public boolean add(E e):将指定的元素添加到此集合的尾部。
|
||||||
|
|
||||||
|
删除
|
||||||
|
public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
|
||||||
|
boolean remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。
|
||||||
|
|
||||||
|
修改:
|
||||||
|
E set(int index, E element) 用指定的元素替换此列表中指定位置的元素。
|
||||||
|
|
||||||
|
查询
|
||||||
|
public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。
|
||||||
|
|
||||||
|
public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。
|
||||||
|
boolean contains(Object o) 如果此列表包含指定的元素,则返回 true
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个存放字符串的集合容器
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
/*
|
||||||
|
添加:
|
||||||
|
void add(int index, E element) 在此列表中的指定位置插入指定的元素 【插队】
|
||||||
|
public boolean add(E e):将指定的元素添加到此集合的尾部。 【排队】
|
||||||
|
*/
|
||||||
|
list.add("杨幂");
|
||||||
|
list.add("迪丽热巴");
|
||||||
|
list.add("白鹿");
|
||||||
|
list.add("王宝强");
|
||||||
|
list.add("白鹿");
|
||||||
|
System.out.println(list);
|
||||||
|
//让张三插队到王宝强的前面
|
||||||
|
list.add(2,"张三");
|
||||||
|
System.out.println(list);
|
||||||
|
|
||||||
|
/*
|
||||||
|
删除
|
||||||
|
public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
|
||||||
|
boolean remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。
|
||||||
|
*/
|
||||||
|
//删除碍眼的张三
|
||||||
|
String removedValue = list.remove(2);
|
||||||
|
System.out.println("removedValue:"+removedValue);
|
||||||
|
System.out.println(list);
|
||||||
|
|
||||||
|
//删除李四
|
||||||
|
System.out.println(list.remove("李四"));//没有则删除失败
|
||||||
|
System.out.println(list.remove("白鹿"));//有则删除成功
|
||||||
|
System.out.println(list);
|
||||||
|
|
||||||
|
/*
|
||||||
|
修改:
|
||||||
|
E set(int index, E element) 用指定的元素替换此列表中指定位置的元素。
|
||||||
|
*/
|
||||||
|
//请将王宝强,改名为王宝宝
|
||||||
|
String preValue = list.set(2, "王宝宝");
|
||||||
|
System.out.println("修改之前的值为:"+preValue);
|
||||||
|
System.out.println(list);
|
||||||
|
|
||||||
|
/*
|
||||||
|
查询
|
||||||
|
public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。
|
||||||
|
*/
|
||||||
|
String res = list.get(1);
|
||||||
|
System.out.println(res);
|
||||||
|
System.out.println(list);
|
||||||
|
|
||||||
|
//经过操作之后集合容器中的数据的长度是多少呢??
|
||||||
|
System.out.println(list.size());
|
||||||
|
|
||||||
|
//集合如何获取集合中对应的所有的元素值呢??for循环遍历
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
String s = list.get(i);
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
//快捷方式:list.fori list.forr
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
System.out.println(list.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
ArrayList_基本类型的存储方式_转换为包装类
|
||||||
|
|
||||||
|
自动装箱:int 型的值 -----装箱成Integer类型的对象
|
||||||
|
自动拆箱:Integer类型的对象-----拆箱为int型的值
|
||||||
|
4类8种的基本数据类型不能存放到集合中,因为集合只能存放引用数据类型
|
||||||
|
|
||||||
|
在java中使用包装类,来对基本数据类型进行包装,包装成引用数据类型
|
||||||
|
|
||||||
|
主要记录int和char的包装类
|
||||||
|
|
||||||
|
|
||||||
|
基本类型 基本类型包装类
|
||||||
|
byte Byte
|
||||||
|
short Short
|
||||||
|
int Integer
|
||||||
|
long Long
|
||||||
|
float Float
|
||||||
|
double Double
|
||||||
|
char Character
|
||||||
|
boolean Boolean
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个保存整数的集合
|
||||||
|
ArrayList<Integer> list = new ArrayList<>();
|
||||||
|
list.add(1);//此处发生了自动装箱:int 型的值 -----装箱成Integer类型的对象
|
||||||
|
list.add(2);
|
||||||
|
list.add(3);
|
||||||
|
System.out.println(list);
|
||||||
|
//从集合中获取第一个元素的值
|
||||||
|
Integer i = list.get(0);
|
||||||
|
int sum = i+10;//此处发生了自动拆箱:Integer类型的对象-----拆箱为int型的值
|
||||||
|
System.out.println(sum);//1
|
||||||
|
//创建一个保存小数的集合
|
||||||
|
ArrayList<Double> list1 = new ArrayList<>();
|
||||||
|
list1.add(1.0);//此处发生了自动装箱:double 型的值 -----装箱成Double类型的对象
|
||||||
|
list1.add(2.1);
|
||||||
|
System.out.println(list1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
常用类-ArrayList-练习2_添加对象
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
//创建一个保存book对象的容器,添加3本书,通过集合遍历判断出哪本书最贵
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ArrayList<Book> bookList = new ArrayList<>();
|
||||||
|
//先创建3本书,或者在添加时,进行创建
|
||||||
|
//Book book1 = new Book("b001", "书1", 10.0, "2020-1-1");
|
||||||
|
Book book2 = new Book("b001", "书2", 10.0, "2020-1-1");
|
||||||
|
Book book3 = new Book("b001", "书3", 50.0, "2020-1-1");
|
||||||
|
bookList.add(new Book("b001", "书1", 30.0, "2020-1-1"));
|
||||||
|
bookList.add(book2);
|
||||||
|
bookList.add(book3);
|
||||||
|
//集合的变量
|
||||||
|
Book maxBook = bookList.get(0);
|
||||||
|
for (int i = 0; i < bookList.size(); i++) {
|
||||||
|
Book book = bookList.get(i);
|
||||||
|
if (book.getPrice() > maxBook.getPrice()) {
|
||||||
|
maxBook = book;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maxBook.showBook();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.random02;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/*
|
||||||
|
随机数
|
||||||
|
*/
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建随机类对象
|
||||||
|
Random random = new Random();
|
||||||
|
//调用对象的方法
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
//3~13 -3 ->0~10
|
||||||
|
//int value = random.nextInt(11)+3;//取值范围为0~9,在java中包头不包尾
|
||||||
|
int value = random.nextInt(3,14);//取值范围为0~9,在java中包头不包尾
|
||||||
|
System.out.println(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.inmind.random02;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
7.常用类-Random-猜数字游戏(1~100)
|
||||||
|
分析:让系统生成一个随机值,由用户来猜,猜的过程要进行提示,大了,还是小了
|
||||||
|
1.使用Random得到一个随机值1~100
|
||||||
|
2.使用Scanner获取用户猜的整数(不停地猜,while-true死循环,break)
|
||||||
|
a.将用户输入的值与生成的值,进行比较,如果大了提示大了
|
||||||
|
b.将用户输入的值与生成的值,进行比较,如果小了提示小了
|
||||||
|
c.将用户输入的值与生成的值,进行比较,如果相等,提示恭喜,结束游戏
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Test04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//使用Random得到一个随机值1~100
|
||||||
|
Random random = new Random();
|
||||||
|
int guessNum = random.nextInt(1, 101);//需要猜测的值
|
||||||
|
System.out.println("随机生成的值:"+guessNum);
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
while (true) {
|
||||||
|
System.out.println("请输入您所猜的值:");
|
||||||
|
int value = sc.nextInt();
|
||||||
|
if (value > guessNum) {
|
||||||
|
System.out.println("您猜的值"+value+"大了");
|
||||||
|
} else if (value < guessNum) {
|
||||||
|
System.out.println("您猜的值"+value+"小了");
|
||||||
|
}else{
|
||||||
|
System.out.println("恭喜您,猜对了!!!游戏结束");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.scanner01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
该类学习Scanner类的使用:
|
||||||
|
步骤:
|
||||||
|
1.导包 import java.util.Scanner;
|
||||||
|
2.通过构造方法创建对象
|
||||||
|
3.通过scanner已经定义好的方法,使用该类的对象来调用,获取键盘输入的内容
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*
|
||||||
|
System.out:系统的输出方法,在控制台输出内容
|
||||||
|
System.in:系统的输入方法,获取键盘输入的内容
|
||||||
|
*/
|
||||||
|
//通过构造方法创建对象
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int value = sc.nextInt();//阻塞方法,等待用户输入内容,用户不输入永远等,回车是结束符
|
||||||
|
System.out.println(value);
|
||||||
|
sc.nextLine();
|
||||||
|
String str = sc.nextLine();
|
||||||
|
System.out.println(str);
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.inmind.scanner01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
//常用类-Scanner-练习2_录入三个整数求最大值(数组),注意3可以写死,最好能动态
|
||||||
|
public class Test02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.用户手动输入求最大值的数据的数量
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("请您输入要计算的整数的个数:");
|
||||||
|
int count = sc.nextInt();
|
||||||
|
|
||||||
|
int[] arr = new int[count];
|
||||||
|
|
||||||
|
//2.动态创建数组,不停地让用户输入对应的值,并存放到数组中
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
System.out.println("请求输入第"+(i+1)+"个整数");
|
||||||
|
int temp = sc.nextInt();
|
||||||
|
arr[i] = temp;
|
||||||
|
}
|
||||||
|
//3.等用户输入完毕,使用数组的遍历来求最大值
|
||||||
|
int max = arr[0];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];
|
||||||
|
if (temp > max) {
|
||||||
|
max = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("您输入的"+count+"个整数中,最大值为:"+max);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.inmind.arrays03;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
arrays类的2个工具类方法
|
||||||
|
|
||||||
|
public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
|
||||||
|
|
||||||
|
public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class ArraysDemo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5};
|
||||||
|
System.out.println(arr);//[I@4eec7777
|
||||||
|
//查看对应数组的内容
|
||||||
|
System.out.println(Arrays.toString(arr));
|
||||||
|
String[] arr1 = {"hello", "java"};
|
||||||
|
System.out.println(Arrays.toString(arr1));
|
||||||
|
int[] arr2 = {1,222,33,4,555,6};
|
||||||
|
Arrays.sort(arr2);//升序排序
|
||||||
|
System.out.println(Arrays.toString(arr2));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.inmind.math04;
|
||||||
|
/*
|
||||||
|
Math工具类的使用
|
||||||
|
public static double abs(double a) :返回 double 值的绝对值。
|
||||||
|
public static double ceil(double a) :返回大于等于参数的最小的整数。
|
||||||
|
public static double floor(double a) :返回小于等于参数最大的整数。
|
||||||
|
public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
|
||||||
|
*/
|
||||||
|
public class MathDemo11 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
//获取绝对值
|
||||||
|
System.out.println(Math.abs(-100));//100
|
||||||
|
//向上取整
|
||||||
|
System.out.println(Math.ceil(-3.7));//-3.0
|
||||||
|
//向下取整
|
||||||
|
System.out.println(Math.floor(-3.4));//-4.0
|
||||||
|
//四舍五入
|
||||||
|
System.out.println(Math.round(4.4));//4
|
||||||
|
System.out.println(Math.round(4.5));//5
|
||||||
|
}
|
||||||
|
|
||||||
|
public void methos() {
|
||||||
|
System.out.println(Math.round(4.4));//4
|
||||||
|
System.out.println(Math.round(4.5));//5
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.static02;
|
||||||
|
/*
|
||||||
|
static关键字的使用:
|
||||||
|
可以修饰成员变量和成员方法。
|
||||||
|
1.使用static修饰成员变量的使用方式:只跟类有关,与对象无关了,并且该类的每个对象都共享了该静态变量的值
|
||||||
|
静态成员变量的使用方式:
|
||||||
|
a.类名.静态变量名(推荐使用)
|
||||||
|
b.对象名.静态变量名(不推荐使用)
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建2个学生(同班同学)
|
||||||
|
Student s1 = new Student("张三", 20);
|
||||||
|
Student s2 = new Student("李四", 21);
|
||||||
|
//你们在哪个教室上课??
|
||||||
|
System.out.println(Student.clazz);
|
||||||
|
System.out.println(s1.clazz);
|
||||||
|
|
||||||
|
//告诉张三,你们今天上课的教室是1903室
|
||||||
|
s1.clazz = "1903室";
|
||||||
|
//遇到李四,你知道今天的教室在哪吗??
|
||||||
|
System.out.println(s2.clazz);
|
||||||
|
Student s3 = new Student("王五", 20);
|
||||||
|
System.out.println(s3.clazz);
|
||||||
|
Student.clazz = "1904";//修改静态变量
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.inmind.static02;
|
||||||
|
/*
|
||||||
|
static关键字的使用:
|
||||||
|
可以修饰成员变量和成员方法。
|
||||||
|
1.使用static修饰成员变量的使用方式:只跟类有关,与对象无关了,并且该类的每个对象都共享了该变量的值
|
||||||
|
静态成员变量的使用方式:
|
||||||
|
a.类名.静态变量名(推荐使用)
|
||||||
|
b.对象名.静态变量名(不推荐使用)
|
||||||
|
2.使用static修饰成员方法的使用方式:只跟类有关,与对象无关了,并且该类的每个对象都可以直接调用静态方法
|
||||||
|
区分
|
||||||
|
a.自定义成员方法:(对象方法),它的定义是给每个对象来调用,必须通过创建对象来调用
|
||||||
|
b 静态方法:(类方法),它的定义是给对应的类直接调用,跟每个对象无关的操作
|
||||||
|
静态方法的使用方式:
|
||||||
|
a.类名.静态方法名(参数列表)(推荐使用)
|
||||||
|
b.对象名.静态方法名(参数列表)(不推荐使用)
|
||||||
|
|
||||||
|
静态方法的作用:静态方法调用的比较方便,不需要创建对象,常用于对应的工具类的抽取,在一个项目中有可能有特定的功能代码,到处都要用,抽取成静态方法,直接通过类名.静态方法名,直接调用功能即可。
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//学生报名,计数,来了几位同学,统计
|
||||||
|
Student s1 = new Student("张三1", 18);
|
||||||
|
Student s2 = new Student("张三2", 18);
|
||||||
|
Student s3 = new Student("张三3", 18);
|
||||||
|
//校长问有多少同学了??
|
||||||
|
Student.showStudentCount();
|
||||||
|
s1.study();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.inmind.static02;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习静态的注意事项:
|
||||||
|
静态方法调用的注意事项:
|
||||||
|
1.静态方法可以直接访问类变量(静态变量)和静态方法。
|
||||||
|
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,反之,成员方法可以直接访问静态变量或静态方法。(先人(静态)不知道后人(对象),后人(张三)是知道先人(刘备)的)
|
||||||
|
3.静态方法中,不能使用this关键字。
|
||||||
|
|
||||||
|
static 修饰的内容:
|
||||||
|
是随着类的加载而加载的,且只加载一次。(只跟类有关)
|
||||||
|
存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。
|
||||||
|
它优先于对象存在,所以,可以被所有对象共享。
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
static String clazz;//班级
|
||||||
|
|
||||||
|
static int studentCount = 0;//学生总人数
|
||||||
|
|
||||||
|
|
||||||
|
public Student(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
|
||||||
|
//只要创建一个学生对象,那么学生人数就自动加1
|
||||||
|
studentCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//静态方法,不再是一个成员方法了(不能访问属性和行为)
|
||||||
|
public static void showStudentCount(){
|
||||||
|
// System.out.println(this);
|
||||||
|
System.out.println(studentCount);
|
||||||
|
/*study(); 静态方法是不能访问非静态内容的!!!!
|
||||||
|
System.out.println(name);
|
||||||
|
System.out.println(age);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
//成员方法(非静态内容,是可以访问静态内容)
|
||||||
|
public void study(){
|
||||||
|
System.out.println(this.name+"的班级有"+studentCount+"人");
|
||||||
|
System.out.println(this);
|
||||||
|
showStudentCount();
|
||||||
|
System.out.println(clazz);
|
||||||
|
System.out.println(studentCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
常用类_String的概述和特点
|
||||||
|
1.String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
|
||||||
|
""双引号字符串就是String类的对象,所以就可以直接调用String类的方法
|
||||||
|
2.字符串不变; 它们的值在创建后不能被更改。 因为String对象是不可变的,它们可以被共享。
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student s1 = new Student();
|
||||||
|
Student s2 = new Student();
|
||||||
|
System.out.println(s1);//com.inmind.string01.Student@3b07d329
|
||||||
|
System.out.println(s1 == s2);//false
|
||||||
|
System.out.println("-----------------------");
|
||||||
|
String str1 = "abc";//str1保存的是地址!!
|
||||||
|
String str2 = "abc";//str2保存的也是地址!!
|
||||||
|
System.out.println(str1);//abc,由于println打印方法调用的是string类的toString,虽然是地址,但是输出了内容
|
||||||
|
System.out.println(str1 == str2);//true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
String的构造方法的作用:将其他类型的数据,转换成String
|
||||||
|
String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
|
||||||
|
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||||
|
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来
|
||||||
|
//字节数组转为字符串的方式
|
||||||
|
byte[] bytes = {97,98,99};//ascii-->97---a
|
||||||
|
String s = new String(bytes);
|
||||||
|
System.out.println(s);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void method02(String[] args) {
|
||||||
|
//public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||||
|
//字符串其实就是一串字符(底层就是字符数组实现)
|
||||||
|
char[] chars = {'a','b','c'};
|
||||||
|
String s = new String(chars);
|
||||||
|
System.out.println(s);//abc
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void method1(String[] args) {
|
||||||
|
//String(String original) 初始化新创建的String对象(迷惑性,主要就是理解String常量池,和new关键字的作用)
|
||||||
|
String str1 = "abc";
|
||||||
|
String str2 = new String("abc");
|
||||||
|
System.out.println(str1);
|
||||||
|
System.out.println(str2);
|
||||||
|
System.out.println(str1 == str2);//false
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
常用类-String-的比较的方法_equals_equalsIgnoreCase
|
||||||
|
|
||||||
|
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
|
||||||
|
|
||||||
|
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
|
||||||
|
*/
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str1 = "abc";
|
||||||
|
String str2 = new String("abc");
|
||||||
|
System.out.println(str1);
|
||||||
|
System.out.println(str2);
|
||||||
|
System.out.println(str1 == str2);//str1和str2的地址不同,只是他们指向最终的内容是相同,所以此处是false
|
||||||
|
System.out.println(str1.equals(str2));//这才是比较内容,这才是开发时所需的操作
|
||||||
|
String str3 = "AbC";
|
||||||
|
System.out.println(str1.equals(str3));
|
||||||
|
System.out.println(str1.equalsIgnoreCase(str3));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
4.常用类-String-的获取方法_length_concat_charAt_indexOf_subString
|
||||||
|
|
||||||
|
public int length () :返回此字符串的长度。
|
||||||
|
|
||||||
|
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||||||
|
|
||||||
|
public char charAt (int index) :返回指定索引处的 char值。
|
||||||
|
|
||||||
|
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||||||
|
|
||||||
|
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
|
||||||
|
|
||||||
|
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String s = "hello world";
|
||||||
|
//我想截取world这个单词
|
||||||
|
//public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
|
||||||
|
String substring = s.substring(6);
|
||||||
|
System.out.println(substring);
|
||||||
|
//我想截取hello这个单词
|
||||||
|
//public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
|
||||||
|
System.out.println(s.substring(0, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void indexMethod(String[] args) {
|
||||||
|
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||||||
|
String s = "123java321java123";
|
||||||
|
//查询下当前字符串是否含有java内容
|
||||||
|
int index = s.indexOf("java");
|
||||||
|
System.out.println(index);//3
|
||||||
|
//查询下当前字符串中第二个java内容的位置
|
||||||
|
int index1 = s.indexOf("java", 3 + 1);
|
||||||
|
System.out.println(index1);//10
|
||||||
|
System.out.println(s.indexOf("xxx"));//-1
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void charAtMethod(String[] args) {
|
||||||
|
//public char charAt (int index) :返回指定索引处的 char值。
|
||||||
|
String str = "hello world";
|
||||||
|
//获取空格字符
|
||||||
|
System.out.println(str.charAt(5));
|
||||||
|
System.out.println(str.charAt(6));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void concatMethod(String[] args) {
|
||||||
|
//public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||||||
|
String s1 = "hello";
|
||||||
|
String s2 = "java";
|
||||||
|
//String s = s1+s2;
|
||||||
|
String s = s1.concat(s2);
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
public static void lengthMethod(String[] args) {
|
||||||
|
//public int length () :返回此字符串的长度。
|
||||||
|
String s = "123456";
|
||||||
|
System.out.println(s.length());//6
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
5.常用类-String-的转换方法_replace_toCharArray_getBytes
|
||||||
|
|
||||||
|
public char[] toCharArray () :将此字符串转换为新的字符数组。
|
||||||
|
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
|
||||||
|
public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
|
||||||
|
*/
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//public String replace (CharSequence target, CharSequence replacement)
|
||||||
|
String s = "woaijava,javaaiwo";
|
||||||
|
//把所有的a替换为*
|
||||||
|
String newStr = s.replace("a", "*");
|
||||||
|
System.out.println(s);
|
||||||
|
System.out.println(newStr);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void getBytesMethod(String[] args) {
|
||||||
|
//public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
|
||||||
|
String s = "abc";
|
||||||
|
//将字符串转换为字节数组
|
||||||
|
byte[] bytes = s.getBytes();
|
||||||
|
System.out.println(bytes);//[B@4eec7777
|
||||||
|
//数组遍历,查看每个元素
|
||||||
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
|
System.out.println(bytes[i]);
|
||||||
|
}
|
||||||
|
System.out.println(Arrays.toString(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void toCharArrayMethod(String[] args) {
|
||||||
|
//public char[] toCharArray () :将此字符串转换为新的字符数组。
|
||||||
|
String s = "hellojava";
|
||||||
|
//得到字符串的每个字符
|
||||||
|
char[] chars = s.toCharArray();
|
||||||
|
System.out.println(chars);//chars中存放的是堆内存中的地址,打印的实现,直接输出该地址对应的数据
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//给你一个携带学生信息的字符串,请将它切割后创建出一个学生对象
|
||||||
|
String s = "张三,20,男,南京";
|
||||||
|
String[] strArr = s.split(",");
|
||||||
|
System.out.println(strArr.length);//4
|
||||||
|
System.out.println(strArr);//[Ljava.lang.String;@4eec7777
|
||||||
|
Student student = new Student(
|
||||||
|
strArr[0],
|
||||||
|
Integer.parseInt(strArr[1]),
|
||||||
|
strArr[2],
|
||||||
|
strArr[3]
|
||||||
|
);
|
||||||
|
System.out.println(student.gender);
|
||||||
|
System.out.println(student.address);
|
||||||
|
System.out.println(student.age);
|
||||||
|
System.out.println(student.name);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
String gender;
|
||||||
|
String address;
|
||||||
|
|
||||||
|
public Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(String name, int age, String gender, String address) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.gender = gender;
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
键盘录入QQ号码,验证格式的正确性(是否是一个QQ号)。
|
||||||
|
* 必须是5—12位数字。【提示,得遍历字符串,查看是否都是数字字符】
|
||||||
|
* 0不能开头。
|
||||||
|
*/
|
||||||
|
public class Test05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.键盘录入QQ号码
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("请您输入一个QQ号:");
|
||||||
|
String qqNum = sc.nextLine();
|
||||||
|
//2.定义一个方法,接收QQ号码,返回结果
|
||||||
|
boolean result = checkQQ(qqNum);
|
||||||
|
//3.打印结果
|
||||||
|
System.out.println("您输入的qq号"+qqNum+"是否满足格式:"+result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean checkQQ(String qqNum) {
|
||||||
|
/*
|
||||||
|
* 必须是5—12位数字。【提示,得遍历字符串,查看是否都是数字字符】
|
||||||
|
* 0不能开头。
|
||||||
|
*/
|
||||||
|
//判断长度
|
||||||
|
int length = qqNum.length();
|
||||||
|
if (length < 5 || length > 12) {
|
||||||
|
System.out.println("您输入的qq号码,长度不符合");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//判断0不能开头
|
||||||
|
if (qqNum.charAt(0) == '0') {
|
||||||
|
System.out.println("您输入的qq号码,0不能开头");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//字符串的每位都是数字,遍历字符串的每个字符,判断是0~9中间的字符才行
|
||||||
|
for (int i = 0; i < qqNum.length(); i++) {
|
||||||
|
//获取每个字符
|
||||||
|
char temp = qqNum.charAt(i);
|
||||||
|
if (temp < '0' || temp > '9') {
|
||||||
|
System.out.println("您输入的qq号码,必须都是数字");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.inmind.abstract06;
|
||||||
|
|
||||||
|
//抽象类
|
||||||
|
public abstract class Animal {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
public Animal(){
|
||||||
|
System.out.println("抽象父类Animal的无参构造执行了");
|
||||||
|
}
|
||||||
|
|
||||||
|
//抽象方法
|
||||||
|
public abstract void eat();
|
||||||
|
public abstract void eat1();
|
||||||
|
public abstract void eat2();
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.abstract06;
|
||||||
|
/*
|
||||||
|
当一个方法的具体功能,我们无法实现,那么就不要写方法体,abstract修饰,就是抽象方法
|
||||||
|
拥有抽象方法的类,也就是一个抽象类
|
||||||
|
|
||||||
|
抽象类的格式:
|
||||||
|
public abstract class 类名{
|
||||||
|
public abstract void 方法名(参数列表);
|
||||||
|
}
|
||||||
|
----------------------------------------------------------------
|
||||||
|
如何使用抽象类和抽象方法???
|
||||||
|
1.抽象类不能被创建对象的,只能通过子类创建对象
|
||||||
|
2.非抽象的子类,必须实现重写抽象类中的抽象方法
|
||||||
|
3.创建子类对象,调用对应方法
|
||||||
|
|
||||||
|
注意事项:
|
||||||
|
1.抽象类不能创建对象,如果创建,编译无法通过而报错。只能创建其非抽象子类的对象。
|
||||||
|
2.抽象类中,可以有构造方法,是供子类创建对象时,初始化父类成员使用的。
|
||||||
|
3.抽象类中,不一定包含抽象方法,但是有抽象方法的类必定是抽象类。
|
||||||
|
4.抽象类的子类,必须重写抽象父类中所有的抽象方法,否则,编译无法通过而报错。除非该子类也是抽象类。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Dog dog = new Dog();
|
||||||
|
dog.eat();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.inmind.abstract06;
|
||||||
|
|
||||||
|
public class Dog extends Animal{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eat() {
|
||||||
|
System.out.println("狗吃骨头");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eat1() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eat2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.inmind.abstract_test07;
|
||||||
|
|
||||||
|
public class Circle extends Shape {
|
||||||
|
|
||||||
|
//创建圆必定要半径
|
||||||
|
public Circle(int r){
|
||||||
|
super(0,0,r);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getArea() {
|
||||||
|
return (int)(Math.PI*getR()*getR());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getZC() {
|
||||||
|
return (int)(2*Math.PI*getR());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.abstract_test07;
|
||||||
|
/*
|
||||||
|
简单练习:定义一个图形类,求周长和面积,但是有不知道如何实现,只能由具体的图形子类(长方形,圆形)实现
|
||||||
|
|
||||||
|
练习:定义一个圆类,参照长方形类来实现!!!(Math.PI)
|
||||||
|
*/
|
||||||
|
public class Demo11 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个长方形,求面积和周长
|
||||||
|
Rectangle rectangle = new Rectangle(4, 3);
|
||||||
|
System.out.println("长方形面积为:"+rectangle.getArea());
|
||||||
|
System.out.println("长方形周长为:"+rectangle.getZC());
|
||||||
|
//创建一个圆形
|
||||||
|
Circle circle = new Circle(2);
|
||||||
|
System.out.println("圆形面积为:"+circle.getArea());
|
||||||
|
System.out.println("圆形周长为:"+circle.getZC());
|
||||||
|
//如何快速定义出一个正方形的类,还不用自己实现周长和面积的方法
|
||||||
|
// (注意:正方形是一种特殊的长方形)
|
||||||
|
Square square = new Square(4);
|
||||||
|
System.out.println("正方形面积为:"+square.getArea());
|
||||||
|
System.out.println("正方形周长为:"+square.getZC());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.inmind.abstract_test07;
|
||||||
|
//长方形类
|
||||||
|
public class Rectangle extends Shape{
|
||||||
|
|
||||||
|
//在创建长方形对象,必须传入2个属性:长和宽
|
||||||
|
public Rectangle(int chang ,int kuan){
|
||||||
|
super(chang,kuan,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getArea() {
|
||||||
|
return getChang()*getKuan();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getZC() {
|
||||||
|
return 2*(getChang()+getKuan());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.inmind.abstract_test07;
|
||||||
|
//图形类:定义一些功能和属性
|
||||||
|
public abstract class Shape {
|
||||||
|
//至少需要长,宽,半径
|
||||||
|
private int chang;
|
||||||
|
private int kuan;
|
||||||
|
private int r;
|
||||||
|
//定义构造方法
|
||||||
|
public Shape() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Shape(int chang, int kuan, int r) {
|
||||||
|
this.chang = chang;
|
||||||
|
this.kuan = kuan;
|
||||||
|
this.r = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
//设置get/set方法,访问私有属性
|
||||||
|
public int getChang() {
|
||||||
|
return chang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChang(int chang) {
|
||||||
|
this.chang = chang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getKuan() {
|
||||||
|
return kuan;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKuan(int kuan) {
|
||||||
|
this.kuan = kuan;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getR() {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setR(int r) {
|
||||||
|
this.r = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
//求周长和面积
|
||||||
|
public abstract int getArea();
|
||||||
|
public abstract int getZC();
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.abstract_test07;
|
||||||
|
|
||||||
|
public class Square extends Rectangle{
|
||||||
|
//在创建正方形对象,必须传入1个属性:边长
|
||||||
|
public Square(int bc){
|
||||||
|
super(bc,bc);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.extends01;
|
||||||
|
|
||||||
|
public class Assistant extends Employee {
|
||||||
|
|
||||||
|
public void help(){
|
||||||
|
System.out.println("助教在帮助学生");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.extends01;
|
||||||
|
|
||||||
|
public class BanZhuRen extends Employee {
|
||||||
|
|
||||||
|
public void manage(){
|
||||||
|
System.out.println("班主任在管理学生");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.extends01;
|
||||||
|
/*
|
||||||
|
继承的格式和和基本使用
|
||||||
|
public class Fu{
|
||||||
|
}
|
||||||
|
public class Zi extends Fu{
|
||||||
|
}
|
||||||
|
继承的特点:子类拥有父类的所有内容,除了被private和构造方法。
|
||||||
|
继承的好处:实现多个类中相同内容的复用,子类可以实现各自的不同功能需求
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个子类对象,老师对象
|
||||||
|
Teacher t = new Teacher();
|
||||||
|
t.name = "张三";
|
||||||
|
t.age = 35;
|
||||||
|
t.salary = 1.5;
|
||||||
|
t.work();
|
||||||
|
t.teach();
|
||||||
|
Assistant a = new Assistant();
|
||||||
|
a.help();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.extends01;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
double salary;
|
||||||
|
|
||||||
|
public void work(){
|
||||||
|
System.out.println("员工工作了");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.inmind.extends01;
|
||||||
|
|
||||||
|
public class Teacher extends Employee{
|
||||||
|
//默认无参构造,所有类都有构造方法
|
||||||
|
|
||||||
|
public void teach(){
|
||||||
|
System.out.println("老师在教学生学java");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind.extends_constructor04;
|
||||||
|
/*
|
||||||
|
继承中构造方法的访问特点:
|
||||||
|
1.创建子类对象时,一定会先调用父类的构造方法
|
||||||
|
2.super调用父类的构造方法时,必须在子类构造方法的第一行
|
||||||
|
3.super可以调用父类的有参,无参构造方法,但必须在子类构造方法的第一行调用
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Zi zi = new Zi();
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.inmind.extends_constructor04;
|
||||||
|
|
||||||
|
public class Fu {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/*public Fu(){
|
||||||
|
System.out.println("父类的无参构造执行了");
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public Fu(int i){
|
||||||
|
this.i = i;
|
||||||
|
System.out.println("父类的有参构造执行了");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.extends_constructor04;
|
||||||
|
|
||||||
|
public class Zi extends Fu{
|
||||||
|
|
||||||
|
public Zi(){
|
||||||
|
//super();//如果没写super(),编译器会主动地在第一行主动添加
|
||||||
|
super(1);
|
||||||
|
System.out.println("子类的无参构造执行了");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
/*
|
||||||
|
继承中成员方法的访问特点:在继承中访问成员方法时,new的是谁,就优先使用谁的成员方法,如果没有则向上找
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Zi zi = new Zi();
|
||||||
|
zi.methodFu();
|
||||||
|
zi.methodZi();
|
||||||
|
zi.method();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
/*
|
||||||
|
重载(overload) :在同一个类中,方法名相同,参数列表不同(类型,数量,顺序)
|
||||||
|
重写(override) :在父子类中,方法名相同,参数列表相同
|
||||||
|
|
||||||
|
java中的4种权限:public > protected>(default)不写就是默认权限>private
|
||||||
|
|
||||||
|
重写的注意事项:
|
||||||
|
1.子类方法重写父类方法时,必须保证权限大于等于父类的权限
|
||||||
|
2.private的内容不能继承的,也就不能重写
|
||||||
|
3.返回值类型,方法名和参数列表保持一致
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
|
||||||
|
public class Fu {
|
||||||
|
|
||||||
|
public void method(){
|
||||||
|
System.out.println("父类的method方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void methodFu(){
|
||||||
|
System.out.println("父类的methodFu方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
|
||||||
|
public class NewPhone extends Phone{
|
||||||
|
|
||||||
|
//重写来电显示功能 ctrl+o
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void show() {
|
||||||
|
super.show();//沿袭父类的功能
|
||||||
|
//可以对父类的show方法进行扩展
|
||||||
|
//System.out.println("新的显示电话号码");
|
||||||
|
System.out.println("显示头像");
|
||||||
|
System.out.println("备注名称");
|
||||||
|
System.out.println("显示电诈");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
|
||||||
|
public class Phone {
|
||||||
|
public void call(){
|
||||||
|
System.out.println("打电话");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(){
|
||||||
|
System.out.println("发短信");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show(){
|
||||||
|
System.out.println("显示电话号码");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
/*
|
||||||
|
使用继承的语法,定义2个类,并实现一些功能
|
||||||
|
1.定义一个10年前的手机类 Phone
|
||||||
|
2.定义一个现在的手机类 NewPhone
|
||||||
|
3.10年新旧手机是否有相同的属性,如果有,请用继承来封装到父类中
|
||||||
|
4.考虑新旧手机是否有不同的功能,如果有,请定义不同的功能方法,是否有相同的功能,也定义出来
|
||||||
|
|
||||||
|
打电话
|
||||||
|
发短信
|
||||||
|
|
||||||
|
玩抖音
|
||||||
|
玩APP
|
||||||
|
|
||||||
|
重写的作用:对父类的功能,不满意,那就重写即可,可以选择性地沿袭或扩展
|
||||||
|
*/
|
||||||
|
public class Test06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Phone phone = new Phone();
|
||||||
|
|
||||||
|
NewPhone newPhone = new NewPhone();
|
||||||
|
phone.show();
|
||||||
|
|
||||||
|
newPhone.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.inmind.extends_method03;
|
||||||
|
|
||||||
|
|
||||||
|
public class Zi extends Fu{
|
||||||
|
|
||||||
|
|
||||||
|
@Override//该注解,用来验证当前是否是重写
|
||||||
|
public void method(){
|
||||||
|
System.out.println("子类的method方法");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void methodZi(){
|
||||||
|
System.out.println("子类的methodZi方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
//定义职员Clerk类继承Employee类,包含属性:经理,重写show方法,在父类实现基础之上增加展示经理信息。
|
||||||
|
public class Clerk extends Employee{
|
||||||
|
private Manager manager;
|
||||||
|
|
||||||
|
public Clerk() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
public Clerk(String name, int id, String department) {
|
||||||
|
super(name, id, department);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Clerk(String name, int id, String department,Manager manager) {
|
||||||
|
super(name, id, department);
|
||||||
|
this.manager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Manager getManager() {
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setManager(Manager manager) {
|
||||||
|
this.manager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
//重写show方法,在父类实现基础之上增加展示经理信息
|
||||||
|
@Override
|
||||||
|
public void show() {
|
||||||
|
super.show();//沿袭
|
||||||
|
System.out.println("我的经理是:"+this.manager.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
/*
|
||||||
|
super关键字的作用:
|
||||||
|
1.super,可以在子类的成员方法中,访问父类的成员变量,格式:super.父类成员变量名
|
||||||
|
2.super,可以在子类,的成员方法中,访问父类的成员方法,格式:super.父类成员方法名
|
||||||
|
3.super,可以在子类的构造方法中,访问父类的构造方法,但是只能在第一行,格式:super(参数列表)
|
||||||
|
|
||||||
|
super:表示父类的空间
|
||||||
|
|
||||||
|
|
||||||
|
this关键字的作用:
|
||||||
|
1.this,可以区分当前类中的局部变量和成员变量
|
||||||
|
2.this,可以在本类中调用其他的成员方法
|
||||||
|
3.this,可以在本类的构造方法中,调用其他的重载的构造方法
|
||||||
|
关于第三点的注意事项:
|
||||||
|
a.this必须在第一行,不能自己调用自己
|
||||||
|
b.this和super在调用构造方法时,都必须在第一行,所以在同一个构造方法中,不能同时出现的
|
||||||
|
c.this可以调用其他的重载的构造方法,不能相互调用
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Zi zi = new Zi();
|
||||||
|
zi.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
//定义员工Employee类。包含属性:姓名,工号,部门,成员方法show,方法内展示员工信息。
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private String department;
|
||||||
|
|
||||||
|
public Employee() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(String name, int id, String department) {
|
||||||
|
this.name = name;
|
||||||
|
this.id = id;
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDepartment() {
|
||||||
|
return department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartment(String department) {
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show(){
|
||||||
|
System.out.println("我叫"+this.name+",我的工号是"+this.id+",我是"+this.department+"部门的");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
|
||||||
|
public class Fu {
|
||||||
|
int numFu = 10;
|
||||||
|
String name = "父类的name";
|
||||||
|
|
||||||
|
public Fu(){}
|
||||||
|
|
||||||
|
public Fu(int numFu){
|
||||||
|
this.numFu = numFu;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void methodFu(){
|
||||||
|
System.out.println("父类的methodFu方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
//定义经理Manager类继承Employee类,包含属性:职员,重写show方法,在父类实现基础之上增加展示职员信息。
|
||||||
|
public class Manager extends Employee{
|
||||||
|
//职员们
|
||||||
|
private ArrayList<Clerk> clerkList;
|
||||||
|
|
||||||
|
public Manager() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Manager(String name, int id, String department) {
|
||||||
|
super(name, id, department);
|
||||||
|
}
|
||||||
|
|
||||||
|
//对经理的独有属性进行操作
|
||||||
|
public ArrayList<Clerk> getClerkList() {
|
||||||
|
return clerkList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClerkList(ArrayList<Clerk> clerkList) {
|
||||||
|
this.clerkList = clerkList;
|
||||||
|
}
|
||||||
|
|
||||||
|
//重写show方法,在父类实现基础之上增加展示职员信息。
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void show() {
|
||||||
|
super.show();//沿袭父类功能
|
||||||
|
//展示自己的职员们
|
||||||
|
System.out.println("我的下属有:");
|
||||||
|
for (int i = 0; i < clerkList.size(); i++) {
|
||||||
|
Clerk clerk = clerkList.get(i);
|
||||||
|
System.out.println(clerk.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
编写步骤:
|
||||||
|
1. 定义员工Employee类。包含属性:姓名,工号,部门,成员方法show,方法内展示员工信息。
|
||||||
|
2. 定义经理Manager类继承Employee类,包含属性:职员,重写show方法,在父类实现基础之上增加展示职员信息。
|
||||||
|
3. 定义职员Clerk类继承Employee类,包含属性:经理,重写show方法,在父类实现基础之上增加展示经理信息。
|
||||||
|
*/
|
||||||
|
public class Test09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个经理
|
||||||
|
Manager manager = new Manager("张三", 1, "开发部");
|
||||||
|
//招3个员工
|
||||||
|
Clerk clerk1 = new Clerk("李四", 10, "开发部",manager);
|
||||||
|
Clerk clerk2 = new Clerk("王五", 11, "开发部",manager);
|
||||||
|
Clerk clerk3 = new Clerk("赵六", 12, "开发部",manager);
|
||||||
|
|
||||||
|
/*clerk1.setManager(manager);
|
||||||
|
clerk2.setManager(manager);
|
||||||
|
clerk3.setManager(manager);*/
|
||||||
|
|
||||||
|
//创建一个集合,保存当前3个员工,隶属于经理管理
|
||||||
|
ArrayList<Clerk> list = new ArrayList<>();
|
||||||
|
list.add(clerk1);
|
||||||
|
list.add(clerk2);
|
||||||
|
list.add(clerk3);
|
||||||
|
manager.setClerkList(list);
|
||||||
|
|
||||||
|
//经理展示下自己的下属
|
||||||
|
manager.show();
|
||||||
|
//职员介绍下自己的经理
|
||||||
|
clerk1.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.extends_this_super05;
|
||||||
|
|
||||||
|
public class Zi extends Fu{
|
||||||
|
int numZi = 20;
|
||||||
|
String name = "子类的name";
|
||||||
|
|
||||||
|
public Zi(){
|
||||||
|
//调用有参构造即可
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
public Zi(int numZi){
|
||||||
|
this(numZi, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Zi(int numZi ,String name){
|
||||||
|
super();
|
||||||
|
this.numZi = numZi;
|
||||||
|
if (name != null) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
System.out.println("必须要执行的初始化操作1");
|
||||||
|
System.out.println("必须要执行的初始化操作2");
|
||||||
|
System.out.println("必须要执行的初始化操作3");
|
||||||
|
}
|
||||||
|
public void show(){
|
||||||
|
System.out.println(this.name);
|
||||||
|
System.out.println(super.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void method() {
|
||||||
|
System.out.println(super.numFu);
|
||||||
|
super.methodFu();
|
||||||
|
int numZi = 30;
|
||||||
|
System.out.println(numZi);
|
||||||
|
System.out.println(this.numZi);
|
||||||
|
this.method1();//调用自己其他的成员方法
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void method1(){
|
||||||
|
System.out.println("子类的method1方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inmind.extends_vars02;
|
||||||
|
/*
|
||||||
|
.继承中成员变量的访问特点
|
||||||
|
1.直接通过对象去访问,看对象创建语句=左边的内容,是谁就优先使用谁的成员变量,如果没有则向上找
|
||||||
|
2.间接通过对象调用成员方法去访问,调用时,方法属于谁,就优先使用谁的成员变量,如果没有则向上找
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//直接创建子类对象
|
||||||
|
Zi zi = new Zi();
|
||||||
|
System.out.println(zi.num);
|
||||||
|
System.out.println(zi.numZi);
|
||||||
|
System.out.println(zi.numFu);
|
||||||
|
//调用子类的方法来访问属性变量
|
||||||
|
zi.methoZi();
|
||||||
|
zi.methodFu();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inmind.extends_vars02;
|
||||||
|
/*
|
||||||
|
区分子类方法中重名的三种变量
|
||||||
|
*/
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Zi zi = new Zi();
|
||||||
|
zi.method();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.extends_vars02;
|
||||||
|
|
||||||
|
public class Fu {
|
||||||
|
int num = 10;//成员变量
|
||||||
|
int numFu = 20;
|
||||||
|
|
||||||
|
public void methodFu(){
|
||||||
|
System.out.println(num);
|
||||||
|
//System.out.println(numZi);//注意:继承中属性和行为只能向上找
|
||||||
|
System.out.println(numFu);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.extends_vars02;
|
||||||
|
|
||||||
|
public class Zi extends Fu{
|
||||||
|
int num = 30;//成员变量
|
||||||
|
int numZi = 40;
|
||||||
|
|
||||||
|
public void methoZi(){
|
||||||
|
System.out.println(num);
|
||||||
|
System.out.println(numZi);
|
||||||
|
System.out.println(numFu);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void method(){
|
||||||
|
int num = 50;//局部变量
|
||||||
|
System.out.println(num);//50 就近原则
|
||||||
|
System.out.println(this.num);//30
|
||||||
|
System.out.println(super.num);//super表示父类,父类的成员变量
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.inmind.extends_implments06;
|
||||||
|
|
||||||
|
public abstract class Fu {
|
||||||
|
public abstract void methodFu();
|
||||||
|
// public abstract void method();
|
||||||
|
public void method(){
|
||||||
|
System.out.println("父类的method");
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.extends_implments06;
|
||||||
|
|
||||||
|
public interface MyInterface1 {
|
||||||
|
public abstract void method1();
|
||||||
|
void method();
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.extends_implments06;
|
||||||
|
|
||||||
|
public interface MyInterface2 {
|
||||||
|
public abstract void method2();
|
||||||
|
void method();
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.extends_implments06;
|
||||||
|
/*
|
||||||
|
单继承多实现的注意事项:
|
||||||
|
1.非抽象的子类实现类必须重写所有的抽象方法
|
||||||
|
2.如果有抽象方法未实现,那么子类实现类必须是抽象类
|
||||||
|
3.如果多个接口中有相同的抽象方法,那么非抽象的子类只需要实现一次即可
|
||||||
|
4.如果多个接口和抽象父类中有相同的抽象方法,那么非抽象的子类也只需要实现一次即可
|
||||||
|
5.如果抽象父类中拥有一个与多个接口中抽象方法,相同的普通方法,那么子类实现类,
|
||||||
|
就可以选择性地重写或不重写该抽象方法,类的方法优先于接口的
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Zi extends Fu implements MyInterface1,MyInterface2{
|
||||||
|
@Override
|
||||||
|
public void methodFu() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method1() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method() {
|
||||||
|
System.out.println("既实现了接口1的method又实现了接口2的method,也实现了抽象父类的method");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.inmind.interface_abstract01;
|
||||||
|
/*
|
||||||
|
接口就是生活中的规范,规则
|
||||||
|
定义格式
|
||||||
|
public interface 接口名 {
|
||||||
|
}
|
||||||
|
|
||||||
|
jdk7之前:抽象方法,常量
|
||||||
|
jdk8:默认方法,静态方法
|
||||||
|
jdk9:私有方法
|
||||||
|
-------------------------------
|
||||||
|
接口的抽象方法的使用步骤:
|
||||||
|
1.接口不能创建对象的,它没有构造方法
|
||||||
|
2.定义一个实现类,实现该接口,并实现接口中的所有的抽象方法
|
||||||
|
public class MyInterfaceImpl(实现类) implements MyInterface(接口){
|
||||||
|
3.创建该接口的实现类的对象,调用接口的方法
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建实现类对象,调用接口的抽象方法
|
||||||
|
MyInterfaceImpl myInterface = new MyInterfaceImpl();
|
||||||
|
myInterface.method();
|
||||||
|
myInterface.method1();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.interface_abstract01;
|
||||||
|
/*
|
||||||
|
接口中抽象方法定义:
|
||||||
|
public abstract 返回值类型 抽象方法名();
|
||||||
|
但凡在接口中定义一个没有方法体{}的方法,就是public abstract修饰;暂时不省略
|
||||||
|
;
|
||||||
|
*/
|
||||||
|
public interface MyInterface {
|
||||||
|
public abstract void method();
|
||||||
|
void method1();//编译器,前面会自动加上 public abstract
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind.interface_abstract01;
|
||||||
|
//一个接口的实现类
|
||||||
|
public class MyInterfaceImpl implements MyInterface {
|
||||||
|
@Override
|
||||||
|
public void method() {
|
||||||
|
System.out.println("实现类(子类)MyInterfaceImpl实现了抽象方法method");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method1() {
|
||||||
|
System.out.println("实现类(子类)MyInterfaceImpl实现了抽象方法method1");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inmind.interface_constant05;
|
||||||
|
/*
|
||||||
|
接口中的内容特点:
|
||||||
|
1.抽象方法(jdk7之前):制定规则,必须遵守的规范
|
||||||
|
2.默认方法(jdk8):优化解决接口升级的问题,选择性地沿用,或者扩展新功能
|
||||||
|
3.静态方法(jdk8):定义一些为接口本身相关的,通用的工具类方法
|
||||||
|
4.私有方法(jdk9): 对默认方法和静态方法,进行共有代码的抽取复用,且不让外界调用
|
||||||
|
5.常量:定义了公共的标准值,为接口的行为提供规范,配套的固定值
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(MyInterface.a);
|
||||||
|
int b = 10+MyInterface.a;
|
||||||
|
System.out.println(b);
|
||||||
|
|
||||||
|
System.out.println(MyInterface.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.interface_constant05;
|
||||||
|
/*
|
||||||
|
接口中定义"成员属性",其实是一个常量,编译器会在成员属性前,默认加上【public static final】!!!
|
||||||
|
final:最终,被它修饰的内容就不能更改
|
||||||
|
接口中的常量的作用:为接口的行为规范,提供配套的固定值
|
||||||
|
接口中的常量如何使用:接口名.常量名来调用
|
||||||
|
*/
|
||||||
|
public interface MyInterface {
|
||||||
|
int a = 10;
|
||||||
|
public static final String name = "张三";
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.inmind.interface_default02;
|
||||||
|
/*
|
||||||
|
JDK8时提供,接口的默认方法
|
||||||
|
|
||||||
|
接口的默认方法的使用步骤:
|
||||||
|
1.接口不能创建对象的,它没有构造方法
|
||||||
|
2.定义一个实现类,实现该接口,并实现接口中的所有的抽象方法
|
||||||
|
public class MyInterfaceImpl(实现类) implements MyInterface(接口){
|
||||||
|
3.创建该接口的实现类的对象,调用接口的默认方法
|
||||||
|
4.在接口的实现类中,可以根据对应的需求,选择性重写,或不重写接口中的默认方法,也可以沿用功能(接口.super.默认方法名)(重点)
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MyInterfaceImpl1 myInterfaceImpl1 = new MyInterfaceImpl1();
|
||||||
|
myInterfaceImpl1.method();
|
||||||
|
myInterfaceImpl1.method1();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.inmind.interface_default02;
|
||||||
|
/*
|
||||||
|
第一个版本的规则(接口),有一个固定功能myMethod
|
||||||
|
第二个版本中,需要增加功能
|
||||||
|
|
||||||
|
接口是可以升级的,添加新功能,之前添加功能只能通过抽象方法,导致所有的实现类都报错,这样非常麻烦。
|
||||||
|
为了解决该问题,jdk8提供了默认方法来解决
|
||||||
|
|
||||||
|
接口中的默认方法的定义格式:
|
||||||
|
public default 返回值类型 方法名(参数列表){
|
||||||
|
默认实现
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
public interface MyInterface {
|
||||||
|
//默认方法
|
||||||
|
public default void method(){
|
||||||
|
System.out.println("MyInterface接口的默认功能1");
|
||||||
|
}
|
||||||
|
|
||||||
|
public default void method1(){
|
||||||
|
System.out.println("MyInterface接口的默认功能2");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.interface_default02;
|
||||||
|
|
||||||
|
public class MyInterfaceImpl1 implements MyInterface{
|
||||||
|
//ctrl+o
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void method() {
|
||||||
|
//MyInterface.super.method();//沿用接口的默认功能
|
||||||
|
System.out.println("实现类MyInterfaceImpl1,重写了接口的method的默认功能");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.interface_extends07;
|
||||||
|
|
||||||
|
public interface MyInterface1 {
|
||||||
|
public static final int a = 10;//放入常量池
|
||||||
|
|
||||||
|
void method1();
|
||||||
|
void method();
|
||||||
|
|
||||||
|
public static void staticMethod1(){
|
||||||
|
System.out.println("接口1的静态方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.interface_extends07;
|
||||||
|
|
||||||
|
public interface MyInterface2 {
|
||||||
|
int b = 20;
|
||||||
|
|
||||||
|
void method2();
|
||||||
|
void method();
|
||||||
|
public static void staticMethod2(){
|
||||||
|
System.out.println("接口2的静态方法");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.inmind.interface_extends07;
|
||||||
|
/*
|
||||||
|
类与类:单继承
|
||||||
|
类与接口:多实现
|
||||||
|
接口与接口:多继承
|
||||||
|
|
||||||
|
类的继承:单继承,一个类永远只有一个父类
|
||||||
|
|
||||||
|
接口的继承:接口是多继承的,接口可以有多个直接的父接口
|
||||||
|
接口的多继承的注意事项:
|
||||||
|
1.如果是继承,子接口继承所有的父接口的抽象方法,子接口的实现类必须实现所有的抽象方法
|
||||||
|
2.如果是继承的父接口中有同名的抽象方法,子接口只会继承一个抽象方法,子接口的实现类也只需要实现一次同名的抽象方法
|
||||||
|
3.接口的静态方法不能通过对象调用
|
||||||
|
4.接口的静态方法只跟本接口相关,只能通过本接口名.静态方法来调用
|
||||||
|
5.常量也类似
|
||||||
|
*/
|
||||||
|
public interface MyInterfaceZi extends MyInterface1,MyInterface2 {
|
||||||
|
void methodZi();
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user