-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
> JVM、JDK、JRE? | ||
JVM -- Java 虚拟机 | ||
JRK -- Java 开发工具包 | ||
JRE -- Java 运行环境 | ||
|
||
三者之间的关系是:JDK > JRE > JVM | ||
|
||
## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
> 什么是多态? | ||
多态就是,对于同一个父类,指向不同子类对象的同一个行为,运行出来结果不同。 | ||
|
||
例如伪代码: | ||
|
||
```java | ||
class Animals { | ||
public void sleep() { | ||
sout("坐着睡!"); | ||
} | ||
} | ||
|
||
class Dog extends Animals { | ||
@Override | ||
public void sleep() { | ||
sout("站立着睡!"); | ||
} | ||
} | ||
|
||
class Cat extends Animals { | ||
@Override | ||
public void sleep() { | ||
sout("睁眼睡!"); | ||
} | ||
} | ||
// 同一个父类 Animals,指向不同子类 Dog、Cat | ||
Animals animals1 = new Dog(); | ||
Animals animals2 = new Cat(); | ||
``` | ||
|
||
对于`animals1.sleep()`和`animals2.sleep()`,最后运行出来可能会有不用的结果,但是这取决于几个条件: | ||
|
||
- 继承类或实现接口 | ||
|
||
- 子类重写方法 | ||
|
||
- 同一个父类,指向不同子类 | ||
|
||
> 重载与重写什么区别? | ||
引用 Wiki 百科: | ||
|
||
> ## 函数重载规则 | ||
> | ||
> - 多个函数定义使用相同的函数名称 | ||
> - 函数参数的数量或类型必须有区别 | ||
> | ||
> 函数重载是静态多态的一种类别,其使用某种“最佳匹配”算法解析函数调用,通过找到形式参数类型与实际参数类型的最佳匹配来解析要调用的具体函数。该算法的细节因语言而异。 | ||
> | ||
> 函数重载通常与[静态类型](https://zh.wikipedia.org/wiki/類型系統)编程语言(在[函数调用](https://zh.wikipedia.org/wiki/子程序)中强制执行[类型检查](https://zh.wikipedia.org/wiki/類型系統#型別檢查))有关。重载函数实际上只是一组具有相同名称的不同函数。具体调用使用哪个函数是在[**编译期**](https://zh.wikipedia.org/wiki/编译期)决定的。 | ||
> | ||
> 在 [Java](https://zh.wikipedia.org/wiki/Java) 中,函数重载也被称为编译时多态和静态多态。 | ||
> | ||
> 函数重载不应与在运行时进行选择的[多态](https://zh.wikipedia.org/wiki/多态_(计算机科学))形式混淆,例如通过[虚函数](https://zh.wikipedia.org/wiki/虚函数)而不是静态函数。 | ||
|
||
|
||
因此我们大概明白: | ||
|
||
- 重载是编译时重载的,编译时根据参数,决定调用哪个方法 | ||
- 重写是运行期重写的,运行时根据父类指向的子类,调用方法 | ||
|
||
|
||
|
||
**总结:** | ||
|
||
重载和重写都是多态的体现,维基百科中也有说明[多态分为动态多态和静态多态](https://zh.wikipedia.org/wiki/%E5%A4%9A%E6%80%81_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6)) | ||
|
||
如图: | ||
|
||
![image-20231214103612773](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202312141036730.png) | ||
|
||
|
||
|
||
那么我们不妨理解为重载为静态动态(编译器决定)、重写为运行期决定的,为动态多态。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
> 什么是 Java 语言? | ||
> | ||
WL: | ||
|
||
Java 语言是一门面向对象的编程语言,不仅吸收了 C++ 语言的各种优点,还舍弃了 C++ 中难以理解的多继承以及指针的概念,因此 Java 语言功能强大且简单易用。Java 语言很好的实现了面向对象的思想,因此支持我们以优雅的思维方式进行复杂的编程。 | ||
|
||
> Java 语言的特点? | ||
> | ||
WL: | ||
|
||
1. 面向对象 | ||
2. 平台无关 | ||
3. 编译与解释并行 | ||
4. 支持多线程 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
> Java创建对象的几种方式? | ||
1. new | ||
|
||
2. 反射 | ||
|
||
3. clone | ||
|
||
4. 序列化 | ||
|
||
|
||
|
||
> 怎么理解反射? | ||
通过new来创建对象就是正射,是在编译时就会确定创建的对象类型;而反射就是动态地获取类信息、构造器进而`newInstance`创建对象的过程。 | ||
|
||
> 怎么通过反射来创建一个对象? | ||
无参实例化: | ||
|
||
`Object obj = Class.forName(类名).getConstructor().newInstance();` | ||
|
||
有参实例化: | ||
|
||
`Object obj = Class.forName(类名).getConstructor(String.class).newInstance("汪汪");` | ||
|
||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
try { | ||
// 获取Dog类的Class对象 | ||
Class<?> dogClass = Class.forName("Dog"); | ||
|
||
// 获取Dog类的构造器 | ||
Constructor<?> dogConstructor = dogClass.getConstructor(); | ||
|
||
// 通过构造器创建Dog对象 | ||
Object dog = dogConstructor.newInstance(); | ||
|
||
// 如果需要初始化参数,可以使用带有参数的构造函数 | ||
Constructor<?> dogConstructorWithParams = dogClass.getConstructor(String.class); | ||
Object dogWithName = dogConstructorWithParams.newInstance("旺财"); | ||
|
||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
class Dog { | ||
private String name; | ||
|
||
public Dog() { | ||
name = "小黄学长"; | ||
} | ||
|
||
public Dog(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
``` | ||
|
||
## | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
> 说说你对 HashMap 数据结构的理解? | ||
首先,hashmap 的数据结构是基于数组和链表的,如图: | ||
|
||
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202312070046177.png) | ||
|
||
so,既然是基于数组和链表的,那就说明数组和链表的特点也就是 HashMap 的特点: | ||
|
||
**数组:寻址快,直接根据索引访问元素,插入和删除慢;** | ||
|
||
**链表:寻址慢,需要从头节点开始遍历,插入和删除快。** | ||
|
||
|
||
|
||
说到 HashMap 就要说到 Java 8 了,Java 8 之前,HashMap 使用一个数组加链表的结构来存储 【K,V】 键值对。 | ||
|
||
如果发生 hash 冲突,那么 | ||
|
||
这将导致在处理 hash 冲突的时候性能不高,尤其是链表很长的时候。因此,Java 8 中的 HashMap 引入了红黑树来替代链表,这样当链表变长的时候,会自动转换为红黑树,从而提高了增删改查的性能。 | ||
|
||
|
||
|
||
> 什么是 Hash 冲突?怎么解决? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters