写在开头
今天完成了HW4。本次作业主要考查了:
- 链表的概念,及其基本操作;
- 父类(superclass)和子类(subclass)概念。(subclass extends superclass)
其中涉及到:
- 父类、子类中变量(instance variable、reference variable)、method的相互关系。(继承,调用,override)
- 子类override父类的method。
- 在子类中对父类的变量和方法进行调用。
- Stack、Heap的概念。
- Java变量储存、调用。
- 类运行
链表的概念
目前遇到的链表结构分为三种:
- Singly Linked Lists
- Circularly Linked Lists
- Doubly Linked Lists
其中主要参数包括:
- DList:
- head - DListNode
- size - int
- DListNode:
- item - Object
- prev - DListNode
- next - DListNode
Constructor:
// construct a new empty list //
public DList() {
head = newNode(null, null, null);
head.setNext(head);
head.setPrev(head);
}
主要操作包括:
public boolean isEmpty() {
public int length() {
public void insertFront(Object item) {
public void insertBack(Object item) {
public void insertAfter(Object item, DListNode node) {
public void insertBefore(Object item, DListNode node) {
public void remove(DListNode node) {
public DListNode front() { // addFirst
public DListNode back() { // addLast
public DListNode next(DListNode node) {
public DListNode prev(DListNode node) {
public boolean checkinList(DListNode node){
……
父类和子类
A subclass extends a superclass. 在父类和子类的inherit and override relation中(主要就是instance variables 和methods),记住三句话:
- A subclass inherits all public instance variables and methods of the superclass. (not inherit the private instance variables and methods of the superclass.)
子类继承父类的所有public/protected instance variables and methods。
- Inherited methods can be overridden.
instance variable CANNOT be overridden(though can be redefined in the subclass, but it might cause problems.)
子类可以override父类的method,但是最好不要动instance variable
- Inheritance hierarchy validation: IS-A 父子类关系
通常在subclass中 ,我们通过override method来给这个子类一个更加具体(specific)method。定义的方式同父类;而对于其他未被override的父类方法,一切照旧。
package list;
public class LockDListNode extends DListNode{
protected boolean lockornot; // 加入了一个新的boolean变量
// constructor
LockDListNode(Object i, DListNode p, DListNode n) {
super(i,p,n);
// 构造函数照旧(DListNode的),但是建成的node是LockDListNode类型的了,并且多了一个属性 - lockornot
// lockornot = false; // default类型就是false
}
}
比如在lockListNode例子中,lockListNode是ListNode的子类。(见上⬆️)
我们重新写了LockListNode中的remove method,在call remove method使首先判断:
-如果“locked”(lockornot = true),调用LockListNode中的新的remove method(not remove);
-如果“unlocked”(lockornot = false),调用super.remove() - DListNode中的remove method把它移走。
此处,在子类中如果希望调用父类中的原方法(未被override的版本),使用“super.方法名()”的方式调用。
(* 存疑1:abstract class/ interface 概念中,subclass must implement all upper class method;
abstract method/class has no body;
- 解答存疑1:INTERFACE
- A class that implements an Interface MUST implement ALL the methods of that interface.
- ALL interface method:
- Public and Abstract (implicitly)
- MUST :
- end in ";"
- no body
Example:
interface Nose{ // interface Nose - public && abstract (implicitly)【?】 public int iMethod(); // iMethod 是定义在interface里面的method,所以他也是public && abstract } 记住,所有**abstract** class/interface里面的method - abstract,下面遇见的第一个concrete class 一定要declare 这些abstract method
)
Stack与Heap
先重温一下书上的定义:(p236)
The Stack:
Where method invocations and local variables live.
____________
| go() |
|___________|
| doStuff()|
|___________|
|main() |
|___________|
(The method on the top of the stack is always the currently- executing one.)
Two kinds(not type) of variables we care:
instance variable, and local variable(stack variable).
(By type we mean: primitive/object reference)
The Heap:
Where ALL objects live. 也就是这些Objects里的variables住的地方。
(also known as "The Garbage-Collectibel Heap".)
- Duck object, Dog object, Button object.
(存疑2:*What about local variables that are object? * p238)
- 解答存疑2:
Local variables 是遥控器是遥控器是遥控器!!!是reference variable to an object, 自己不是objects!!!
Objects也不会到stacks上去,only the Reference/ remote control goes on the stacks. (object还是在heap上)
声明:本文中小写字母p标记页码为《Head FREMEirst Java 2nd edition》中相应页码;大写字母P标记页码为《Data Structure》中相应页码。