显示继承类中的对象数组 [英] Display array of objects from an inheriting class
问题描述
您好,
我试图显示一个对象数组,以便显示对象信息。但是我没有得到正确的结果。结果如下:
[LItem; @ 55f96302
[LItem; @ 55f96302
[LItem; @ 55f96302
[LItem; @ 55f96302
[LItem; @ 55f96302
物品等级:
private String title;
private String 分类;
private String 类型;
private int ID;
private int year;
private 日历dateLastReturned;
private boolean available;
private RentItem最新;
public 项目( int ID, int year, String title, String 分类,字符串类型)
{
此 .ID = ID;
此 .year = year;
此 .title = title;
此 .classification =分类;
此 .genre = genre;
dateLastReturned = null;
available = true;
latest = null;
}
public Item()
{
ID = 0 跨度>;
year = 0 ;
title = ;
分类= ;
genre = ;
available = true;
dateLastReturned = null;
latest = null;
}
public String getTitle(){
return title;
}
public void setTitle( String title){
this .title = title;
}
public String getClassification(){
返回分类;
}
public void setClassification(字符串分类){
此 .classification = classification;
}
public String getGenre(){
return 类型;
}
public void setGenre( String genre){
this .genre = genre;
}
public int getID(){
return ID;
}
public void setID( int iD){
ID = iD;
}
public int getYear(){
返回年;
}
public void setYear( int year){
this .year = year;
}
public 日历getDateLastReturned(){
return dateLastReturned;
}
public void setDateLastReturned(Calendar dateLastReturned){
this .dateLastReturned = dateLastReturned;
}
public boolean isAvailable(){
返回可用;
}
public void setAvailable( boolean available){
this .available = available;
}
public RentItem getLatest(){
return latest;
}
public void setLatest(RentItem latest){
此 .latest = latest;
}
public void returnItem(Calendar endDate)
{
setDateLastReturned(endDate);
available = true;
}
public String toString()
{
字符串 str = 跨度>;
str = str + ID: + ID + \ n + 年份: +年+ \ n + 标题: + title + \ n + 分类: +分类+ \ n + 类型: + genre + \ n;
str = str + 返回日期: + retDay + / + retMonth + / + retYear + \ n + 可用: + available + \\\
跨度>;
if (getLatest()!= null)
str = str + 最近租期: + \ t \ t\t\t + getLatest()。getReference();
return str;
}
DVD类(此类是Item类的子类):
private int numberOfDiscs;
private int filmRunTime;
private 字符串字幕;
public DVD( int ID, int year, String title, String 分类,字符串类型, int numberOfDiscs, int filmRunTime,字符串字幕)
{
this .numberOfDiscs = numberOfDiscs;
this .filmRunTime = filmRunTime;
此 .subtitles =字幕;
}
public DVD()
{
numberOfDiscs = 0 < /跨度>;
filmRunTime = 0 ;
subtitles = ;
}
public int getNumberOfDiscs(){
return numberOfDiscs;
}
public void setNumberOfDiscs( int numberOfDiscs){
this .numberOfDiscs = numberOfDiscs;
}
public int getFilmRunTime(){
返回 filmRunTime;
}
public void setFilmRunTime( int filmRunTime){
this .filmRunTime = filmRunTime;
}
public String getSubtitles(){
返回字幕;
}
public void setSubtitles(字符串字幕){
此 .subtitles =字幕;
}
@覆盖
public 字符串 toString(){
return DVD [numberOfDiscs = + numberOfDiscs + ,filmRunTime =
+ filmRunTime + ,subtitles = + subtitles + ,getTitle()=
+ getTitle()+ ,getClassification()= + getClassification()
+ ,getGenre()= + getGenre()+ ,getID ()= + getID()
+ ,getYear()= + getYear()+ ,getDateLastReturned()=
+ getDateLastReturned()+ ,isAvailable()= + isAvailable ()
+ ,getLatest()= + getLatest()+ ,toString()=
+ super .toString()+ ,getClass()= + getClass()
+ ,hashCode()= + hashCode()+ ];
}
我的尝试:
我尝试过以下方法:
静态 Item [] productStorage = new 项[ 2 ];
public static void initialise()
{
// 创建对象
DVD dvd1 = 新 DVD( 1 , 2010 , 快门群岛,< span class =code-string> 16, 神秘,惊悚, 1 , 138 , 法语,波兰语,英语) ;
DVD dvd2 = 新 DVD( 2 , 2006 , Prestige, 12, 戏剧,神秘, 1 , 130 , 英语,德语,法语,西班牙语);
// 为数组赋值
productStorage [< span class =code-digit> 0 ] = dvd1;
productStorage [ 1 ] = dvd2;
for ( int i = 0 ; i< productStorage.length; i ++)
{
System.out.println(productStorage);
}
}
然而它显示如下:
[LItem; @ 55f96302
[LItem; @ 55f96302
任何帮助将不胜感激
谢谢!
您的问题出在本声明中:引用:System.out.println(productStorage);
仔细看看周围的代码,你应该看看 i 指的是......
for(int i = 0; i< productStorage.length; i ++)
{
System。通过out.println(productStorage);
}
你能表明你得到了什么吗?
我认为你只是保留了你在构造函数中提供的三个值...
public DVD(int ID,int year,String title,String classification,String genre,int numberOfDiscs,int filmRunTime,String subtitles)
{
this.numberOfDiscs = numberOfDiscs;
this.filmRunTime = filmRunTime;
this.subtitles =字幕;
}
这意味着这些是唯一可以打印的内容......其余的(ID,年份标题,分类,类型)从未分配过,因此它们将为null / default值。
Hello,
I was trying to display an array of objects so that it will show object information. However I did not get the right result. The result looked like this:
[LItem;@55f96302
[LItem;@55f96302
[LItem;@55f96302
[LItem;@55f96302
[LItem;@55f96302
Item class:
private String title;
private String classification;
private String genre;
private int ID;
private int year;
private Calendar dateLastReturned;
private boolean available;
private RentItem latest;
public Item(int ID, int year, String title, String classification, String genre)
{
this.ID = ID;
this.year = year;
this.title = title;
this.classification = classification;
this.genre = genre;
dateLastReturned = null;
available = true;
latest = null;
}
public Item()
{
ID = 0;
year = 0;
title = "";
classification = "";
genre = "";
available = true;
dateLastReturned = null;
latest = null;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public Calendar getDateLastReturned() {
return dateLastReturned;
}
public void setDateLastReturned(Calendar dateLastReturned) {
this.dateLastReturned = dateLastReturned;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public RentItem getLatest() {
return latest;
}
public void setLatest(RentItem latest) {
this.latest = latest;
}
public void returnItem(Calendar endDate)
{
setDateLastReturned(endDate);
available = true;
}
public String toString()
{
String str = "";
str= str + "ID: "+ID + "\n"+"Year: "+year+"\n"+"Title: "+title+"\n"+"Classification: "+ classification+"\n"+"Genre: "+genre+"\n";
str = str +"Return Date: "+retDay+"/"+retMonth+"/"+retYear+"\n"+"Available: "+available+"\n";
if (getLatest() != null)
str = str + "Most Recent Rental:"+"\t\t\t\t" +getLatest().getReference();
return str;
}
DVD class(This class is the subclass of the Item class):
private int numberOfDiscs;
private int filmRunTime;
private String subtitles;
public DVD(int ID, int year, String title, String classification, String genre, int numberOfDiscs, int filmRunTime, String subtitles)
{
this.numberOfDiscs = numberOfDiscs;
this.filmRunTime = filmRunTime;
this.subtitles = subtitles;
}
public DVD()
{
numberOfDiscs = 0;
filmRunTime = 0;
subtitles = "";
}
public int getNumberOfDiscs() {
return numberOfDiscs;
}
public void setNumberOfDiscs(int numberOfDiscs) {
this.numberOfDiscs = numberOfDiscs;
}
public int getFilmRunTime() {
return filmRunTime;
}
public void setFilmRunTime(int filmRunTime) {
this.filmRunTime = filmRunTime;
}
public String getSubtitles() {
return subtitles;
}
public void setSubtitles(String subtitles) {
this.subtitles = subtitles;
}
@Override
public String toString() {
return "DVD [numberOfDiscs=" + numberOfDiscs + ", filmRunTime="
+ filmRunTime + ", subtitles=" + subtitles + ", getTitle()="
+ getTitle() + ", getClassification()=" + getClassification()
+ ", getGenre()=" + getGenre() + ", getID()=" + getID()
+ ", getYear()=" + getYear() + ", getDateLastReturned()="
+ getDateLastReturned() + ", isAvailable()=" + isAvailable()
+ ", getLatest()=" + getLatest() + ", toString()="
+ super.toString() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + "]";
}
What I have tried:
I have tried the following:
static Item[] productStorage= new Item[2];
public static void initialise()
{
//creating objects
DVD dvd1 = new DVD(1, 2010, "Shutter Islands", "16", "Mystery, Thriller", 1, 138, "French, Polish, English");
DVD dvd2 = new DVD(2, 2006, "Prestige", "12", "Drama, Mystery", 1, 130, "English, German, French, Spanish");
//Assigning value to the array
productStorage[0] = dvd1;
productStorage[1] = dvd2;
for(int i = 0; i < productStorage.length; i++)
{
System.out.println(productStorage);
}
}
However it displays it like that:
[LItem;@55f96302
[LItem;@55f96302
Any help will be appreciated
Thanks!
Your problem is in this statement:Quote:System.out.println(productStorage);
Look carefully at the code around it and you should see what i am referring to...
for(int i = 0; i < productStorage.length; i++) { System.out.println(productStorage); }
Can you show what you are getting?
What I think it is though is you are only keeping three of the values you are providing in the constructor...
public DVD(int ID, int year, String title, String classification, String genre, int numberOfDiscs, int filmRunTime, String subtitles) { this.numberOfDiscs = numberOfDiscs; this.filmRunTime = filmRunTime; this.subtitles = subtitles; }which means these are the only things that will print... the rest (ID, year title, classification, genre) never assigned so they will be null/default values.
这篇关于显示继承类中的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!