9
9
* [ 继承] ( #继承 )
10
10
* [ 1. 访问权限] ( #1-访问权限 )
11
11
* [ 2. 抽象类与接口的区别] ( #2-抽象类与接口的区别 )
12
- * [ 3. super() ] ( #3-super )
12
+ * [ 3. super] ( #3-super )
13
13
* [ String] ( #string )
14
14
* [ 1. String, StringBuffer and StringBuilder] ( #1-string,-stringbuffer-and-stringbuilder )
15
15
* [ 2. String 不可变的原因] ( #2-string-不可变的原因 )
@@ -114,23 +114,35 @@ public InitialOrderTest() {
114
114
115
115
## 1. 概览
116
116
117
- - public final native Class<?> getClass()
118
- - public native int hashCode()
119
- - public boolean equals(Object obj)
120
- - protected native Object clone() throws CloneNotSupportedException
121
- - public String toString()
122
- - public final native void notify()
123
- - public final native void notifyAll()
124
- - public final native void wait(long timeout) throws InterruptedException
125
- - public final void wait(long timeout, int nanos) throws InterruptedException
126
- - public final void wait() throws InterruptedException
127
- - protected void finalize() throws Throwable { }
117
+ ``` java
118
+ public final native Class<?> getClass()
119
+
120
+ public native int hashCode()
121
+
122
+ public boolean equals(Object obj)
123
+
124
+ protected native Object clone() throws CloneNotSupportedException
125
+
126
+ public String toString()
127
+
128
+ public final native void notify()
129
+
130
+ public final native void notifyAll()
131
+
132
+ public final native void wait(long timeout) throws InterruptedException
133
+
134
+ public final void wait(long timeout, int nanos) throws InterruptedException
135
+
136
+ public final void wait() throws InterruptedException
137
+
138
+ protected void finalize() throws Throwable {}
139
+ ```
128
140
129
141
## 2. clone()
130
142
131
143
** 浅拷贝**
132
144
133
- 引用类型引用的是同一个对象, clone() 方法默认就是浅拷贝实现。
145
+ 引用类型引用同一个对象。 clone() 方法默认就是浅拷贝实现。
134
146
135
147
<div align =" center " > <img src =" ../pics//d990c0e7-64d1-4ba3-8356-111bc91e53c5.png " /> </div ><br >
136
148
@@ -147,19 +159,19 @@ public InitialOrderTest() {
147
159
- 对于基本类型,== 就是判断两个值是否相等;
148
160
- 对于引用类型,== 是判断两个引用是否引用同一个对象,而 equals() 是判断引用的对象是否等价。
149
161
150
- 等价性:[ 散列 ] ( https://github.com/CyC2018/InterviewNotes /blob/master/notes/Java%20%E5%AE%B9%E5%99%A8.md#%E6%95%A3%E5%88%97 )
162
+ 等价性:[ 散列 ] ( https://github.com/CyC2018/Interview-Notebook /blob/master/notes/Java%20%E5%AE%B9%E5%99%A8.md#%E6%95%A3%E5%88%97 )
151
163
152
164
# 继承
153
165
154
166
## 1. 访问权限
155
167
156
168
Java 中有三个访问权限修饰符:private、protected 以及 public,如果不加访问修饰符,表示包级可见。
157
169
158
- 可以对类或类中的成员(字段以及方法)加上访问修饰符。成员可见表示其它类可以用成员所在类的对象访问到该成员;类可见表示其它类可以用这个类创建对象,可以把类当做包中的一个成员,然后包表示一个类,这样就好理解了 。
170
+ 可以对类或类中的成员(字段以及方法)加上访问修饰符。成员可见表示其它类可以用成员所在类的对象访问到该成员;类可见表示其它类可以用这个类创建对象。在理解类的可见性时 ,可以把类当做包中的一个成员,然后包表示一个类,那么就可以类比成员的可见性 。
159
171
160
172
protected 用于修饰成员,表示在继承体系中成员对于子类可见。但是这个访问修饰符对于类没有意义,因为包没有继承体系。
161
173
162
- 更详细的内容:[ 浅析 Java 中的访问权限控制 ] ( http://www.importnew.com/18097.html )
174
+ 更详细的内容:[ 浅析 Java 中的访问权限控制] ( http://www.importnew.com/18097.html )
163
175
164
176
## 2. 抽象类与接口的区别
165
177
@@ -177,7 +189,7 @@ public abstract class GenericServlet implements Servlet, ServletConfig, Serializ
177
189
}
178
190
```
179
191
180
- 接口定义了一组方法,但是接口都没有方法的实现,也就是说这些方法都是抽象方法 。
192
+ 接口定义了一组方法,但是接口都没有方法的实现,可以理解为这些方法都是抽象方法 。
181
193
182
194
``` java
183
195
public interface Externalizable extends Serializable {
@@ -188,26 +200,47 @@ public interface Externalizable extends Serializable {
188
200
}
189
201
```
190
202
191
- 更详细的内容:[ Java 抽象类与接口的区别 ] ( http://www.importnew.com/12399.html )
203
+ 更详细的内容:[ Java 抽象类与接口的区别] ( http://www.importnew.com/12399.html )
192
204
193
- ## 3. super()
205
+ ## 3. super
194
206
195
- 用来访问父类的构造函数父类的方法,第二种情况中,子类需要重载父类的方法。
207
+ ** 访问父类的成员**
208
+
209
+ 如果子类覆盖了父类的中某个方法的实现,那么就可以通过使用 super 关键字来引用父类的方法实现。
210
+
211
+ ``` java
212
+ public class Superclass {
213
+ public void printMethod () {
214
+ System . out. println(" Printed in Superclass." );
215
+ }
216
+ }
217
+ ```
196
218
197
219
``` java
198
220
public class Subclass extends Superclass {
199
- // overrides printMethod in Superclass
221
+ // Overrides printMethod in Superclass
200
222
public void printMethod () {
201
223
super . printMethod();
202
224
System . out. println(" Printed in Subclass" );
203
225
}
204
226
public static void main (String [] args ) {
205
227
Subclass s = new Subclass();
206
- s.printMethod();
228
+ s.printMethod();
207
229
}
208
230
}
209
231
```
210
232
233
+ ** 访问父类的构造函数**
234
+
235
+ 可以使用 super () 函数访问父类的构造函数,从而完成一些初始化的工作。
236
+
237
+ ```java
238
+ public MountainBike (int startHeight , int startCadence , int startSpeed , int startGear ) {
239
+ super (startCadence, startSpeed, startGear);
240
+ seatHeight = startHeight;
241
+ }
242
+ ```
243
+
211
244
> [Using the Keyword super](https:// docs.oracle.com/javase/tutorial/java/IandI/super.html)
212
245
213
246
# String
0 commit comments