We believe that writing is about content, about what you want to say – not about fancy formatting.
我们坚信写作写的是内容,所思所想,而不是花样格式。
— Ulysses for Mac
关于收获:
导图:
抽象类和接口区别:
Abstract class | Interface | |
---|---|---|
实例化 | 不能 | 不能 |
类 | 一种继承关系,一个类只能使用一次继承关系。可以通过继承多个接口实现多重继承 | 一个类可以实现多个interface |
数据成员 | 可有自己的 | 静态的不能被修改即必须是static final,一般不在此定义 |
方法 | 可以私有的,非abstract方法,必须实现 | 不可有私有的,默认是public,abstract 类型 |
变量 | 可有私有的,默认是friendly 型,其值可以在子类中重新定义,也可以重新赋值 | 不可有私有的,默认是public static final 型,且必须给其初值,实现类中不能重新定义,不能改变其值。 |
设计理念 | 表示的是“is-a”关系 | 表示的是“like-a”关系 |
实现 | 需要继承,要用extends | 要用implements |
一个类可以实现自多个接口:

类需要替代这两个接口所定义的所有方法
重点说明: (用接口实现多重继承)


多重继承发生问题原因之一在于属性(数据结构)冲突,也就是存储空间的冲突。由于接口不与任何存储空间相关联,因此可以解决存储空间冲突的问题
对于继承的方法的冲突,当使用接口之后,由于接口只定义了方法的抽象,没有具体的执行代码,因此也不会发生代码冲突的问题。
拓展示例:


对比补充:

内部类:
在Java中,可以将一个类定义在另一个类里面或者一个方法(类成员函数)里面,这样的类称为内部类。广泛意义上的内部类一般来说包括四种:
- 成员内部类
- 局部内部类
- 匿名内部类
- 静态内部类
集合:
Java集合主要分三种类型:
Set(集) List(列表) Map(映射)
重点掌握的集合:
- HashSet
- ArrayList
- LinkedList
- HashMap
重点掌握的集合操作:
增、删、改、查(遍历)
HashSet:
import java.util.HashSet;
import java.util.Iterator;
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>();
System.out.println("没数据前直接打印原HashSet结构:");
System.out.println(hashSet);
System.out.println();
// 增:
for (int i = 0; i < 4; i++) {
hashSet.add((char)(i+97)+"");
}
// 打印原set结构内容:
System.out.println("直接打印原HashSet结构:");
System.out.println(hashSet);
System.out.println();
// 删:
hashSet.remove("a");
System.out.println("直接删除数据后HashSet结构:");
System.out.println(hashSet);
System.out.println();
// 查在遍历中:
// 第一种遍历:
System.out.println("测试第一种遍历:");
for(String str : hashSet){
System.out.println(str);
}
System.out.println();
// 第二种遍历:
System.out.println("测试第二种遍历:");
Iterator<String> iterator = hashSet.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
HashSet输出:

ArrayList:
import java.util.Iterator;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
System.out.println("没数据前直接打印原list结构:");
System.out.println(list);
// 增:
for (int i = 0; i < 4; i++) {
list.add((char)(i+97)+"");
}
System.out.println("直接打印原list结构:");
System.out.println(list);
System.out.println();
// 删:
list.remove(1);
list.remove("a");
System.out.println("打印删除数据后list结构:");
System.out.println(list);
System.out.println();
// 查在遍历中:
// 第一种遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println();
// 第二种遍历
for(String s : list){
System.out.println(s);
}
System.out.println();
// 第三种遍历
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
ArrayList打印:

LinkedList:
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
System.out.println("没数据前直接打印原list结构:");
System.out.println(list);
// 增:
for (int i = 0; i < 4; i++) {
list.add((char)(i+97)+"");
}
System.out.println("直接打印原list结构:");
System.out.println(list);
System.out.println();
// 删:
list.remove(1);
list.remove("a");
System.out.println("打印删除数据后list结构:");
System.out.println(list);
System.out.println();
// 查在遍历中:
// 第一种遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println();
// 第二种遍历
for(String s : list){
System.out.println(s);
}
System.out.println();
// 第三种遍历
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
LinkedList打印结果:

HashMap:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class HashMapTest {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
System.out.println("没数据前直接打印原HashMap结构:");
System.out.println(map);
// 增:
for (int i = 0; i < 5; i++) {
map.put(Integer.toString(i+1), (char)(i+97)+"");
}
System.out.println("直接打印原HashMap结构:");
System.out.println(map);
System.out.println();
// 删:
map.remove("1");
map.remove("2", "b");
System.out.println("打印删除数据后HashMap结构:");
System.out.println(map);
// 改(即增加同样key的数据以覆盖改变value的值)
map.put("3", "cc");
System.out.println("打印改3后HashMap结构:");
System.out.println(map);
// 查在遍历中:
// 第一种遍历
System.out.println("第一次遍历");
for(String s : map.keySet()){
System.out.println(s+" : "+map.get(s));
}
// 第二种遍历
System.out.println("第二次遍历");
Iterator<String> iteratorSet = map.keySet().iterator();
while(iteratorSet.hasNext()){
String str = iteratorSet.next();
System.out.println(str+" : "+map.get(str));
}
// 第三种遍历
System.out.println("第三次遍历");
for(Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey()+" : "+entry.getValue());
}
// 第四种遍历
System.out.println("第四次遍历");
for(String str : map.values()){
System.out.println(str);
}
// 第五种遍历
System.out.println("第五次遍历");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while(it.hasNext()){
Entry<String, String> entry = it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
HashMap输出结果:

性能分析举例:
集合易错特别说明:
BDHW - 7.30 集合
异常学习:
- 理解异常概念
- 运用try块、catch块和finally块处理异常
- 运用多重catch块处理异常
- 运用嵌套try catch块处理异常
- 运用throw 和 throws处理异常
- 运用java编写和使用自定义异常
进行异常处理:(通过5个关键字实现)
- try catch finally
- throw throws
finally:

注意多重catch块 异常 顺序:
catch异常类 要先catch子类后父类

所以 这样错:

(IOException 是 FileNotFoundException的父类)
关于拓展:
世界上所有的追求都是因为热爱
一枚爱编码 爱生活 爱分享的IT信徒
— hongXkeX
网友评论