【发布时间】:2017-08-18 21:16:04
【问题描述】:
为什么我必须调用super.isEmpty()?我试过this.isEmpty(),但是调试过程暂停了。
import java.util.*;
public class Stack<e> extends ArrayList<e> {
/**
*
*/
private static final long serialVersionUID = 1L;
public void showPrompt(){
System.out.println("Please input informations five times and type Enter between each gap:");
}
// `enter code here`
public e peek(){
return get(size()-1);
}
public void push(e o){
add(o);
}
public e pop(){
e o=get(size()-1);
remove (size()-1);
return o;
}
public boolean isEmpty(){
return super.isEmpty();
/* Why did I have to invoke super.isEmpty()? */
}
public String toString(){
return "stack"+toString();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<String> s=new Stack<>();
s.showPrompt();
Scanner scan=new Scanner(System.in);
for(int i=0;i<5;i++){
s.push(scan.next());
}
System.out.println("Reverse order is:");
while(!s.isEmpty()){
System.out.println(s.pop());
}
}
}
【问题讨论】:
-
您是在问为什么
super是必要的,或者为什么this不起作用? -
是的,我就是这个意思
-
这是两个问题。你问的是哪一个?
-
另外,请仔细阅读minimal reproducible example 发布代码指南 - 您不需要所有代码来询问任何一个问题(这两个问题都是许多现有问题的重复)
-
我的意思是为什么不使用 'this' 或什么都不使用,而是使用 'super'。?关于 superclass 的业务是什么?