我无法覆盖字符串方法



你好,我有两个java类"List"one_answers";ListPlayground"。问题是我不能重写toString()方法,因为我得到这个错误:

error: toString() in List cannot override toString() in Object
public String toString() {
^
return type String is not compatible with java.lang.String
where String is a type-variable:
String extends Object declared in class List
List.java:59: error: incompatible types: java.lang.String cannot be converted to String
String string = "";
^
where String is a type-variable:
String extends Object declared in class List
Note: List.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

这是我的类

public class List<String> {
private class Node {
private String element = null;
private Node next = null;
private Node(String element, Node next) {
this.element = element;
this.next = next;
}
private Node(String element) {
this.element = element;
}
}

private Node head = null;
private Node current = head;

public void prepend(String object) {
head = new Node(object, head);
}

public void append(String object) {
if(head == null) {
head = new Node(object);
return;
}
Node current = head;
while(current.next != null) {
current = current.next;
}
current.next = new Node(object);
}

public String first() {
get(0);
}

public String get(int index) {
Node current = head;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.element;
}

public int size() {
Node current = head;
int size = 0;
for(; current != null; size++) {
current = current.next;
}
return size;
}

public String toString() {
String string = "";
while(current != null) {
string += head.element + " -> ";
current = head.next;
}
return string;
}
}
下面是ListPlayground类:
public class ListPlayground {
public static void main(String[] args) {

List<String> stringliste = new List()<>;

stringliste.append("World");
stringliste.append("!");
stringliste.prepend("Hello");


System.out.println("The Length of the List is: " + stringliste.size());
System.out.println("The first Element of the List is: " + stringliste.first());
System.out.println("The element with Index 2 is: " + stringliste.get(2));
System.out.println("The last element is: " + stringliste.get(stringliste.size() - 1));
System.out.println("The whole List is: " + stringliste.toString());
System.out.println("And again the whole List " + stringliste.toString());

}
}

有人能帮我一下吗?我试着调试我的代码,但是没有成功。我知道类"对象"是所有类的超类,我必须重写toString()方法,但我不明白为什么toString()方法是错误的列表类?

这是因为您的泛型类型称为String,它遮蔽了java.lang.String类。它们都扩展了Object,但它们在本质上是不同的。

  1. 要解决这个问题,请将您的泛型参数调用为其他名称。例如:
public class List<T> {
private class Node {
private T element = null;
private Node next = null;
private Node(T element, Node next) {
this.element = element;
this.next = next;
}
private Node(T element) {
this.element = element;
}
}

private Node head = null;
private Node current = head;

public void prepend(T object) {
head = new Node(object, head);
}

public void append(T object) {
if(head == null) {
head = new Node(object);
return;
}
Node current = head;
while(current.next != null) {
current = current.next;
}
current.next = new Node(object);
}

public T first() {
get(0);
}

public T get(int index) {
Node current = head;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.element;
}

public int size() {
Node current = head;
int size = 0;
for(; current != null; size++) {
current = current.next;
}
return size;
}

public String toString() {
String string = "";
while(current != null) {
string += head.element + " -> ";
current = head.next;
}
return string;
}
}
  1. 或者您可以在toString()方法中指定要返回的字符串例如:
public java.lang.String toString() {
java.lang.String string = "";
while(current != null) {
string += head.element + " -> ";
current = head.next;
}
return string;
}

一般来说,我更倾向于解决方案1)而不是2)。引入一个泛型参数来掩盖类名从来都不是一个好主意。

使用public class List<String>时,"String"是通用类型,而不是java.lang.String

你应该改变你的类声明并删除你不需要的类型。

public class List {
... your code here
}

public class List<T>public class List<String>相同,在这两种情况下,它都是泛型类型,而不是java.lang.String类型。

在代码中声明了一个List<String>类,这在语法上类似于声明一个List<T>类,其中"T"叫做"String.

当你尝试重写时编译器会看到这样的内容:

public T toString()

同样在toString方法的实现中,使用+=是非常低效的,它使用StringBuilder来代替。

最新更新