package linked;
public class PlsWork {
private String name;
private int studentNo;
public PlsWork(String name, int studentNo){
this.name=name;
this.studentNo=studentNo;
}
public String getname(){
return name;
}
public int getstudentNo(){
return studentNo;
}
}
package linked;
import java.util.LinkedList;
public class Linked {
public static void main(String[] args) {
LinkedList myLinkedList = new LinkedList();
myLinkedList.addFirst("A");
System.out.println(myLinkedList);
PlsWork ok = new PlsWork("obinna",3);
myLinkedList.add(ok);
System.out.println(myLinkedList);
}
}
当我运行代码时,我得到以下答案 [A,linked.PlsWork@6d06d69c]
而不是[A,obinna 3]
答案 0 :(得分:0)
linked.PlsWork@6d06d69c
是对象的引用。这是toString
默认显示的内容。您可能希望覆盖toString
类的PlsWork
,例如:
class PlsWork {
...
@Override
public String toString() {
return name + ' ' + studentNo;
}
}