[英]Why am I getting a BeanCreationException when trying to initialise a bean using the constructor-arg element
[英]I am getting unusual object reference codes and null values when I initialise a constructor
我有幾個關於如何設置類實例的問題。 如果我有一個對象構造函數,如下所示:
Object(String newName, ArrayList<Person> newPersonList){
name=newName;
personList=newPersonList;
System.out.println(personList);}
然后將其作為Object
類中的方法分配給一個人:
matchPersonToObject(Person person){
this.matchedPerson=person;
person.addToObjects(this); //do I need to add the full project.domain address in here?
//Because if so it will only let me put the class `Person` in rather than an instance `person`
}
//in the person class:
addToObjects(Object obj){
this.objectList.add(obj);
System.out.println(objectList);
}
然后,我將其初始化如下:
Person chris=new Person("Chris");
Object obj1=new Object("thing",new ArrayList<Person>(Arrays.asList(chris)))
它的輸出為:
[project.domain.Person@1a40fff] //personList in object constructor
[Object - null] //objectList once person has been matched to it
我的兩個問題是:a)給人實例的十六進制代碼是什么? 為什么不只顯示[project.domain.chris]? b)當我將對象添加到objectList時,為什么將此注冊為null? 我是否正確初始化了ArrayList?
第一:切勿使用您正在使用的語言的保留字來命名您的類,例如,避免創建名稱如Vector,Long等的類,從而在代碼中產生混淆。
第二:您正在查看的十六進制代碼是您的類Person的哈希碼,請覆蓋Person類的toString()方法。
public String toString() {
return <anything_you_want> + name;
}
可能會導致錯誤的包導入,因為您有一個帶有Name對象的類,而編譯器無法識別您的類,java.lang.Object與Object類不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.