Compare commits
67 Commits
c4b089a048
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ae87d6d972 | |||
| b208689e11 | |||
| 890ad81fad | |||
| 2d28aa4c3e | |||
| c4394dc6b5 | |||
| 21914a5025 | |||
| 3956c0173d | |||
| aa4af28eec | |||
| 49c8f29d83 | |||
| 2ff6748e56 | |||
| c4210a98bd | |||
| 507b4ddfd7 | |||
| 55e81651f1 | |||
| bdf2351a89 | |||
| cdad66331c | |||
| 41617bd714 | |||
| 437c685222 | |||
| 1e4b3cd7a0 | |||
| b991c929a5 | |||
| a618bde74f | |||
| a0996aad04 | |||
| 1363911529 | |||
| 7a1e0e5143 | |||
| 0da36bf18a | |||
| 6390be44af | |||
| 0b33175663 | |||
| 9d8a20013c | |||
| 7ccae302ff | |||
| 2c7b3cc1e7 | |||
| a7a2d8c51c | |||
| ddca915d58 | |||
| 4c08e64e1f | |||
| db361a3082 | |||
| acc637a0ff | |||
| c55ffbfab1 | |||
| b5a8af3ffb | |||
| 9c41ddb63e | |||
| d3961b7e72 | |||
| 47f3d8425b | |||
| 5ecdbfd1cd | |||
| 730a921200 | |||
| a65790adce | |||
| e388c1b3be | |||
| 4eb64048bf | |||
| 6d5bd529c7 | |||
| 42c7e02de6 | |||
| 2ccb704e31 | |||
| b36603b570 | |||
| 76461a9b21 | |||
| 464b18be9d | |||
| b6bdce8620 | |||
| 7da8c2100f | |||
| 9388b14f53 | |||
| c39514417b | |||
| d65005955a | |||
| 55ad571736 | |||
| 9a9e6a53bf | |||
| d4c29e7cbd | |||
| 999c747834 | |||
| 6ea6c24b43 | |||
| 9a0527f635 | |||
| fd81c59ecb | |||
| 738603420a | |||
| 9faebbb087 | |||
| 5fd16ef048 | |||
| c5474d8399 | |||
| dc901e1fce |
65
day07/src/com/inmind/arraylist03/Demo07.java
Normal file
65
day07/src/com/inmind/arraylist03/Demo07.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
常用类_ArrayList_使用步骤
|
||||
1.导包java.util.ArrayList
|
||||
2.通过构造方法创建对象ArrayList()
|
||||
3.调用方法,add(E e)
|
||||
|
||||
arraylist的好处:1.长度可变 2.可以进行增删改查 3.arraylist可以存放任意引用数据类型
|
||||
|
||||
注意:ArrayList在使用的时候,大部分都是将一种类型的数据放在一个容器arraylist中
|
||||
|
||||
new GodNess()
|
||||
new ArrayList<String>():表示创建一个只存放String类型的数据的集合
|
||||
new ArrayList<GodNess>():表示创建一个只存放GodNess类型的数据的集合
|
||||
|
||||
泛型决定了集合中存放的数据类型!!!
|
||||
*/
|
||||
public class Demo07 {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
//创建arraylist对象
|
||||
/*ArrayList lists = new ArrayList();//如果不写泛型,默认容器可以添加object,任意引用数据类型都可以添加到容器中
|
||||
//往集合中添加内容
|
||||
lists.add(1);//此处添加的是引用数据类型,不是基本数据类型,后面再说
|
||||
lists.add(1.1);
|
||||
lists.add(true);
|
||||
lists.add("ddd");
|
||||
Student s = new Student();
|
||||
lists.add(s);*/
|
||||
//创建一个只能添加字符串的集合
|
||||
ArrayList<String> strLists = new ArrayList<>();
|
||||
strLists.add("张三");
|
||||
strLists.add("李四");
|
||||
strLists.add("王五");
|
||||
//保存是地址,打印的内容是 [张三, 李四, 王五],这里虽然输出了内容,是由于ArrayList类,重写了底层的功能实现(toString方法),在打印时不直接给地址,而是地址指向的内容
|
||||
System.out.println(strLists);
|
||||
|
||||
//创建一个只能添加学生的集合
|
||||
Student s1 = new Student();
|
||||
Student s2 = new Student();
|
||||
ArrayList<Student> studentList = new ArrayList<>();
|
||||
studentList.add(s1);
|
||||
studentList.add(s2);
|
||||
System.out.println(studentList);//保存是地址
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void method(String[] args) {
|
||||
//引用数据类型,作为数组的内容,比如String
|
||||
String[] arr1 = new String[3];
|
||||
String[] arr2 = new String[]{"hehe","heihei","haha"};
|
||||
String[] arr3 = {"hehe","heihei","haha"};
|
||||
|
||||
//当数组定义完毕,我们还想要添加更多的数据??
|
||||
// 能添加吗???不能,数组长度固定
|
||||
//能减少吗???不能,数组长度固定
|
||||
//为了解决以上的问题,有一个更好的容器,arraylist!!!
|
||||
//arraylist的好处:1.长度可变 2.可以进行增删改查 3.arraylist可以存放任意引用数据类型
|
||||
}
|
||||
}
|
||||
124
day07/src/com/inmind/arraylist03/Demo08.java
Normal file
124
day07/src/com/inmind/arraylist03/Demo08.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
常用类_ArrayList_其他常用方法说明
|
||||
对于元素的操作,基本体现在——增、删、修、查。
|
||||
常用的方法有
|
||||
添加:
|
||||
void add(int index, E element) 在此列表中的指定位置插入指定的元素。
|
||||
boolean add(E e) 将指定的元素追加到此列表的末尾。
|
||||
删除:
|
||||
E remove(int index) 删除该列表中指定位置的元素。
|
||||
boolean remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。
|
||||
修改:
|
||||
E set(int index, E element) 用指定的元素替换此列表中指定位置的元素。
|
||||
查询:
|
||||
E get(int index) 返回此列表中指定位置的元素。
|
||||
|
||||
集合的长度
|
||||
int size() 返回此列表中的元素数。
|
||||
*/
|
||||
public class Demo08 {
|
||||
//集合的修改
|
||||
public static void main(String[] args) {
|
||||
//创建一个保存字符串的集合
|
||||
ArrayList<String> lists = new ArrayList<>();
|
||||
lists.add("柳岩");
|
||||
lists.add("迪丽热巴");
|
||||
lists.add("王宝强");
|
||||
lists.add("杨幂");
|
||||
System.out.println(lists);
|
||||
/*
|
||||
修改:
|
||||
E set(int index, E element) 用指定的元素替换此列表中指定位置的元素。
|
||||
*/
|
||||
//请将王宝强修改为“王宝宝”
|
||||
String reVal = lists.set(2, "王宝宝");
|
||||
System.out.println(reVal);
|
||||
System.out.println(lists);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//集合的删除操作
|
||||
public static void remove(String[] args) {
|
||||
//创建一个保存字符串的集合
|
||||
ArrayList<String> lists = new ArrayList<>();
|
||||
|
||||
lists.add("柳岩");
|
||||
lists.add("迪丽热巴");
|
||||
lists.add("白鹿");
|
||||
lists.add("杨幂");
|
||||
System.out.println(lists);
|
||||
/*
|
||||
删除:
|
||||
E remove(int index) 删除该列表中指定位置的元素。
|
||||
boolean remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。
|
||||
*/
|
||||
//删除索引为1的数据
|
||||
String removeResult = lists.remove(1);
|
||||
System.out.println(removeResult);
|
||||
System.out.println(lists);
|
||||
System.out.println("----------------");
|
||||
//删除王宝强
|
||||
boolean isSuccess = lists.remove("王宝强");
|
||||
System.out.println(isSuccess);
|
||||
System.out.println(lists);
|
||||
System.out.println("----------------");
|
||||
//删除白鹿
|
||||
isSuccess = lists.remove("白鹿");
|
||||
System.out.println(isSuccess);
|
||||
System.out.println(lists);
|
||||
}
|
||||
|
||||
//集合的查询
|
||||
public static void get(String[] args) {
|
||||
//创建一个保存字符串的集合
|
||||
ArrayList<String> lists = new ArrayList<>();
|
||||
|
||||
lists.add("柳岩");
|
||||
lists.add("迪丽热巴");
|
||||
lists.add("白鹿");
|
||||
lists.add("杨幂");
|
||||
/*
|
||||
查询:
|
||||
E get(int index) 返回此列表中指定位置的元素
|
||||
*/
|
||||
String str1 = lists.get(0);
|
||||
System.out.println(str1);
|
||||
System.out.println(lists.get(1));
|
||||
System.out.println(lists.get(2));
|
||||
System.out.println(lists.get(3));
|
||||
System.out.println("-----------------------");
|
||||
//获取集合中每个元素,应该怎么办??for循环:集合的遍历
|
||||
for (int i = 0; i < lists.size(); i++) {
|
||||
System.out.println(lists.get(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//集合的添加
|
||||
public static void add(String[] args) {
|
||||
//创建一个保存字符串的集合
|
||||
ArrayList<String> lists = new ArrayList<>();
|
||||
/*
|
||||
添加:
|
||||
void add(int index, E element) 在此列表中的指定位置插入指定的元素。 插队
|
||||
boolean add(E e) 将指定的元素追加到此列表的末尾。 排队
|
||||
*/
|
||||
lists.add("柳岩");
|
||||
lists.add("迪丽热巴");
|
||||
lists.add("白鹿");
|
||||
lists.add("杨幂");
|
||||
System.out.println(lists);
|
||||
System.out.println(lists.size());
|
||||
//添加王宝强在白鹿之前
|
||||
lists.add(2, "王宝强");
|
||||
System.out.println(lists);
|
||||
System.out.println(lists.size());
|
||||
|
||||
}
|
||||
}
|
||||
31
day07/src/com/inmind/arraylist03/Demo09.java
Normal file
31
day07/src/com/inmind/arraylist03/Demo09.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
ArrayList_基本类型的存储方式_转换为包装类
|
||||
|
||||
自动装箱:int 型的值 -----装箱成Integer类型的对象
|
||||
自动拆箱:Integer类型的对象-----拆箱为int型的值
|
||||
4类8种的基本数据类型不能存放到集合中,因为集合只能存放引用数据类型
|
||||
|
||||
在java中使用包装类,来对基本数据类型进行包装,包装成引用数据类型
|
||||
|
||||
引用数据类型:数组 类 接口
|
||||
基本数据类型:int char boolean byte short long float double 四类八种
|
||||
*/
|
||||
public class Demo09 {
|
||||
public static void main(String[] args) {
|
||||
//定义一个集合保存int类型的数据
|
||||
ArrayList<Integer> lists = new ArrayList<>();
|
||||
lists.add(1);//自动装箱: 1 ----> new Integer(1)
|
||||
lists.add(2);
|
||||
lists.add(3);
|
||||
System.out.println(lists);
|
||||
|
||||
//获取集合中第二个值(包头不包尾,从0开始)
|
||||
Integer i = lists.get(1);
|
||||
int value = i+10;//自动拆箱:integer i ----> int 2
|
||||
System.out.println(value);
|
||||
}
|
||||
}
|
||||
21
day07/src/com/inmind/arraylist03/Demo10.java
Normal file
21
day07/src/com/inmind/arraylist03/Demo10.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
//常用类_ArrayList_练习1_添加小数double
|
||||
public class Demo10 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个存放小数的集合
|
||||
ArrayList<Double> lists = new ArrayList<>();
|
||||
//添加数据
|
||||
lists.add(1.0);
|
||||
lists.add(2.0);
|
||||
lists.add(3.0);
|
||||
System.out.println(lists);
|
||||
double d = 4;//自动类型提升
|
||||
lists.add(d);
|
||||
|
||||
//获取第4个值
|
||||
System.out.println(lists.get(3));//4.0
|
||||
}
|
||||
}
|
||||
28
day07/src/com/inmind/arraylist03/Demo11.java
Normal file
28
day07/src/com/inmind/arraylist03/Demo11.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
//常用类_ArrayList_练习2_添加对象
|
||||
public class Demo11 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个集合,保存3个学生对象
|
||||
ArrayList<Student> students = new ArrayList<>();
|
||||
//创建3个学生对象
|
||||
Student s1 = new Student("小王1", 18);
|
||||
Student s2 = new Student("小王2", 19);
|
||||
Student s3 = new Student("小王3", 17);
|
||||
|
||||
students.add(s1);
|
||||
students.add(s2);
|
||||
students.add(s3);
|
||||
System.out.println(students);
|
||||
|
||||
//遍历集合,将每个学生的信息输出
|
||||
for (int i = 0; i < students.size(); i++) {
|
||||
//获取每个元素
|
||||
Student s = students.get(i);
|
||||
System.out.println(s.getName() + "---" + s.getAge());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
37
day07/src/com/inmind/arraylist03/Demo12.java
Normal file
37
day07/src/com/inmind/arraylist03/Demo12.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
ArrayList_练习3_指定格式拼接字符串
|
||||
定义以指定格式打印集合的方法(ArrayList类型作为参数),使用@分隔每个元素。
|
||||
格式参照 [元素@元素@元素]。
|
||||
*/
|
||||
public class Demo12 {
|
||||
public static void main(String[] args) {
|
||||
//准备好数据,调用方法即可
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add("杨幂");
|
||||
list.add("白鹿");
|
||||
list.add("杨超越");
|
||||
printArrayList(list);
|
||||
}
|
||||
|
||||
//定义以指定格式打印集合的方法(ArrayList类型作为参数)
|
||||
public static void printArrayList(ArrayList<String> list) {
|
||||
String resutl = "[";
|
||||
//遍历集合,获取每个元素
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
//获取每个元素
|
||||
String s = list.get(i);
|
||||
//判断是否是最后一个元素(一个集合的最后一个索引值一定是集合.size() - 1)
|
||||
if (i == (list.size() - 1)) {
|
||||
resutl = resutl + s + "]";
|
||||
} else {
|
||||
resutl = resutl + s + "@";
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(resutl);
|
||||
}
|
||||
}
|
||||
66
day07/src/com/inmind/arraylist03/Student.java
Normal file
66
day07/src/com/inmind/arraylist03/Student.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
private int age = 18;
|
||||
private int id;
|
||||
private String gender;
|
||||
private double score;
|
||||
|
||||
|
||||
public Student(){
|
||||
//对所有的属性设置默认值
|
||||
}
|
||||
|
||||
public Student(String name){
|
||||
//针对指定的属性赋值,其他的属性默认
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Student(String name, int age){
|
||||
//针对指定的属性赋值,其他的属性默认
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(double score) {
|
||||
this.score = score;
|
||||
}
|
||||
}
|
||||
55
day07/src/com/inmind/arraylist03/Test13.java
Normal file
55
day07/src/com/inmind/arraylist03/Test13.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package com.inmind.arraylist03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
常用类_ArrayList_练习4_获取偶数集合
|
||||
有一个原本的集合,该集合中有正整数,1,3,4,5,8,9
|
||||
将集合中的偶数都取出来,保存,奇数不要
|
||||
*/
|
||||
public class Test13 {
|
||||
public static void main(String[] args) {
|
||||
//1.定义数据
|
||||
ArrayList<Integer> integers = new ArrayList<>();
|
||||
integers.add(2);
|
||||
integers.add(3);
|
||||
integers.add(3);
|
||||
integers.add(4);
|
||||
integers.add(8);
|
||||
integers.add(6);
|
||||
//2.调用方法,打印只有偶数的集合
|
||||
// getOushu(integers);
|
||||
getOushu1(integers);
|
||||
}
|
||||
|
||||
//实现方式二:直接将原集合中的奇数删除即可
|
||||
public static void getOushu1(ArrayList<Integer> lists) {
|
||||
//遍历集合,判断是奇数,就删除,remove
|
||||
for (int i = 0; i < lists.size(); i++) {
|
||||
Integer value = lists.get(i);
|
||||
if (value % 2 != 0) {//自动拆箱
|
||||
lists.remove(i);
|
||||
i--;//注意:但凡在集合遍历中删除,一定要加i--,避免跳过元素
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(lists);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//实现方式一:重新创建一个新的集合,遍历原集合,判断是偶数则添加到新集合
|
||||
public static void getOushu(ArrayList<Integer> lists) {
|
||||
ArrayList<Integer> newLists = new ArrayList<>();
|
||||
//遍历原集合,判断是偶数则保存到新集合
|
||||
for (int i = 0; i < lists.size(); i++) {
|
||||
Integer value = lists.get(i);
|
||||
if (value % 2 == 0) {//自动拆箱
|
||||
newLists.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(newLists);
|
||||
}
|
||||
}
|
||||
34
day07/src/com/inmind/random02/Demo05.java
Normal file
34
day07/src/com/inmind/random02/Demo05.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.inmind.random02;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/*
|
||||
random类获取随机数
|
||||
*/
|
||||
public class Demo05 {
|
||||
//random类的基本使用
|
||||
public static void method(String[] args) {
|
||||
//创建一个随机数生成器对象
|
||||
Random random = new Random();
|
||||
//调用random类的方法nextInt方法
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
System.out.println(random.nextInt(10));//0~9,java是包头不包尾的
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
//获取10个 【55~66,包含55和66】之间的随机值
|
||||
//55~66 - 55 ---> 0~11 ----->random.nextInt(12)
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
// int val = random.nextInt(12)+55;
|
||||
int val = random.nextInt(55, 67);//17以上才有
|
||||
System.out.println(val);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
42
day07/src/com/inmind/random02/Demo06.java
Normal file
42
day07/src/com/inmind/random02/Demo06.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.inmind.random02;
|
||||
/*
|
||||
常用类_Random_猜数字游戏(1~100)
|
||||
分析:
|
||||
1.使用random类获取一个随机值
|
||||
2.使用scanner输入你所猜的值
|
||||
a.将输入的猜的值跟随机数做比较,如果猜的值大了,需要提示猜的值:33大了
|
||||
b.将输入的猜的值跟随机数做比较,如果猜的值小了,需要提示猜的值:33小了
|
||||
c.将输入的猜的值跟随机数做比较,如果猜的值一致,需要提示正确,并结束游戏
|
||||
3.注意:如果用户猜不对,程序永远不结束(死循环)
|
||||
*/
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Demo06 {
|
||||
public static void main(String[] args) {
|
||||
//1.使用random类获取一个随机值
|
||||
int randomNum = new Random().nextInt(1, 101);
|
||||
System.out.println("猜数字游戏开始,您可以输入1~100的数字!!!");
|
||||
//2.使用scanner输入你所猜的值
|
||||
Scanner sc = new Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.println("请输入您所猜的值:");
|
||||
int guessNum = sc.nextInt();
|
||||
//使用if格式三:
|
||||
if (guessNum > randomNum) {
|
||||
//a.将输入的猜的值跟随机数做比较,如果猜的值大了,需要提示猜的值:33大了
|
||||
System.out.println("您猜的值:"+guessNum+"大了");
|
||||
} else if (guessNum < randomNum) {
|
||||
//b.将输入的猜的值跟随机数做比较,如果猜的值小了,需要提示猜的值:33小了
|
||||
System.out.println("您猜的值:" + guessNum + "小了");
|
||||
} else {
|
||||
//c.将输入的猜的值跟随机数做比较,如果猜的值一致,需要提示正确,并结束游戏
|
||||
//提示猜对了
|
||||
System.out.println("恭喜您,答对了!!");
|
||||
//结束游戏
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
day07/src/com/inmind/scanner01/Demo01.java
Normal file
34
day07/src/com/inmind/scanner01/Demo01.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.inmind.scanner01;
|
||||
//1.导包java.util.Scanner
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
该类学习Scanner类的使用
|
||||
步骤:
|
||||
1.导包java.util.Scanner
|
||||
2.通过构造方法创建出Scanner类的对象
|
||||
3.通过Scanner类中已经定义好的方法,通过对象来调用,获取键盘输入的内容
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
System.in:系统的输入方法,键盘输入
|
||||
System.out:系统的输出方法,在控制台输出
|
||||
*/
|
||||
//2.通过构造方法创建出Scanner类的对象
|
||||
Scanner sc = new Scanner(System.in);
|
||||
//3.通过Scanner类中已经定义好的方法,通过对象来调用,获取键盘输入的内容
|
||||
System.out.println("请输入一个数字:");
|
||||
int i = sc.nextInt();//nextInt():阻塞程序,等待用户输入内容
|
||||
System.out.println(i);
|
||||
double d = sc.nextDouble();
|
||||
System.out.println(d);
|
||||
sc.nextLine();//将之前的多余的回车获取一次
|
||||
String str = sc.nextLine();
|
||||
System.out.println(str);
|
||||
|
||||
System.out.println("程序结束");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
18
day07/src/com/inmind/scanner01/Demo02.java
Normal file
18
day07/src/com/inmind/scanner01/Demo02.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.inmind.scanner01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
//键盘录入两个整数求和
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
//接收2个整数
|
||||
System.out.println("请输入一个整数:");
|
||||
int a = sc.nextInt();
|
||||
System.out.println("请输入第二个整数:");
|
||||
int b = sc.nextInt();
|
||||
//求和
|
||||
System.out.println("和值:"+(a+b));
|
||||
}
|
||||
|
||||
}
|
||||
30
day07/src/com/inmind/scanner01/Demo03.java
Normal file
30
day07/src/com/inmind/scanner01/Demo03.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.inmind.scanner01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
//键盘录入三个整数求最大值(可以动态地创建数组,来接收输入的内容,使用for来简化重新的代码)
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
//创建Scanner对象
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("请您输入要比较大小的整数的个数:");
|
||||
|
||||
int num = sc.nextInt();//由用户决定
|
||||
//动态初始化创建长度为3的数组
|
||||
int[] arr = new int[num];
|
||||
//for循环遍历,获取用户输入的整数
|
||||
for (int i = 0; i < num; i++) {
|
||||
System.out.println("请输入第"+(i+1)+"个整数:");
|
||||
arr[i] = sc.nextInt();
|
||||
}
|
||||
//计算数组中哪个值最大
|
||||
int max = arr[0];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
if (arr[i] > max) {
|
||||
max = arr[i];
|
||||
}
|
||||
}
|
||||
System.out.println("您输入的最大值为:"+max);
|
||||
|
||||
}
|
||||
}
|
||||
23
day07/src/com/inmind/scanner01/Demo04.java
Normal file
23
day07/src/com/inmind/scanner01/Demo04.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.inmind.scanner01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
匿名对象:没有名字的对象
|
||||
匿名对象的使用场景:当一个对象只要被使用一次的时候,可以使用匿名对象来简单实现
|
||||
|
||||
new Scanner(System.in):就是匿名对象
|
||||
Scanner sc = new Scanner(System.in);就是有了对象名,可以重复操作
|
||||
*/
|
||||
public class Demo04 {
|
||||
public static void main(String[] args) {
|
||||
//获取一个整数即可
|
||||
/*Scanner sc = new Scanner(System.in);
|
||||
int i = sc.nextInt();*/
|
||||
|
||||
/*int i = new Scanner(System.in).nextInt();
|
||||
System.out.println(i);*/
|
||||
|
||||
System.out.println(new Scanner(System.in).nextInt());
|
||||
}
|
||||
}
|
||||
33
day08/src/com/inmind/arrays03/Demo14.java
Normal file
33
day08/src/com/inmind/arrays03/Demo14.java
Normal file
@@ -0,0 +1,33 @@
|
||||
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 Demo14 {
|
||||
//public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
|
||||
public static void method1(String[] args) {
|
||||
int[] arr1 = {1,2,3};
|
||||
System.out.println(arr1);//[I@4eec7777
|
||||
System.out.println(Arrays.toString(arr1));
|
||||
double[] arr2 = {1.0,2.0,3.0};
|
||||
System.out.println(arr2);//[D@3b07d329
|
||||
System.out.println(Arrays.toString(arr2));
|
||||
}
|
||||
|
||||
//public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {1, 22, 3, 4, 44, 66, 22, 31, 5};
|
||||
System.out.println(Arrays.toString(arr));
|
||||
Arrays.sort(arr);
|
||||
System.out.println("排序之后:");
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
||||
26
day08/src/com/inmind/arrays03/Demo15.java
Normal file
26
day08/src/com/inmind/arrays03/Demo15.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.arrays03;
|
||||
/*
|
||||
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 Demo15 {
|
||||
public static void main(String[] args) {
|
||||
//public static double abs(double a) :返回 double 值的绝对值。
|
||||
System.out.println(Math.abs(-2));//2
|
||||
System.out.println(Math.abs(-2.2));//2.2
|
||||
//public static double ceil(double a) :返回大于等于参数的最小的整数。(向上取整)
|
||||
System.out.println(Math.ceil(6.1));//7.0
|
||||
System.out.println(Math.ceil(-3.6));//-3.0
|
||||
//public static double floor(double a) :返回小于等于参数最大的整数。(向下取整)
|
||||
System.out.println(Math.floor(4.9));//4.0;
|
||||
System.out.println(Math.floor(-6.6));//-7.0
|
||||
//public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
|
||||
System.out.println(Math.round(3.4));//3
|
||||
System.out.println(Math.round(4.6));//5
|
||||
System.out.println(Math.round(-8.8));//-9
|
||||
|
||||
}
|
||||
}
|
||||
25
day08/src/com/inmind/arrays03/Test16.java
Normal file
25
day08/src/com/inmind/arrays03/Test16.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.inmind.math04;
|
||||
/*
|
||||
计算在 -10.8 到5.9 之间,绝对值大于6 或者小于2.1 的整数有多少个?
|
||||
分析:
|
||||
取值范围: -10.8 ~5.9
|
||||
条件>6||<2.1
|
||||
定义一个变量来计数
|
||||
*/
|
||||
public class Test16 {
|
||||
public static void main(String[] args) {
|
||||
double start = -10.8;
|
||||
double end = 5.9;
|
||||
int count = 0;
|
||||
|
||||
for (double i = Math.ceil(start); i < end ; i++) {
|
||||
System.out.print(i+" ");
|
||||
//条件>6||<2.1
|
||||
if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println(count);
|
||||
}
|
||||
}
|
||||
28
day08/src/com/inmind/static02/Demo08.java
Normal file
28
day08/src/com/inmind/static02/Demo08.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.inmind.static02;
|
||||
/*
|
||||
static关键字的使用:
|
||||
可以修饰成员变量和成员方法。
|
||||
1.使用static修饰成员变量的使用方式:只跟类有关,与对象无关了,并且该类的每个对象都共享了该变量的值
|
||||
静态成员变量的使用方式:
|
||||
a.类名.静态变量名(推荐使用)
|
||||
b.对象名.静态变量名(不推荐使用)
|
||||
*/
|
||||
public class Demo08 {
|
||||
public static void main(String[] args) {
|
||||
//创建2个学生
|
||||
Student s1 = new Student("张三", 18);
|
||||
Student s2 = new Student("李四", 19);
|
||||
//将教室设置为1903室
|
||||
Student.clazz = "1903室";
|
||||
//遇到学生s1,询问,你的教室是哪里
|
||||
// System.out.println(s1.name + "的教室是" + s1.clazz);
|
||||
System.out.println(s1.name + "的教室是" + Student.clazz);
|
||||
//告诉学生s1,你的教室改为1905室
|
||||
Student.clazz = "1905室";
|
||||
|
||||
//遇到学生s2,询问,你的教室是哪里
|
||||
System.out.println(s2.name + "的教室是" + Student.clazz);
|
||||
|
||||
//注意:静态内容被所有对象共享。
|
||||
}
|
||||
}
|
||||
35
day08/src/com/inmind/static02/Demo09.java
Normal file
35
day08/src/com/inmind/static02/Demo09.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.inmind.static02;
|
||||
/*
|
||||
static关键字的使用:
|
||||
2.使用static修饰成员方法的使用方式:只跟类有关,与对象无关了,并且该类的每个对象都可以直接调用静态方法
|
||||
区分
|
||||
a.自定义成员方法:(对象方法),它的定义是给每个对象来调用,必须通过创建对象来调用
|
||||
b 静态方法:(类方法),它的定义是给对应的类直接调用,跟每个对象无关的操作
|
||||
静态方法的使用方式:
|
||||
a.类名.静态方法名(参数列表)(推荐使用)
|
||||
b.对象名.静态方法名(参数列表)(不推荐使用)
|
||||
|
||||
静态方法的作用:静态方法调用的比较方便,不需要创建对象,常用于对应的工具类的抽取,在一个项目中有可能有特定的功能代码,到处都要用,抽取成静态方法,直接通过类名.静态方法名,直接调用功能即可。
|
||||
--------------------------------------------------------------------------------------
|
||||
学习静态的注意事项:
|
||||
静态方法调用的注意事项:
|
||||
1.静态方法可以直接访问类变量(静态变量)和静态方法。
|
||||
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,反之,成员方法可以直接访问静态变量或静态方法。(先人(静态内容)不知道后人(对象),后人(对象)是知道先人(静态内容)的)
|
||||
3.静态方法中,不能使用this关键字。
|
||||
|
||||
static 修饰的内容:
|
||||
是随着类的加载而加载的,且只加载一次。(只跟类有关)
|
||||
存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。
|
||||
它优先于对象存在,所以,可以被所有对象共享。
|
||||
*/
|
||||
public class Demo09 {
|
||||
public static void main(String[] args) {
|
||||
Student s1 = new Student("张三", 18);
|
||||
Student s2 = new Student("李四", 19);
|
||||
Student.clazz = "1903室";
|
||||
//调用静态方法
|
||||
Student.show();
|
||||
s1.show();
|
||||
s2.show();
|
||||
}
|
||||
}
|
||||
30
day08/src/com/inmind/static02/Demo10.java
Normal file
30
day08/src/com/inmind/static02/Demo10.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.inmind.static02;
|
||||
/*
|
||||
静态代码块:
|
||||
定义在成员位置,使用static修饰的代码块{ }。
|
||||
位置:类中方法外。
|
||||
执行:随着类的加载而执行且执行一次,优先于main方法和构造方法的执行。
|
||||
|
||||
格式:
|
||||
static{
|
||||
java语句;
|
||||
}
|
||||
|
||||
静态代码块的作用:在程序运行之前,就进行一些操作,常用于对静态变量的初始化操作,后期工作中常用于对配置文件的值赋值操作
|
||||
*/
|
||||
public class Demo10 {
|
||||
static int i = 10;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("main方法");
|
||||
Student s = new Student();
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
|
||||
//静态代码块
|
||||
static{
|
||||
i = 20;
|
||||
System.out.println("静态代码块执行了");
|
||||
}
|
||||
}
|
||||
48
day08/src/com/inmind/static02/Student.java
Normal file
48
day08/src/com/inmind/static02/Student.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.inmind.static02;
|
||||
|
||||
public class Student {
|
||||
//属性(成员变量)
|
||||
String name;
|
||||
int age;
|
||||
//静态变量
|
||||
static String clazz;
|
||||
public Student() {
|
||||
System.out.println("Student无参构造被调用");
|
||||
}
|
||||
public Student(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
//静态方法,只跟类有关
|
||||
public static void show(){
|
||||
System.out.println("学生的教室为:"+clazz);
|
||||
//注意:静态方法中不能访问非静态的内容
|
||||
/*System.out.println("学生的姓名为:"+name);
|
||||
System.out.println("学生的年龄为:"+age);
|
||||
getName();*/
|
||||
//注意:在静态方法,无法使用this关键字
|
||||
// System.out.println(this);
|
||||
}
|
||||
|
||||
//成员方法(对象方法),只跟对象有关
|
||||
public String getName() {
|
||||
//注意:在非静态方法(成员方法)中可以访问静态内容
|
||||
System.out.println(clazz);
|
||||
show();
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
26
day08/src/com/inmind/string01/Demo03.java
Normal file
26
day08/src/com/inmind/string01/Demo03.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
常用类_String的比较的方法_equals_equalsIgnoreCase
|
||||
|
||||
String(引用数据类型)的比较:
|
||||
==:表示比较String对象的地址,其实没有任何意义,==一般用于基本数据类型的值是否相等比较
|
||||
equals:表示比较String对象地址所指向的内容
|
||||
|
||||
比较的方法:
|
||||
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
|
||||
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小
|
||||
写。
|
||||
*/
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
String s1 = "hello";
|
||||
String s2 = new String("hello");
|
||||
System.out.println(s1 == s2);//比较地址 false
|
||||
System.out.println(s1);
|
||||
System.out.println(s2);
|
||||
System.out.println(s1.equals(s2));//比较内容 true
|
||||
System.out.println("-----------------------");
|
||||
String s3 = "HeLlo";
|
||||
System.out.println(s1.equalsIgnoreCase(s2));//true
|
||||
}
|
||||
}
|
||||
62
day08/src/com/inmind/string01/Demo04.java
Normal file
62
day08/src/com/inmind/string01/Demo04.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
常用类_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 int indexOf(String str, int fromIndex)
|
||||
|
||||
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) {
|
||||
//public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
|
||||
String str = "helloworldjava";
|
||||
String subStr1 = str.substring(5);
|
||||
System.out.println(subStr1);
|
||||
//public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex
|
||||
//注意:java一定是包头不包尾
|
||||
//获取world
|
||||
System.out.println(str.substring(5, 10));
|
||||
|
||||
}
|
||||
|
||||
public static void method4(String[] args) {
|
||||
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||||
String str = "123java321java 123";
|
||||
//查询"java"出现的索引
|
||||
int index = str.indexOf("java");
|
||||
System.out.println(index);//3
|
||||
//查询“sql”出现的索引
|
||||
System.out.println(str.indexOf("sql"));//-1
|
||||
//查询"java"第二次出现的索引
|
||||
//public int indexOf(String str, int fromIndex)
|
||||
System.out.println(str.indexOf("java", index + 1));//10
|
||||
}
|
||||
|
||||
public static void method3(String[] args) {
|
||||
//public char charAt (int index) :返回指定索引处的 char值。
|
||||
String str = "helloworld";
|
||||
char c = str.charAt(5);
|
||||
System.out.println(c);//w
|
||||
|
||||
}
|
||||
public static void method2(String[] args) {
|
||||
//public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||||
String str = "hello";
|
||||
String s = str.concat("world");
|
||||
System.out.println(s);//helloworld
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void method1(String[] args) {
|
||||
//public int length () :返回此字符串的长度。常用于遍历,或者字符数据的长度
|
||||
String str = "hello";
|
||||
System.out.println(str.length());//5
|
||||
}
|
||||
}
|
||||
39
day08/src/com/inmind/string01/Demo06.java
Normal file
39
day08/src/com/inmind/string01/Demo06.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
常用类_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 str = "今天,天气很好,我很开心";
|
||||
//要求:将所有的“天”,替换为“*”
|
||||
String replaceStr = str.replace("天", "***");
|
||||
System.out.println(replaceStr);
|
||||
}
|
||||
|
||||
public static void method2(String[] args) {
|
||||
//作用:将字符串转为字节数组数据
|
||||
//public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
|
||||
String s = "123";
|
||||
byte[] bytes = s.getBytes();
|
||||
System.out.println(bytes);
|
||||
//只能遍历数组,一一打印
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
byte b = bytes[i];
|
||||
System.out.println(b);
|
||||
}
|
||||
}
|
||||
|
||||
public static void method1(String[] args) {
|
||||
//public char[] toCharArray () :将此字符串转换为新的字符数组。
|
||||
String s = "hello";
|
||||
//作用:字符串转为字符数组
|
||||
char[] chars = s.toCharArray();
|
||||
System.out.println(chars);//注意:chars保存的是地址,打印方法,底层实现,将该地址的内容
|
||||
}
|
||||
}
|
||||
19
day08/src/com/inmind/string01/Demo07.java
Normal file
19
day08/src/com/inmind/string01/Demo07.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
常用类_String的分割方法_split
|
||||
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组
|
||||
*/
|
||||
public class Demo07 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
String str = "张三,18,28,上海";
|
||||
//按,分割,获取对应的数据。注意:按什么分割,该字符串就不会出现
|
||||
String[] strs = str.split(",");
|
||||
System.out.println(strs.length);//4
|
||||
//将一段字符串转为学生对象
|
||||
Student s = new Student(strs[0], Integer.parseInt(strs[2]));
|
||||
System.out.println(s.getName());
|
||||
System.out.println(s.getAge());
|
||||
|
||||
}
|
||||
}
|
||||
26
day08/src/com/inmind/string01/StringDemo01.java
Normal file
26
day08/src/com/inmind/string01/StringDemo01.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
常用类_String的概述和特点
|
||||
1.String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
|
||||
""双引号字符串就是String类的对象,所以就可以直接调用String类的方法
|
||||
2.字符串不变; 它们的值在创建后不能被更改。 因为String对象是不可变的,它们可以被共享。
|
||||
|
||||
*/
|
||||
public class StringDemo01 {
|
||||
public static void main(String[] args) {
|
||||
//创建2个学生对象
|
||||
Student s1 = new Student("小王", 18);
|
||||
Student s2 = new Student("小王", 18);
|
||||
System.out.println(s1);
|
||||
System.out.println(s2);
|
||||
System.out.println(s1 == s2);//false 注意:引用数据类型中保存的是地址,当前地址不同
|
||||
|
||||
//定义2个字符串对象
|
||||
String str1 = "abc";//str1中保存是地址,所有引用数据类型,保存的永远是地址!!!
|
||||
System.out.println(str1);//这里由于String类的底层源码重写了toString方法,所以打印的是对应地址指向的内容
|
||||
|
||||
String str2 = "abc";
|
||||
System.out.println(str1 == str2);//true
|
||||
|
||||
}
|
||||
}
|
||||
44
day08/src/com/inmind/string01/StringDemo02.java
Normal file
44
day08/src/com/inmind/string01/StringDemo02.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
String类的构造方法:创建String类的对象
|
||||
|
||||
String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
|
||||
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
|
||||
*/
|
||||
public class StringDemo02 {
|
||||
public static void main(String[] args) {
|
||||
//public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
|
||||
//定义一个字节数组
|
||||
byte[] bytes = {97, 98, 99};// 97 -- 'a'
|
||||
//创建String对象
|
||||
//将字节数组的内容拼接成新的字符串(根据编码方式,将对应的十进制的值编码成字符串内容)
|
||||
//使用场景:javase,io流知识点中的字节流中来使用
|
||||
String s = new String(bytes);
|
||||
System.out.println(s);//abc
|
||||
}
|
||||
|
||||
public static void method2() {
|
||||
//public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||
//定义一个字符数组
|
||||
char[] chars = {'a', 'b', 'c'};//静态初始化简写形式
|
||||
//创建String对象
|
||||
// String s = new String(chars);
|
||||
String s = new String(chars,1,2);
|
||||
System.out.println(s);//abc
|
||||
|
||||
}
|
||||
public static void metdhod1() {
|
||||
/*
|
||||
String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列;
|
||||
换句话说,新创建的字符串是参数字符串的副本
|
||||
*/
|
||||
String s1 = "abc";
|
||||
//通过构造方法创建对象
|
||||
String s2 = new String(s1);
|
||||
System.out.println(s1);
|
||||
System.out.println(s2);
|
||||
System.out.println(s1 == s2);//false,因为s2使用了new在堆中开辟了新空间,虽然指向同一个内容,但是s1和s2的引用地址不同
|
||||
//总结:字符串String 如果要比较内容的话,一定不能使用 == 号,因为它比较的是地址
|
||||
}
|
||||
}
|
||||
66
day08/src/com/inmind/string01/Student.java
Normal file
66
day08/src/com/inmind/string01/Student.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.inmind.string01;
|
||||
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
private int age = 18;
|
||||
private int id;
|
||||
private String gender;
|
||||
private double score;
|
||||
|
||||
|
||||
public Student(){
|
||||
//对所有的属性设置默认值
|
||||
}
|
||||
|
||||
public Student(String name){
|
||||
//针对指定的属性赋值,其他的属性默认
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Student(String name, int age){
|
||||
//针对指定的属性赋值,其他的属性默认
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(double score) {
|
||||
this.score = score;
|
||||
}
|
||||
}
|
||||
36
day08/src/com/inmind/string01/Test05.java
Normal file
36
day08/src/com/inmind/string01/Test05.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.inmind.string01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
字符串查找。
|
||||
* 键盘录入一个大字符串,再录入一个小字符串。
|
||||
* 统计小字符串在大字符串中出现的次数。
|
||||
*/
|
||||
public class Test05 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("请输入一个长字符串:");
|
||||
String str = sc.nextLine();
|
||||
System.out.println("请输入一个要查询的字符串:");
|
||||
String subStr = sc.nextLine();
|
||||
|
||||
//调用统计次数的方法
|
||||
int count = getCount(str, subStr);
|
||||
System.out.println("出现的次数是:" + count);
|
||||
|
||||
}
|
||||
|
||||
//定义一个方法统计短字符串出现的次数(java123java java)
|
||||
public static int getCount(String str, String subStr){
|
||||
int count = 0;//统计次数
|
||||
//循环查找indexOf, 如果返回-1,则说明没有找到,结束循环
|
||||
//public int indexOf (String str)
|
||||
int index = 0;//记录每次查找内容的索引,便于下一次查找(index+1)
|
||||
while ((index = str.indexOf(subStr, index)) != -1) {
|
||||
count++;
|
||||
index++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
17
day09/src/com/inmind/abstract05/Animal.java
Normal file
17
day09/src/com/inmind/abstract05/Animal.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.inmind.abstract05;
|
||||
/*
|
||||
抽象的概念:
|
||||
1.当一个方法体无法实现时,则取消方法体,定义为抽象方法
|
||||
2.一个类中如果有抽象方法,则这个类必须声明为抽象类
|
||||
*/
|
||||
public abstract class Animal {
|
||||
|
||||
public Animal(){
|
||||
System.out.println("抽象类Animal的构造方法");
|
||||
}
|
||||
|
||||
|
||||
//动物吃的方法
|
||||
public abstract void eat();
|
||||
|
||||
}
|
||||
10
day09/src/com/inmind/abstract05/Cat.java
Normal file
10
day09/src/com/inmind/abstract05/Cat.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.inmind.abstract05;
|
||||
|
||||
public class Cat extends Animal{
|
||||
|
||||
|
||||
@Override
|
||||
public void eat() {
|
||||
System.out.println("猫吃鱼");
|
||||
}
|
||||
}
|
||||
26
day09/src/com/inmind/abstract05/Demo06.java
Normal file
26
day09/src/com/inmind/abstract05/Demo06.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.abstract05;
|
||||
/*
|
||||
如何使用抽象类和抽象方法???
|
||||
1.抽象类不能被创建对象的,只能通过子类创建对象
|
||||
2.非抽象的子类,必须实现重写抽象类中的抽象方法
|
||||
3.创建子类对象,调用对应方法
|
||||
|
||||
注意事项:
|
||||
1. 抽象类不能创建对象,如果创建,编译无法通过而报错。只能创建其非抽象子类的对象
|
||||
2. 抽象类中,可以有构造方法,是供子类创建对象时,初始化父类成员使用的
|
||||
3. 抽象类中,不一定包含抽象方法,但是有抽象方法的类必定是抽象类
|
||||
4. 抽象类的子类,必须重写抽象父类中所有的抽象方法,否则,编译无法通过而报错。除非该子类也是抽象类。
|
||||
|
||||
抽象类的意义是对某一些功能添加强制约束。
|
||||
基于重写来实现的,我们已经实现了自己该有的功能或,在此基础上,可以选择性地扩展功能
|
||||
|
||||
*/
|
||||
public class Demo06 {
|
||||
public static void main(String[] args) {
|
||||
//Animal animal = new Animal();
|
||||
Dog dog = new Dog();
|
||||
dog.eat();
|
||||
Cat cat = new Cat();
|
||||
cat.eat();
|
||||
}
|
||||
}
|
||||
14
day09/src/com/inmind/abstract05/Dog.java
Normal file
14
day09/src/com/inmind/abstract05/Dog.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.inmind.abstract05;
|
||||
|
||||
public class Dog extends Animal{
|
||||
|
||||
//编译器主动添加默认无参构造
|
||||
public Dog(){
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eat(){
|
||||
System.out.println("狗吃骨头");
|
||||
};
|
||||
}
|
||||
19
day09/src/com/inmind/abstract05/test/Circle.java
Normal file
19
day09/src/com/inmind/abstract05/test/Circle.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.abstract05.test;
|
||||
|
||||
public class Circle extends Shape{
|
||||
|
||||
//强制要求创建圆形的时候,必定要给半径,才能创建圆形对象
|
||||
public Circle(int r){
|
||||
super(r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return Math.PI*getR()*getR();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getZC() {
|
||||
return 2*Math.PI*getR();
|
||||
}
|
||||
}
|
||||
22
day09/src/com/inmind/abstract05/test/Rectangle.java
Normal file
22
day09/src/com/inmind/abstract05/test/Rectangle.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.inmind.abstract05.test;
|
||||
|
||||
public class Rectangle extends Shape{
|
||||
|
||||
|
||||
//强制要求创建矩形的时候,必定要给我一长宽,才能创建矩形对象
|
||||
public Rectangle(int chang ,int kuan){
|
||||
super(chang,kuan);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
// return super.getChang()*super.getKuan();
|
||||
return getChang()*getKuan();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getZC() {
|
||||
return 2*(getChang()+getKuan());
|
||||
}
|
||||
}
|
||||
57
day09/src/com/inmind/abstract05/test/Shape.java
Normal file
57
day09/src/com/inmind/abstract05/test/Shape.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.inmind.abstract05.test;
|
||||
|
||||
public abstract class Shape {
|
||||
//定义共性的属性
|
||||
private int chang; //长
|
||||
private int kuan;//宽
|
||||
private int r;//半径
|
||||
|
||||
//满参构造方法
|
||||
public Shape(int chang, int kuan, int r) {
|
||||
this.chang = chang;
|
||||
this.kuan = kuan;
|
||||
this.r = r;
|
||||
|
||||
//初始化操作
|
||||
}
|
||||
|
||||
public Shape(int chang, int kuan) {
|
||||
/*this.chang = chang;
|
||||
this.kuan = kuan;*/
|
||||
this(chang,kuan,0);
|
||||
}
|
||||
|
||||
public Shape(int r) {
|
||||
// this.r = r;
|
||||
this(0, 0, r);
|
||||
}
|
||||
|
||||
|
||||
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 double getArea();
|
||||
public abstract double getZC();
|
||||
}
|
||||
9
day09/src/com/inmind/abstract05/test/Square.java
Normal file
9
day09/src/com/inmind/abstract05/test/Square.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.inmind.abstract05.test;
|
||||
|
||||
public class Square extends Rectangle{
|
||||
|
||||
//强制要求创建正方形的时候,必定要给我一个边长,才能创建正方形对象
|
||||
public Square(int bc){
|
||||
super(bc,bc);
|
||||
}
|
||||
}
|
||||
31
day09/src/com/inmind/abstract05/test/Test07.java
Normal file
31
day09/src/com/inmind/abstract05/test/Test07.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.inmind.abstract05.test;
|
||||
/*
|
||||
- 定义形状抽象类Shape,矩形Rectangle和圆形Circle继承Shape类。
|
||||
- 圆形只能通过指定半径的方式,创建Circle对象。
|
||||
- 矩形只能通过指定长,宽的方法,创建Rectangle对象。
|
||||
|
||||
并且计算出各自的面积和周长
|
||||
|
||||
1.抽象类Shape
|
||||
2.图形至少有2个功能,抽象方法:计算面积和周长
|
||||
3.定义具体的图形:圆形Circle,矩形Rectangle
|
||||
4.创建子类对象,调用构造方法(传入参数:半径 ,长,宽 这些属性抽取到父类中,子类直接使用)
|
||||
|
||||
*/
|
||||
public class Test07 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个矩形对象
|
||||
Rectangle rectangle = new Rectangle(4, 2);
|
||||
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(2);
|
||||
System.out.println("正方形的周长是:"+square.getZC());
|
||||
System.out.println("正方形的面积是:"+square.getArea());
|
||||
}
|
||||
}
|
||||
8
day09/src/com/inmind/extends01/Assistant.java
Normal file
8
day09/src/com/inmind/extends01/Assistant.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.extends01;
|
||||
|
||||
public class Assistant extends Employee{
|
||||
|
||||
public void help() {
|
||||
System.out.println("在生活上帮助学生");
|
||||
}
|
||||
}
|
||||
8
day09/src/com/inmind/extends01/BanZhuRen.java
Normal file
8
day09/src/com/inmind/extends01/BanZhuRen.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.extends01;
|
||||
|
||||
public class BanZhuRen extends Employee {
|
||||
|
||||
public void manage() {
|
||||
System.out.println("在学校管理学生");
|
||||
}
|
||||
}
|
||||
26
day09/src/com/inmind/extends01/Demo01.java
Normal file
26
day09/src/com/inmind/extends01/Demo01.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.inmind.extends01;
|
||||
/*
|
||||
继承的格式和和基本使用
|
||||
public class Fu{
|
||||
}
|
||||
public class Zi extends Fu{
|
||||
}
|
||||
继承的特点:子类拥有父类的所有内容,除了被private和构造方法。
|
||||
继承的好处:实现多个类中相同内容的复用,子类可以实现各自的不同功能需求!!!!
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个班主任对象
|
||||
BanZhuRen bzr = new BanZhuRen();
|
||||
bzr.age = 20;
|
||||
bzr.name = "张三";
|
||||
bzr.salary = 5000.0;
|
||||
bzr.eat();
|
||||
|
||||
Teacher teacher = new Teacher();
|
||||
teacher.age = 20;
|
||||
teacher.name = "张三";
|
||||
teacher.salary = 5000.0;
|
||||
teacher.eat();
|
||||
}
|
||||
}
|
||||
11
day09/src/com/inmind/extends01/Employee.java
Normal file
11
day09/src/com/inmind/extends01/Employee.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.inmind.extends01;
|
||||
|
||||
public class Employee {
|
||||
String name;
|
||||
int age;
|
||||
double salary;
|
||||
|
||||
public void eat(){
|
||||
System.out.println("要吃饭");
|
||||
}
|
||||
}
|
||||
9
day09/src/com/inmind/extends01/Teacher.java
Normal file
9
day09/src/com/inmind/extends01/Teacher.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.inmind.extends01;
|
||||
|
||||
public class Teacher extends Employee{
|
||||
|
||||
|
||||
public void teach() {
|
||||
System.out.println("老师在上课了");
|
||||
}
|
||||
}
|
||||
24
day09/src/com/inmind/extends_constructor04/Demo05.java
Normal file
24
day09/src/com/inmind/extends_constructor04/Demo05.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.inmind.extends_constructor04;
|
||||
/*
|
||||
继承中构造方法的访问特点
|
||||
1.创建子类对象时,一定会调用父类的构造方法
|
||||
2.super调用父类的构造方法时,必须在子类的构造方法的第一行
|
||||
3.super可以调用父类的有参构造方法。
|
||||
*/
|
||||
public class Demo05 {
|
||||
public static void main(String[] args) {
|
||||
//创建子类对象
|
||||
Zi zi = new Zi();
|
||||
//创建父类对象
|
||||
Fu fu = new Fu(10);
|
||||
System.out.println("--------------------------------");
|
||||
zi.methodZi();
|
||||
System.out.println("--------------------------------");
|
||||
zi.method1(10);
|
||||
System.out.println("--------------------------------");
|
||||
zi.method2();
|
||||
|
||||
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
}
|
||||
18
day09/src/com/inmind/extends_constructor04/Fu.java
Normal file
18
day09/src/com/inmind/extends_constructor04/Fu.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.inmind.extends_constructor04;
|
||||
|
||||
public class Fu {
|
||||
int num = 100;
|
||||
//父类的无参构造方法
|
||||
public Fu(){
|
||||
System.out.println("父类无参构造方法");
|
||||
}
|
||||
//父类的有参构造方法
|
||||
public Fu(int num){
|
||||
System.out.println("父类有参构造方法");
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
public void methodFu() {
|
||||
System.out.println("父类的成员方法执行了");
|
||||
}
|
||||
}
|
||||
74
day09/src/com/inmind/extends_constructor04/Zi.java
Normal file
74
day09/src/com/inmind/extends_constructor04/Zi.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.inmind.extends_constructor04;
|
||||
|
||||
|
||||
import com.inmind.extends01.Employee;
|
||||
|
||||
/*
|
||||
super可以表示父类的引用(对象)
|
||||
super三个使用操作:
|
||||
1.super,可以在子类的成员方法中,访问父类的成员变量,格式super.父类成员变量
|
||||
2.super,可以在子类的成员方法中,访问父类的成员方法,格式:super.父类成员方法名(参数列表)
|
||||
3.super,可以在子类的构造方法中,访问父类的构造方法,格式:super(参数列表)
|
||||
-------------------------------------------------------------------
|
||||
谁调用成员方法,方法中的this就是谁(对象)
|
||||
this表示本类,super表示父类
|
||||
|
||||
this关键字使用方式
|
||||
1.this,可以区分当前类中局部变量和成员变量
|
||||
2.this,可以在本类的成员方法中,调用该类的其他的成员方法(其实在一个类中所有的成员变量和成员方法的调用之前都有this.)
|
||||
3.this,可以在本类的构造方法中,调用其他的重载构造方法,格式为 this(参数列表)
|
||||
|
||||
关于第三点的注意事项:
|
||||
a.this必须在第一行,不能调用自己构造方法(自己不能调用自己)
|
||||
b.this和super在调用构造方法时,都必须在第一行,所以在一个构造方法中不能同时出现
|
||||
c.this可以调用其他的重载构造方法,但是不能互相调用(死循环)
|
||||
|
||||
*/
|
||||
public class Zi extends Fu {//子类继承了父类,拥有了父类的非私有的成员
|
||||
int num = 50;
|
||||
int num1;
|
||||
|
||||
//无参构造方法
|
||||
public Zi(){
|
||||
//super(10);//注意:如果不写该行代码,编译器会自动添加super(),调用父类的无参构造,并且必须在第一行
|
||||
this(0);
|
||||
}
|
||||
//有参构造方法
|
||||
public Zi(int num){
|
||||
this(num, 0);
|
||||
}
|
||||
public Zi(int num,int num1){
|
||||
this.num = num;
|
||||
this.num1 = num1;
|
||||
System.out.println("子类的有参参构造方法");
|
||||
|
||||
System.out.println("初始化操作1");
|
||||
System.out.println("初始化操作2");
|
||||
System.out.println("初始化操作3");
|
||||
System.out.println("初始化操作4");
|
||||
System.out.println("初始化操作5");
|
||||
}
|
||||
|
||||
|
||||
//子类的成员方法
|
||||
public void methodZi(){
|
||||
System.out.println("父类的成员变量num:"+super.num);
|
||||
//调用父类的成员方法
|
||||
super.methodFu();
|
||||
}
|
||||
|
||||
|
||||
//子类的成员方法
|
||||
public void method1(int num){//形参也是一个局部变量
|
||||
//区分本类的同名的num
|
||||
System.out.println(num);//局部变量
|
||||
System.out.println(this.num);//本类的成员变量
|
||||
System.out.println(super.num);//父类的成员变量
|
||||
}
|
||||
|
||||
public void method2(){
|
||||
//调用本类的成员方法
|
||||
method1(10);
|
||||
this.method1(10);
|
||||
}
|
||||
}
|
||||
21
day09/src/com/inmind/extends_member02/Demo02.java
Normal file
21
day09/src/com/inmind/extends_member02/Demo02.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.inmind.extends_member02;
|
||||
/*
|
||||
.继承中成员变量的访问特点
|
||||
1.直接通过对象去访问,看对象创建语句=左边的内容,是谁就优先使用谁的成员变量,如果没有则向上找
|
||||
2.间接通过对象调用成员方法去访问,调用时,方法属于谁,就优先使用谁的成员变量,如果没有则向上找
|
||||
*/
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//直接创建子类对象
|
||||
Zi zi = new Zi();
|
||||
System.out.println(zi.numFu);//200
|
||||
System.out.println(zi.numZi);//100
|
||||
System.out.println(zi.num);//101
|
||||
System.out.println("----------");
|
||||
zi.methodZi();
|
||||
System.out.println("----------");
|
||||
zi.methodFu();
|
||||
System.out.println("----------3种同名变量的访问--------------");
|
||||
zi.method();
|
||||
}
|
||||
}
|
||||
10
day09/src/com/inmind/extends_member02/Fu.java
Normal file
10
day09/src/com/inmind/extends_member02/Fu.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.inmind.extends_member02;
|
||||
//父类
|
||||
public class Fu {
|
||||
int numFu = 200;
|
||||
int num = 201;//父类的成员变量
|
||||
public void methodFu(){
|
||||
System.out.println(numFu);
|
||||
System.out.println(num);
|
||||
}
|
||||
}
|
||||
19
day09/src/com/inmind/extends_member02/Zi.java
Normal file
19
day09/src/com/inmind/extends_member02/Zi.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.extends_member02;
|
||||
//子类
|
||||
public class Zi extends Fu {
|
||||
int numZi = 100;
|
||||
int num = 101;//子类的成员变量
|
||||
|
||||
public void methodZi(){
|
||||
System.out.println(numFu);
|
||||
System.out.println(numZi);
|
||||
System.out.println(num);
|
||||
}
|
||||
|
||||
public void method(){
|
||||
int num = 50;//局部变量
|
||||
System.out.println(num);//局部变量
|
||||
System.out.println(this.num);//本类的成员变量
|
||||
System.out.println(super.num);//父类的成员变量
|
||||
}
|
||||
}
|
||||
20
day09/src/com/inmind/extends_override03/Demo03.java
Normal file
20
day09/src/com/inmind/extends_override03/Demo03.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.inmind.extends_override03;
|
||||
/*
|
||||
继承中的重写:
|
||||
重载(overload):在同一个类中,方法名相同,参数列表不同
|
||||
重写(override):在父子类中,方法名相同,参数列表相同(重写,覆盖重写,覆写)
|
||||
|
||||
成员方法的访问特点:在父子类中,通过对象去访问成员方法,new后面是谁,优先使用谁的成员方法,没有则向上找
|
||||
|
||||
成员变量看 = 左边
|
||||
成员方法看 = 右边 的new
|
||||
|
||||
|
||||
重写的注意事项:
|
||||
权限:public>protected>(default)不写就是默认>private
|
||||
1. 子类方法覆盖父类方法,必须要保证权限大于等于父类权限。
|
||||
2.private的内容不能被继承,所以也就没有重写概念
|
||||
3.返回值类型、方法名和参数类型必须一致
|
||||
*/
|
||||
public class Demo03 {
|
||||
}
|
||||
8
day09/src/com/inmind/extends_override03/Fu.java
Normal file
8
day09/src/com/inmind/extends_override03/Fu.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.extends_override03;
|
||||
|
||||
public class Fu {
|
||||
public void method(){
|
||||
System.out.println("父类的method方法");
|
||||
}
|
||||
|
||||
}
|
||||
9
day09/src/com/inmind/extends_override03/Zi.java
Normal file
9
day09/src/com/inmind/extends_override03/Zi.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.inmind.extends_override03;
|
||||
|
||||
public class Zi extends Fu{
|
||||
|
||||
@Override //该注解的作用:判断当前是否是一个重写的方法
|
||||
public void method(){
|
||||
System.out.println("父类的method方法");
|
||||
}
|
||||
}
|
||||
15
day09/src/com/inmind/extends_override03/test/Demo04.java
Normal file
15
day09/src/com/inmind/extends_override03/test/Demo04.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.inmind.extends_override03.test;
|
||||
/*
|
||||
重写的应用场景:手机
|
||||
重写的意义:对于父类中方法的功能的扩展
|
||||
*/
|
||||
public class Demo04 {
|
||||
public static void main(String[] args) {
|
||||
//买新旧手机,感受下重写的效果
|
||||
Phone phone = new Phone();
|
||||
phone.show();
|
||||
System.out.println("-------");
|
||||
NewPhone newPhone = new NewPhone();
|
||||
newPhone.show();
|
||||
}
|
||||
}
|
||||
14
day09/src/com/inmind/extends_override03/test/NewPhone.java
Normal file
14
day09/src/com/inmind/extends_override03/test/NewPhone.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.inmind.extends_override03.test;
|
||||
|
||||
public class NewPhone extends Phone{
|
||||
//ctrl+O 展示父类中可以重写的方法
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
//重写时,可以选择性地沿用父类的功能,然后扩展,也可以直接推翻,重新实现
|
||||
super.show();//沿用父类的功能
|
||||
//以下代码就是对父类功能的扩展
|
||||
System.out.println("显示头像");
|
||||
System.out.println("显示归属地");
|
||||
}
|
||||
}
|
||||
18
day09/src/com/inmind/extends_override03/test/Phone.java
Normal file
18
day09/src/com/inmind/extends_override03/test/Phone.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.inmind.extends_override03.test;
|
||||
|
||||
public class Phone {
|
||||
String brand;
|
||||
String color;
|
||||
|
||||
public void call(String number) {
|
||||
System.out.println("给"+number+"打电话");
|
||||
}
|
||||
|
||||
public void send(String number) {
|
||||
System.out.println("给"+number+"发短信");
|
||||
}
|
||||
|
||||
public void show() {
|
||||
System.out.println("显示电话号码");
|
||||
}
|
||||
}
|
||||
50
day10/src/com/inmind/duotai08/Demo08.java
Normal file
50
day10/src/com/inmind/duotai08/Demo08.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.inmind.duotai08;
|
||||
/*
|
||||
多态的格式和使用
|
||||
|
||||
多态的前提:【重点】
|
||||
继承或者实现【二选一】 类与类的继承 实现类与接口的实现
|
||||
方法的重写【意义体现:不重写,无意义】
|
||||
父类引用指向子类对象【格式体现】
|
||||
---------------------------------------------------
|
||||
在多态中,成员变量和成员方法的访问特点
|
||||
在多态中,成员变量的访问特点:
|
||||
1.直接通过对象访问变量,对象创建语句中,=左边是谁,就优先使用谁的成员变量,如果没有则向上找
|
||||
2.通过对象调用成员方法,间接访问成员变量,方法属于谁,就优先使用谁的成员变量,如果没有则向上找
|
||||
|
||||
在多态中,成员方法的访问特点:
|
||||
1.编译看左边,运行看右边(在多态里面,方法的调用只能调用父类拥有的方法,而子类中独有的方法是不能调用)
|
||||
2.通过对象直接访问成员方法时,new 后是谁,就优先使用谁的成员方法,没有则向上找。
|
||||
|
||||
多态的格式:
|
||||
父类 对象名 = new 子类对象();//多态,父类和子类的关系也可以使用接口(父类)和实现类(子类)的关系来操作
|
||||
|
||||
一定要注意:编译看左边,运行看右边。
|
||||
多态中:代码的书写时,父类中没有的变量和方法是不能写的,因为编译看左边,编译器会直接报错
|
||||
|
||||
*/
|
||||
public class Demo08 {
|
||||
public static void main(String[] args) {
|
||||
Zi zi = new Zi();//不是多态,只是简单的面向对象
|
||||
Fu fu = new Zi();//是多态:父类引用指向子类对象【格式体现】
|
||||
fu.methodFu();
|
||||
fu.method();//多态写法
|
||||
//多态是为了更好的封装,是动态的框架设计技术,扩展
|
||||
System.out.println("---------------多态中成员变量的访问特点---------------");
|
||||
//对象直接访问
|
||||
System.out.println(fu.numFu);//10
|
||||
System.out.println(fu.num);//20
|
||||
//由于多态中编译看左边
|
||||
fu.methodFu();
|
||||
//fu.methodZi();//Fu类中没有methodZi()方法定义,所以在编译时期就无法调用
|
||||
System.out.println("---------------多态中成员方法的访问特点---------------");
|
||||
/*
|
||||
在多态中,成员方法的访问特点:
|
||||
1.编译看左边,运行看右边(在多态里面,方法的调用只能调用父类拥有的方法,而子类中独有的方法是不能调用)
|
||||
2.通过对象直接访问成员方法时,new 后是谁,就优先使用谁的成员方法,没有则向上找。
|
||||
*/
|
||||
fu.methodFu();
|
||||
fu.method();
|
||||
//fu.methodZi();//错误:编译看左边
|
||||
}
|
||||
}
|
||||
16
day10/src/com/inmind/duotai08/Fu.java
Normal file
16
day10/src/com/inmind/duotai08/Fu.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.inmind.duotai08;
|
||||
|
||||
public class Fu {
|
||||
int numFu = 10;
|
||||
int num = 20;
|
||||
|
||||
public void methodFu(){
|
||||
System.out.println(numFu);
|
||||
System.out.println(num);
|
||||
System.out.println("父类的methodFu方法");
|
||||
}
|
||||
|
||||
public void method(){
|
||||
System.out.println("父类的method方法");
|
||||
}
|
||||
}
|
||||
18
day10/src/com/inmind/duotai08/Zi.java
Normal file
18
day10/src/com/inmind/duotai08/Zi.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.inmind.duotai08;
|
||||
|
||||
public class Zi extends Fu{
|
||||
int numZi = 30;
|
||||
int num = 40;
|
||||
|
||||
public void methodZi(){
|
||||
System.out.println(numFu);
|
||||
System.out.println(num);
|
||||
System.out.println(numZi);
|
||||
System.out.println("子类的methodZi方法");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method(){
|
||||
System.out.println("子类的method方法");
|
||||
}
|
||||
}
|
||||
9
day10/src/com/inmind/duotai08/downcast/Animal.java
Normal file
9
day10/src/com/inmind/duotai08/downcast/Animal.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.inmind.duotai08.downcast;
|
||||
|
||||
public abstract class Animal {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
//吃的行为
|
||||
public abstract void eat();
|
||||
}
|
||||
12
day10/src/com/inmind/duotai08/downcast/Cat.java
Normal file
12
day10/src/com/inmind/duotai08/downcast/Cat.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.inmind.duotai08.downcast;
|
||||
|
||||
public class Cat extends Animal{
|
||||
@Override
|
||||
public void eat() {
|
||||
System.out.println("猫吃鱼");
|
||||
}
|
||||
|
||||
public void catchMouse(){
|
||||
System.out.println("猫抓老鼠");
|
||||
}
|
||||
}
|
||||
23
day10/src/com/inmind/duotai08/downcast/Demo10.java
Normal file
23
day10/src/com/inmind/duotai08/downcast/Demo10.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.inmind.duotai08.downcast;
|
||||
/*
|
||||
学习的对象的向上转型和向下转型,也就是引用数据类型的数据类型转换(自动转换和强制类型转换)
|
||||
|
||||
注意:多态的向下转型时,一定要是对应的类型才能转换,否则会报错java.lang.ClassCastException(类型转换异常)
|
||||
*/
|
||||
public class Demo10 {
|
||||
public static void main(String[] args) {
|
||||
//多态的写法
|
||||
Animal a = new Dog();//父类引用指向子类对象,对象的向上转型,非常安全
|
||||
a.eat();
|
||||
//a.watchDoor();//编译看左边
|
||||
|
||||
//现在a就是一只狗,我想让它看门
|
||||
Dog dog = (Dog) a;
|
||||
dog.watchDoor();
|
||||
|
||||
//现在a是一个动物,让它变成猫,抓老鼠
|
||||
Cat cat = (Cat) a;
|
||||
cat.catchMouse();
|
||||
|
||||
}
|
||||
}
|
||||
38
day10/src/com/inmind/duotai08/downcast/Demo11.java
Normal file
38
day10/src/com/inmind/duotai08/downcast/Demo11.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.inmind.duotai08.downcast;
|
||||
/*
|
||||
如何在代码中判断出当前的多态父类类型是不是对应的子类类型呢?
|
||||
instanceof 关键字
|
||||
多态类型 instanceof 子类类型 会有一个返回值 boolean
|
||||
true: 多态类型就是子类类型,(animal是一个猫)
|
||||
false:多态类型就不是子类类型,(animal不是一个猫)
|
||||
*/
|
||||
public class Demo11 {
|
||||
public static void main(String[] args) {
|
||||
//来一只狗
|
||||
Dog dog = new Dog();
|
||||
//来一只猫
|
||||
Cat cat = new Cat();
|
||||
//假设每个子类对象都要执行同一段代码,再调用eat,并调用各自的独有的功能
|
||||
work(dog);
|
||||
work(cat);
|
||||
}
|
||||
|
||||
//使用多态的好处,定义一个方法
|
||||
public static void work(Animal animal) {//多态的好用,向上转型
|
||||
System.out.println(1);
|
||||
System.out.println(2);
|
||||
System.out.println(3);
|
||||
animal.eat();
|
||||
|
||||
//在进行向下转型时,必须先判断是否是该类型
|
||||
if (animal instanceof Dog) {
|
||||
Dog d = (Dog) animal;
|
||||
d.watchDoor();
|
||||
}
|
||||
|
||||
if (animal instanceof Cat) {
|
||||
Cat c = (Cat) animal;
|
||||
c.catchMouse();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
day10/src/com/inmind/duotai08/downcast/Dog.java
Normal file
12
day10/src/com/inmind/duotai08/downcast/Dog.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.inmind.duotai08.downcast;
|
||||
|
||||
public class Dog extends Animal{
|
||||
@Override
|
||||
public void eat() {
|
||||
System.out.println("狗吃骨头");
|
||||
}
|
||||
|
||||
public void watchDoor(){
|
||||
System.out.println("狗看门");
|
||||
}
|
||||
}
|
||||
23
day10/src/com/inmind/duotai08/interface_duotai/Demo12.java
Normal file
23
day10/src/com/inmind/duotai08/interface_duotai/Demo12.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.inmind.duotai08.interface_duotai;
|
||||
/*
|
||||
接口和实现类的多态
|
||||
|
||||
多态的前提:
|
||||
1.继承或实现
|
||||
2.重写
|
||||
3.父类引用指向子类对象
|
||||
*/
|
||||
public class Demo12 {
|
||||
public static void main(String[] args) {
|
||||
MyInterfaceImpl myInterface = new MyInterfaceImpl();//是不是多态???不是
|
||||
//多态的格式:父类引用指向子类对象
|
||||
//接口中的多态格式:接口类型 对象名 = new 实现类类型();
|
||||
MyInterface myInterface1 = new MyInterfaceImpl();//多态的写法,向上转型
|
||||
myInterface1.show();//编译看左边,运行看右边
|
||||
//但是我就想要调用实现类的show1方法,那怎么办???
|
||||
if (myInterface1 instanceof MyInterfaceImpl) {//为了类型转换时,避免出现类型转换异常,先判断类型
|
||||
MyInterfaceImpl m = (MyInterfaceImpl)myInterface1; //向下转型
|
||||
m.show1();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.inmind.duotai08.interface_duotai;
|
||||
|
||||
public interface MyInterface {
|
||||
public abstract void show();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.inmind.duotai08.interface_duotai;
|
||||
|
||||
public class MyInterfaceImpl implements MyInterface{
|
||||
@Override
|
||||
public void show() {
|
||||
System.out.println("实现类的show方法");
|
||||
}
|
||||
|
||||
|
||||
public void show1() {
|
||||
System.out.println("实现类的show1方法");
|
||||
}
|
||||
}
|
||||
8
day10/src/com/inmind/duotai08/test/Assistant.java
Normal file
8
day10/src/com/inmind/duotai08/test/Assistant.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.duotai08.test;
|
||||
|
||||
public class Assistant extends Employee{
|
||||
@Override
|
||||
public void work() {
|
||||
System.out.println("辅导员在辅导学生");
|
||||
}
|
||||
}
|
||||
46
day10/src/com/inmind/duotai08/test/Demo09.java
Normal file
46
day10/src/com/inmind/duotai08/test/Demo09.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.inmind.duotai08.test;
|
||||
/*
|
||||
多态的核心好处可以概括为:**“以不变应万变”**—— 通过接口或者父类,应对具体实现的变化。
|
||||
它让代码更简洁、更灵活、更易于扩展,是构建大型、可维护系统的关键技术。
|
||||
*/
|
||||
public class Demo09 {
|
||||
public static void main(String[] args) {
|
||||
//没有多态的代码编写
|
||||
//创建出每个子类对象,固定地执行一段功能代码,调用子类的work
|
||||
Teacher teacher = new Teacher();
|
||||
|
||||
Assistant assistant = new Assistant();
|
||||
|
||||
//以上代码没有多态,所以有50个子类,重复的代码就要编写或封装50次,太繁琐
|
||||
//多态写法
|
||||
Employee e1 = new Teacher();//引用数据类型向上转型
|
||||
Employee e2 = new Assistant();//引用数据类型向上转型
|
||||
dTWork(e1);//引用数据类型向上转型
|
||||
dTWork(e2);//引用数据类型向上转型
|
||||
dTWork(teacher);//引用数据类型向上转型
|
||||
dTWork(assistant);//引用数据类型向上转型
|
||||
}
|
||||
|
||||
//定义一个父类类型接收子类对象,多态的写法
|
||||
private static void dTWork(Employee employee) {
|
||||
System.out.println("hello");
|
||||
System.out.println("world");
|
||||
employee.work();
|
||||
}
|
||||
|
||||
//定义一个重复调用的普通写法
|
||||
/*private static void dWork1(Assistant assistant) {
|
||||
System.out.println("hello");
|
||||
System.out.println("world");
|
||||
assistant.work();
|
||||
}
|
||||
|
||||
private static void dWork(Teacher teacher) {
|
||||
System.out.println("hello");
|
||||
System.out.println("world");
|
||||
teacher.work();
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
8
day10/src/com/inmind/duotai08/test/Employee.java
Normal file
8
day10/src/com/inmind/duotai08/test/Employee.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.duotai08.test;
|
||||
|
||||
public abstract class Employee {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
public abstract void work();//强制子类必须实现重写该方法
|
||||
}
|
||||
8
day10/src/com/inmind/duotai08/test/Teacher.java
Normal file
8
day10/src/com/inmind/duotai08/test/Teacher.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.duotai08.test;
|
||||
|
||||
public class Teacher extends Employee{
|
||||
@Override
|
||||
public void work() {
|
||||
System.out.println("老师在上课");
|
||||
}
|
||||
}
|
||||
42
day10/src/com/inmind/duotai08/test2/Computer.java
Normal file
42
day10/src/com/inmind/duotai08/test2/Computer.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.inmind.duotai08.test2;
|
||||
/*
|
||||
笔记本类,包含运行功能、关机功能、使用USB设备功能
|
||||
实现笔记本使用USB鼠标、USB键盘
|
||||
使用USB鼠标、USB键盘:将一类USB设备可以传递给电脑,进行使用,其实就是定义一个方法接收USB类型(多态)
|
||||
*/
|
||||
public class Computer {
|
||||
//运行功能
|
||||
public void powerOn(){
|
||||
System.out.println("电脑开机了");
|
||||
}
|
||||
|
||||
//关机功能
|
||||
public void powerOff(){
|
||||
System.out.println("电脑关机了");
|
||||
}
|
||||
|
||||
//使用USB设备功能
|
||||
//具体是什么USB设备,笔记本并不关心,只要符合USB规格的设备都可以
|
||||
public void useUsbDevice(Usb usbDevice){//接口的多态:Usb usbDevice = new 键盘、鼠标、U盘等USB设备对象;(向上转型)
|
||||
if (usbDevice == null) {
|
||||
System.out.println("您使用的USB设备不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
usbDevice.open();
|
||||
//必须先向下转型
|
||||
if (usbDevice instanceof KeyBoard) {
|
||||
KeyBoard keyBoard = (KeyBoard) usbDevice;
|
||||
keyBoard.knock();
|
||||
}
|
||||
|
||||
if (usbDevice instanceof Mouse) {
|
||||
Mouse mouse = (Mouse) usbDevice;
|
||||
mouse.click();
|
||||
}
|
||||
}
|
||||
|
||||
public void closeUsbDevice(Usb usbDevice) {
|
||||
usbDevice.close();
|
||||
}
|
||||
}
|
||||
35
day10/src/com/inmind/duotai08/test2/Demo13.java
Normal file
35
day10/src/com/inmind/duotai08/test2/Demo13.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.inmind.duotai08.test2;
|
||||
/*
|
||||
笔记本电脑(laptop)通常具备使用USB设备的功能。
|
||||
在生产时,笔记本都预留了可以插入USB设备的USB接口,但具体是什么USB设备,笔记本厂商并不关心,只要符合USB规格的设备都可以。
|
||||
|
||||
定义USB接口,具备最基本的开启功能和关闭功能。
|
||||
鼠标和键盘要想能在电脑上使用,那么鼠标和键盘也必须遵守USB规范,实现USB接口,否则鼠标和键盘的生产出来也无法使用。
|
||||
|
||||
进行描述笔记本类,实现笔记本使用USB鼠标、USB键盘
|
||||
使用USB鼠标、USB键盘:将一类USB设备可以传递给电脑,进行使用,其实就是定义一个方法接收USB类型(多态)
|
||||
|
||||
USB接口,包含开启功能、关闭功能
|
||||
笔记本类,包含运行功能、关机功能、使用USB设备功能
|
||||
鼠标类,要实现USB接口,并具备点击的方法
|
||||
键盘类,要实现USB接口,具备敲击的方法
|
||||
*/
|
||||
public class Demo13 {
|
||||
public static void main(String[] args) {
|
||||
//现实中先买一台电脑
|
||||
Computer computer = new Computer();
|
||||
//再买个键盘和鼠标
|
||||
KeyBoard keyBoard = new KeyBoard();
|
||||
Mouse mouse = new Mouse();
|
||||
//电脑开机
|
||||
computer.powerOn();
|
||||
//使用电脑:使用USB设备
|
||||
computer.useUsbDevice(mouse);
|
||||
computer.useUsbDevice(keyBoard);
|
||||
//玩了一伙,关机吃饭
|
||||
computer.closeUsbDevice(keyBoard);
|
||||
computer.closeUsbDevice(mouse);
|
||||
|
||||
computer.powerOff();
|
||||
}
|
||||
}
|
||||
18
day10/src/com/inmind/duotai08/test2/KeyBoard.java
Normal file
18
day10/src/com/inmind/duotai08/test2/KeyBoard.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.inmind.duotai08.test2;
|
||||
//键盘类,要实现USB接口,具备敲击的方法
|
||||
public class KeyBoard implements Usb{
|
||||
@Override
|
||||
public void open() {
|
||||
System.out.println("键盘启动了");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
System.out.println("键盘关闭了");
|
||||
}
|
||||
|
||||
//接口实现类的独有的功能
|
||||
public void knock(){
|
||||
System.out.println("键盘敲击输入内容");
|
||||
}
|
||||
}
|
||||
19
day10/src/com/inmind/duotai08/test2/Mouse.java
Normal file
19
day10/src/com/inmind/duotai08/test2/Mouse.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.duotai08.test2;
|
||||
//鼠标类,要实现USB接口,并具备点击的方法
|
||||
public class Mouse implements Usb{
|
||||
@Override
|
||||
public void open() {
|
||||
System.out.println("鼠标启动了");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
System.out.println("鼠标关闭了");
|
||||
}
|
||||
|
||||
//独有的功能
|
||||
public void click(){
|
||||
System.out.println("鼠标点击了");
|
||||
}
|
||||
|
||||
}
|
||||
6
day10/src/com/inmind/duotai08/test2/Usb.java
Normal file
6
day10/src/com/inmind/duotai08/test2/Usb.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.inmind.duotai08.test2;
|
||||
//USB接口,包含开启功能、关闭功能
|
||||
public interface Usb {
|
||||
public abstract void open();
|
||||
void close();
|
||||
}
|
||||
8
day10/src/com/inmind/extends_impls06/Demo05.java
Normal file
8
day10/src/com/inmind/extends_impls06/Demo05.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.inmind.extends_impls06;
|
||||
|
||||
public class Demo05 {
|
||||
public static void main(String[] args) {
|
||||
Zi zi = new Zi();
|
||||
zi.sameMethod();
|
||||
}
|
||||
}
|
||||
10
day10/src/com/inmind/extends_impls06/Fu.java
Normal file
10
day10/src/com/inmind/extends_impls06/Fu.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.inmind.extends_impls06;
|
||||
|
||||
//抽象父类
|
||||
public abstract class Fu {
|
||||
public abstract void methodFu();
|
||||
//抽象父类中的普通方法
|
||||
public void sameMethod(){
|
||||
System.out.println("抽象父类中的方法sameMethod()");
|
||||
};
|
||||
}
|
||||
6
day10/src/com/inmind/extends_impls06/MyInterface1.java
Normal file
6
day10/src/com/inmind/extends_impls06/MyInterface1.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.inmind.extends_impls06;
|
||||
|
||||
public interface MyInterface1 {
|
||||
void method1();
|
||||
void sameMethod();
|
||||
}
|
||||
6
day10/src/com/inmind/extends_impls06/MyInterface2.java
Normal file
6
day10/src/com/inmind/extends_impls06/MyInterface2.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.inmind.extends_impls06;
|
||||
|
||||
public interface MyInterface2 {
|
||||
void method2();
|
||||
void sameMethod();
|
||||
}
|
||||
33
day10/src/com/inmind/extends_impls06/Zi.java
Normal file
33
day10/src/com/inmind/extends_impls06/Zi.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.inmind.extends_impls06;
|
||||
/*
|
||||
单继承多实现的注意事项:
|
||||
1.非抽象的子类实现类必须重写所有的抽象方法
|
||||
2.如果有抽象方法未实现,那么子类实现类必须是抽象类
|
||||
3.如果多个接口中有相同的抽象方法,那么非抽象的子类只需要实现一次即可
|
||||
4.如果多个接口和抽象父类中有相同的抽象方法,那么非抽象的子类也只需要实现一次即可
|
||||
5.如果抽象父类中拥有一个与多个接口中抽象方法,相同的普通方法,那么子类实现类,
|
||||
就可以选择性地重写或不重写该抽象方法,类的方法优先于接口的
|
||||
|
||||
*/
|
||||
public class Zi extends Fu implements MyInterface1,MyInterface2{
|
||||
@Override
|
||||
public void methodFu() {
|
||||
System.out.println("子类重写抽象父类中的抽象方法");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method1() {
|
||||
System.out.println("实现类实现接口MyInterface1中的抽象方法");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sameMethod() {
|
||||
System.out.println("实现类实现接口MyInterface1和MyInterface2中的同名抽象方法");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void method2() {
|
||||
System.out.println("实现类实现接口MyInterface2中的抽象方法");
|
||||
}
|
||||
}
|
||||
22
day10/src/com/inmind/interface_abstract01/Demo01.java
Normal file
22
day10/src/com/inmind/interface_abstract01/Demo01.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.inmind.interface_abstract01;
|
||||
/*
|
||||
接口:Integerface,用于定义统一要遵守的规范和规则
|
||||
|
||||
接口的抽象方法的使用步骤:
|
||||
1.接口不能创建对象的,它没有构造方法
|
||||
2.定义一个实现类,实现该接口,并实现接口中的所有的抽象方法
|
||||
public class MyInterfaceImpl(实现类) implements MyInterface(接口){
|
||||
3.创建该接口的实现类的对象,调用接口的方法
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
//接口不能创建对象的,它没有构造方法
|
||||
//MyInterface myInterface = new MyInterface();
|
||||
|
||||
//创建该接口的实现类的对象,调用接口的方法
|
||||
MyInterfaceImpl myInterface = new MyInterfaceImpl();
|
||||
myInterface.method();
|
||||
myInterface.method1();
|
||||
|
||||
}
|
||||
}
|
||||
21
day10/src/com/inmind/interface_abstract01/MyInterface.java
Normal file
21
day10/src/com/inmind/interface_abstract01/MyInterface.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.inmind.interface_abstract01;
|
||||
/*
|
||||
接口就是生活中的规范,规则
|
||||
定义格式
|
||||
public interface 接口名 {
|
||||
}
|
||||
|
||||
jdk7之前:抽象方法,常量
|
||||
jdk8:默认方法,静态方法
|
||||
jdk9:私有方法
|
||||
|
||||
--------------------------------------
|
||||
接口中抽象方法定义:
|
||||
public abstract 返回值类型 抽象方法名();
|
||||
但凡在接口中定义一个没有方法体{}的方法,就是public abstract修饰;暂时不省略
|
||||
*/
|
||||
public interface MyInterface {
|
||||
//抽象方法
|
||||
public abstract void method();
|
||||
void method1();//注意:接口中,定义的抽象方法,如果不写abstract,编译器也会主动加上public abstract
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.inmind.interface_abstract01;
|
||||
|
||||
//实现类,实现MyInterface接口
|
||||
public class MyInterfaceImpl implements MyInterface{
|
||||
@Override
|
||||
public void method() {
|
||||
System.out.println("实现类MyInterfaceImpl重写抽象方法method()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method1() {
|
||||
System.out.println("实现类MyInterfaceImpl重写抽象方法method1()");
|
||||
}
|
||||
}
|
||||
17
day10/src/com/inmind/interface_constatnt05/Demo04.java
Normal file
17
day10/src/com/inmind/interface_constatnt05/Demo04.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.inmind.interface_constatnt05;
|
||||
/*
|
||||
接口中的内容特点:
|
||||
1.抽象方法(jdk7之前):制定规则,必须遵守的规范
|
||||
2.默认方法(jdk8):优化解决接口升级的问题,选择性地沿用,或者扩展新功能
|
||||
3.静态方法(jdk8):定义一些跟接口本身相关的,通用的工具类方法
|
||||
4.私有方法(jdk9): 对默认方法和静态方法,进行共有代码的抽取复用,且不让外界调用
|
||||
5.常量:定义了公共的标准值,为接口的行为提供规范,配套的固定值
|
||||
*/
|
||||
public class Demo04 {
|
||||
public static void main(String[] args) {
|
||||
//使用接口中定义的“成员变量”
|
||||
System.out.println(MyInterfaceConstatnt.num);
|
||||
System.out.println(MyInterfaceConstatnt.num1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.inmind.interface_constatnt05;
|
||||
/*
|
||||
接口中定义"成员变量",其实是一个常量,编译器会在成员属性前,默认加上【public static final】!!!
|
||||
final:最终,被它修饰的内容就不能更改
|
||||
接口中的常量的作用:为接口的行为规范,提供配套的固定值
|
||||
接口中的常量如何使用:接口名.常量名来调用
|
||||
*/
|
||||
public interface MyInterfaceConstatnt {
|
||||
public static final int num = 10;//"成员变量"
|
||||
int num1 = 20;//编译器会在成员属性前,默认加上【public static final】!!!
|
||||
}
|
||||
22
day10/src/com/inmind/interface_default02/Demo02.java
Normal file
22
day10/src/com/inmind/interface_default02/Demo02.java
Normal file
@@ -0,0 +1,22 @@
|
||||
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) {
|
||||
//实现类1
|
||||
MyInterfaceImpl impl = new MyInterfaceImpl();
|
||||
impl.method2();
|
||||
|
||||
//实现类2
|
||||
MyInterfaceImpl1 impl1 = new MyInterfaceImpl1();
|
||||
impl1.method2();
|
||||
}
|
||||
}
|
||||
24
day10/src/com/inmind/interface_default02/MyInterface.java
Normal file
24
day10/src/com/inmind/interface_default02/MyInterface.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.inmind.interface_default02;
|
||||
/*
|
||||
第一个版本的规则,有一个固定功能method
|
||||
第二个版本中,需要增加功能method2
|
||||
|
||||
接口是可以升级的,添加新功能,之前添加功能只能通过抽象方法,导致所有的实现类都报错,这样非常麻烦。
|
||||
为了解决该问题,jdk8提供了默认方法来解决
|
||||
|
||||
接口中的默认方法的定义格式:
|
||||
public default 返回值类型 方法名(参数列表){
|
||||
默认实现
|
||||
}
|
||||
|
||||
*/
|
||||
public interface MyInterface {
|
||||
public abstract void method();
|
||||
|
||||
void method1();
|
||||
|
||||
//默认方法
|
||||
public default void method2(){
|
||||
System.out.println("接口中的默认方法method2");
|
||||
};//新增的功能规范
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.inmind.interface_default02;
|
||||
|
||||
//实现类,实现MyInterface接口
|
||||
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,20 @@
|
||||
package com.inmind.interface_default02;
|
||||
|
||||
//实现类,实现MyInterface接口
|
||||
public class MyInterfaceImpl1 implements MyInterface {
|
||||
@Override
|
||||
public void method() {
|
||||
System.out.println("实现类MyInterfaceImpl重写抽象方法method()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method1() {
|
||||
System.out.println("实现类MyInterfaceImpl重写抽象方法method1()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method2() {//实现类覆盖了接口中的默认功能
|
||||
//MyInterface.super.method2();//沿用接口中的功能
|
||||
System.out.println("实现类MyInterfaceImpl1,完全重写了接口MyInterface中的默认方法method2");
|
||||
}
|
||||
}
|
||||
24
day10/src/com/inmind/interface_extends07/Demo07.java
Normal file
24
day10/src/com/inmind/interface_extends07/Demo07.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.inmind.interface_extends07;
|
||||
/*
|
||||
类的继承:单继承的,一个类永远都只能有一个直接父类
|
||||
|
||||
接口继承,接口是多继承的,接口可以有很多个直接父类
|
||||
注意事项:
|
||||
1.如果是继承,子接口可以继承所有父接口的抽象方法的,子接口的实现类必须实现所有的抽象方法
|
||||
2.如果继承的父接口们中有同名的抽象方法,子类接口只会继承一个抽象方法,子接口的实现类也只需要实现一次该同名抽象方法。
|
||||
3.接口的静态方法不能通过对象调用的
|
||||
4.父接口的静态方法不能通过子类接口直接访问调用的
|
||||
5.静态方法属于哪个接口,就应该由哪个接口名直接调用
|
||||
|
||||
总结:
|
||||
类与类: 单继承 extends
|
||||
类与接口: 多实现 implements
|
||||
接口与接口:多继承 extends
|
||||
*/
|
||||
public class Demo07 {
|
||||
public static void main(String[] args) {
|
||||
//调用2个父接口的静态方法
|
||||
MyInterfaceFu1.methodSFu1();
|
||||
MyInterfaceFu2.methodSFu2();
|
||||
}
|
||||
}
|
||||
14
day10/src/com/inmind/interface_extends07/MyInterfaceFu1.java
Normal file
14
day10/src/com/inmind/interface_extends07/MyInterfaceFu1.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public interface MyInterfaceFu1 {
|
||||
void methodFu1();
|
||||
|
||||
public static void methodSFu1(){
|
||||
System.out.println("MyInterfaceFu1的静态方法methodSFu1");
|
||||
}
|
||||
|
||||
public default void methodD(){
|
||||
System.out.println("MyInterfaceFu1的默认方法methodD");
|
||||
}
|
||||
|
||||
}
|
||||
14
day10/src/com/inmind/interface_extends07/MyInterfaceFu2.java
Normal file
14
day10/src/com/inmind/interface_extends07/MyInterfaceFu2.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public interface MyInterfaceFu2 {
|
||||
void methodFu2();
|
||||
void methodFu();
|
||||
|
||||
public static void methodSFu2(){
|
||||
System.out.println("MyInterfaceFu2的静态方法methodSFu2");
|
||||
}
|
||||
|
||||
public default void methodD(){
|
||||
System.out.println("MyInterfaceFu2的默认方法methodD");
|
||||
}
|
||||
}
|
||||
11
day10/src/com/inmind/interface_extends07/MyInterfaceZi.java
Normal file
11
day10/src/com/inmind/interface_extends07/MyInterfaceZi.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public interface MyInterfaceZi extends MyInterfaceFu1,MyInterfaceFu2{
|
||||
void methodZi();
|
||||
|
||||
//注意:如果父接口中的默认方法有重名的,那么子接口必须重写该默认方法
|
||||
@Override
|
||||
default void methodD() {
|
||||
System.out.println("重新实现重名的默认方法");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.inmind.interface_extends07;
|
||||
|
||||
public class MyInterfaceZiImpl implements MyInterfaceZi{
|
||||
@Override
|
||||
public void methodZi() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void methodFu1() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void methodFu2() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void methodFu() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.inmind.interface_private04;
|
||||
/*
|
||||
既有默认方法,和静态方法,有可能在一个接口中有很多这些方法
|
||||
|
||||
接口的私有方法的作用:给接口中默认方法来调用的,可以将相同的重复的功能代码进行抽取优化,也可以对静态方法进行抽取
|
||||
|
||||
私有方法的定义格式:
|
||||
private (static) 返回值类型 方法名(参数列表) {
|
||||
java方法体
|
||||
}
|
||||
*/
|
||||
public interface MyInterfacePrivate {
|
||||
//3个默认方法
|
||||
default void method1(){
|
||||
System.out.println("method1方法执行");
|
||||
sameContent();
|
||||
}
|
||||
default void method2(){
|
||||
System.out.println("method2方法执行");
|
||||
sameContent();
|
||||
}
|
||||
default void method3(){
|
||||
System.out.println("method3方法执行");
|
||||
sameContent();
|
||||
}
|
||||
|
||||
//定义一个私有方法来对默认方法中相同部分进行抽取复用
|
||||
private static void sameContent(){
|
||||
System.out.println("相同的代码片段1");
|
||||
System.out.println("相同的代码片段2");
|
||||
System.out.println("相同的代码片段3");
|
||||
System.out.println("相同的代码片段4");
|
||||
}
|
||||
|
||||
}
|
||||
25
day10/src/com/inmind/interface_static03/Demo03.java
Normal file
25
day10/src/com/inmind/interface_static03/Demo03.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.inmind.interface_static03;
|
||||
/*
|
||||
jdk8提供的接口中的静态方法
|
||||
接口中的静态方法的作用:封装接口相关的通用的功能,给相关实现类作为辅助工具的方法
|
||||
|
||||
类中的静态:只跟类有关,跟对象无关
|
||||
类中的静态调用:
|
||||
1.对象名.静态内容(不推荐)
|
||||
2.类名.静态内容(推荐)
|
||||
|
||||
接口的静态方法
|
||||
1.是否通过对象调用静态方法的形式来操作???不能
|
||||
2.只能通过接口名.静态方法来调用
|
||||
接口的静态方法不能通过实现类对象调用或操作,由于接口的多继承(接口与接口之间的关系)导致
|
||||
*/
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
//接口中静态方法的调用---正确的做法
|
||||
MyInterface.staticMethod();
|
||||
|
||||
//使用接口的实现类来调用接口的静态方法-------错误做法,与之前的类中静态操作不同
|
||||
/*MyInterfaceImpl myInterface = new MyInterfaceImpl();
|
||||
myInterface.staticMethod();*/
|
||||
}
|
||||
}
|
||||
15
day10/src/com/inmind/interface_static03/MyInterface.java
Normal file
15
day10/src/com/inmind/interface_static03/MyInterface.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.inmind.interface_static03;
|
||||
/*
|
||||
定义静态方法格式
|
||||
public static 返回值类型 方法名(参数列表){
|
||||
方法体
|
||||
};
|
||||
|
||||
*/
|
||||
public interface MyInterface {
|
||||
// void method();//抽象方法
|
||||
//静态方法
|
||||
public static void staticMethod(){
|
||||
System.out.println("接口中的静态方法");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.inmind.interface_static03;
|
||||
|
||||
public class MyInterfaceImpl implements MyInterface{
|
||||
//重写快捷键:ctrl+o
|
||||
|
||||
|
||||
}
|
||||
28
day11/src/com/inmind/class_member_var09/Armor.java
Normal file
28
day11/src/com/inmind/class_member_var09/Armor.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.inmind.class_member_var09;
|
||||
|
||||
//防御装类
|
||||
public class Armor {
|
||||
private String name;
|
||||
private int protectNum;//防御值
|
||||
|
||||
public Armor(String name, int protectNum) {
|
||||
this.name = name;
|
||||
this.protectNum = protectNum;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getProtectNum() {
|
||||
return protectNum;
|
||||
}
|
||||
|
||||
public void setProtectNum(int protectNum) {
|
||||
this.protectNum = protectNum;
|
||||
}
|
||||
}
|
||||
46
day11/src/com/inmind/class_member_var09/Demo07.java
Normal file
46
day11/src/com/inmind/class_member_var09/Demo07.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.inmind.class_member_var09;
|
||||
/*
|
||||
类作为成员变量类型(hero,weapon,armor)
|
||||
案例:英雄角色,获取武器,获取防御装,闯关
|
||||
*/
|
||||
public class Demo07 {
|
||||
public static void main(String[] args) {
|
||||
//创建英雄角色对象
|
||||
Hero hero = new Hero();
|
||||
hero.setName("德玛西亚");
|
||||
|
||||
//打怪,掉落武器
|
||||
Weapon weapon = new Weapon("屠龙刀", 999);
|
||||
hero.setWeapon(weapon);
|
||||
//打怪,掉落防具
|
||||
Armor armor = new Armor("复活甲", 888);
|
||||
hero.setArmor(armor);
|
||||
|
||||
//打boss
|
||||
hero.attack();
|
||||
//boss回击
|
||||
hero.protect();
|
||||
|
||||
System.out.println("BOSS掉落了一本技能书");
|
||||
Skill skill = new Skill() {
|
||||
@Override
|
||||
public void outSkill() {
|
||||
System.out.println("施放野火燎原技能,团队伤害9999");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "野火燎原";
|
||||
}
|
||||
};
|
||||
//装备技能
|
||||
hero.setSkill(skill);
|
||||
//英雄获取自己的技能,并施放
|
||||
hero.getSkill().outSkill();
|
||||
|
||||
|
||||
System.out.println("游戏结束");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user