所有人
我有问题〜
在main方法中,int size = s.getSize()
但是当我更改为int size = s.size()
为什么编译器显示NoSuchMethodError?
Mystack是ArrayList的子类
为什么我不能使用size()
方法?
package ch11;
import java.util.ArrayList;
import java.util.Scanner;
public class ch11_10 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MyStack s = getStack(input);
int size = s.getSize();//換成s.size()
for (int i = 0; i < size; i++) {
System.out.println(s.pop());
}
}
public static MyStack getStack(Scanner input) {
System.out.print("Enter five strings: ");
MyStack s = new MyStack();
for (int i = 0; i < 5; i++) {
s.push(input.next());
}
return s;
}
}
class MyStack extends ArrayList {
public int getSize() {
return size();
}
public Object peek() {
return get(size() - 1);
}
public Object pop() {
Object o = get(size() - 1);
remove(size() - 1);
return o;
}
public void push(Object o) {
add(o);
}
@Override
public String toString() {
return "stack: " + toString();
}
}