java 我无法重写toString方法[duplicate]

epfja78i  于 2023-01-15  发布在  Java
关注(0)|答案(3)|浏览(178)
    • 此问题在此处已有答案**:

Java generics - The type parameter String is hiding the type String(1个答案)
1小时前关闭。
你好我有两个Java类"List"和"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());
    
    }

}

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

yquaqz18

yquaqz181#

这是因为你的泛型类型叫做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)。引入一个隐藏类名的泛型参数从来都不是一个好主意。

hrirmatl

hrirmatl2#

当使用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类型。

rsl1atfo

rsl1atfo3#

代码中声明了List<String>类,这在语法上类似于声明一个List<T>类,其中“T”称为“String”。
所以当你试图重写的时候编译器会看到这个:
公共T到字符串()
此外,在toString方法的实现中,使用+=非常低效,它使用StringBuilder代替。

相关问题