覆盖toString()或outprint对象值不起作用 [英] override toString(), or outprint object values not working

查看:82
本文介绍了覆盖toString()或outprint对象值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我只添加了这两个类,因为我在这里遇到了问题。但如果你需要检查其余的,我也会添加它们。所以我的问题是,我做了一个迷你网上商店,在那里我可以制作产品,在类别中添加这些产品等。问题来自Magazin(商店类),我需要返回/打印订单对象(在Magazin类的底部) ),在main方法中制作。我试图覆盖toString方法,并使用String.valueOf()方法,但我只得到内存位置。注意这些方法在outprinting类别时起作用。

Sry,如果你不理解代码中的单词,它是用我的语言写的,而且由于时间不够,我无法重写它们。但我改变了大部分内容并在需要的地方添加了注释。

可能是我的小错误,我略过了。如果有人能说出那是什么......(我不需要代码告诉我我的错误)。

提前致谢。





Hello,

I added only these two classes,because I have the problem here.But if you need to check the rest i will add those too.So my problem is, I make a mini online shop,where I can make products, add those products in categorys etc.The problem comes in Magazin(Shop class) where I need to return/outprint the order object(at the bottom the Magazin class), made in main method.I tried to override the toString method,and use the String.valueOf() method but I get only the memory location.Note that these methods worked at outprinting the category's.
Sry if you dont understand the words in the code, it was writen in my language,and in lack of time I couldn't rewrite them all.But i changed the most part and added comments where needed.
Probably its small error I skim over.If anyone cann tell what is that...(I dont need code just tell me my mistake).
Thanks in advance.


import java.util.ArrayList;

public class Order {

 <pre lang="xml">
    private ArrayList<Produs> produse;
    private String country;
    private String city;
    private String state;
    private int postalCode;

    public Comanda(String order, String country, String city, String state,
            int postalCode) {

        this.produse = produse;
        this.country = country;
        this.city = city;
        this.state = state;
        this.postalCode = postalCode;

    }


}</pre>







import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Magazin { // shop class

	private List<CategorieProdus> categorii = new ArrayList<CategorieProdus>();
	private List<Order> order = new ArrayList<Order>();

	public String afiseaza() {  // outprint category  -- using this and it works                                              
		return String.valueOf(categorii);
	}

	// @Override
	// public String toString() {
	//
	// return "\n" + categorii + ": \n";
	//
	// }

	public void adaugareCategorie(CategorieProdus... cat) {  // add category
		for (CategorieProdus s : cat) {
			categorii.add(s);
		}

	}

	public void stergeCategorie(CategorieProdus... sterge) {  // delete category
		for (CategorieProdus s : sterge) {
			if (categorii.contains(s)) {

				categorii.remove(s);

			}

		}

	}

	public void adaugareComanda(Order comanda) { // add order
		this.order.add(comanda);


	}

	public void stergeComanda() {

	}

	public String afisareComenzi() {  // outprint order!!!
		return String.valueOf(order);

	}

}
</pre>

推荐答案

您的 toString 方法没有实现可言,所以不生成任何字符串数据。您正在尝试返回列表,这将(很可能)只给您一个内存地址。您需要在此方法中实际编写一些代码以创建要显示的字符串。
Your toString method has no implementation to speak of, so is not producing any string data. You are trying to return a List which will (most likely) just give you a memory address. You need to actually write some code in this method to create the string that you want displayed.


这篇关于覆盖toString()或outprint对象值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆