简体   繁体   English

Java 和 object 中的堆栈无法转换为 java.lang.String

[英]Stack in Java and object cannot be converted to java.lang.String

How can I reference to this piece of code?如何引用这段代码?

    class Example
{
    public static void main (String[] args) throws java.lang.Exception
    {

        Person thePerson1 = new Person("Name1", "Surname1", 1901);
        Person thePerson2 = new Person("Name2", "Surname2", 1901);
        Person thePerson3 = new Person("Name3", "Surname3", 1901);
        Person thePerson4 = new Person("Name4", "Surname4", 1901);
        Person thePerson5 = new Person("Name5", "Surname5", 1901);

        Stack<String> thestack = new Stack<String>();
        thestack.push(thePerson1);
        thestack.push(thePerson2);
        thestack.push(thePerson3);
    }
}

I have an issue: incompatible types: Person cannot be converted to java.我有一个问题:不兼容的类型:Person 无法转换为 java。 lang.朗。 String, line 56 Some messages have been simplified;字符串,第 56 行 一些消息已被简化; recompile with -Xdiags: verbose to get full output, line -1使用 -Xdiags 重新编译:详细以获取完整的 output,第 -1 行

How to change the type of the Stack If I want to use thePerson1, thePerson2,etc?如果我想使用 thePerson1、thePerson2 等,如何更改堆栈的类型?

You should define it as a Stack<Person> , not a Stack<String> :您应该将其定义为Stack<Person> ,而不是Stack<String>

Stack<Person> thestack = new Stack<Person>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM