英文:
Basic print out problem using constructors
问题
我似乎无法打印出我分配给cookieShop的值。
期望输出:
cookies123.75
实际输出:
Store@2aae9190
我是初学者。谢谢。
英文:
public class Store {
public Store(String product, int count, double price) {
}
public static void main(String[] args) {
Store cookieShop = new Store("cookies", 12, 3.75);
System.out.println(cookieShop);
}
}
I can't seem to print out the values I assigned to cookieShop.
Expected output:
cookies123.75
Output:
Store@2aae9190
>Beginner here. Thanks.
答案1
得分: 1
- 你在构造函数中什么也没有做。
- 如果你在不覆盖
toString()
方法的情况下打印这个对象,你会得到一个十六进制表示。做类似以下的操作:
public class Store {
String product;
int count;
double price;
public Store(String product, int count, double price) {
this.product=product;
this.count=count;
this.price=price;
}
public static void main(String[] args) {
Store cookieShop = new Store("cookies", 12, 3.75);
System.out.println(cookieShop);
}
@Override
public String toString() {
return "Store [product=" + product + ", count=" + count + ", price=" + price + "]";
}
}
英文:
- You are doing nothing in your constructor.
- If you print this object without overriding the toString(), you will get a hexadecimal representation.
Do something like below
public class Store {
String product;
int count;
double price;
public Store(String product, int count, double price) {
this.product=product;
this.count=count;
this.price=price;
}
public static void main(String[] args) {
Store cookieShop = new Store("cookies", 12, 3.75);
System.out.println(cookieShop);
}
@Override
public String toString() {
return "Store [product=" + product + ", count=" + count + ", price=" + price + "]";
}
}
答案2
得分: 0
你好,这是因为您从未在构造函数中分配输入变量。尝试定义一些类变量(private String product; private int count; private double price),并在构造函数中将输入分配给类变量。然后为类变量创建getter和setter方法,并覆盖toString()方法(这些操作可以轻松委托给您的IDE,例如Eclipse或Intellij)。
重新运行程序,然后查看发生了什么。
英文:
Hi it's beacause you never assing the input varabiables in the constructor. Try to define some class variables (private String product;private int count;private double price) and in the constructor assign the input to the class varabiables. Than create getter and setter method for the class variables and override toString() method (this operations can be easily delegated to youd IDE i.e Eclipse or Intellij).
Rerun the program and see what happens.
答案3
得分: -2
你需要为你的Store类定义一个toString方法。默认情况下,如果在你的类中未重写,所有对象都将使用java.lang.Object类的toString方法(https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--),该方法只是打印类的名称和十六进制表示:
getClass().getName() + '@' + Integer.toHexString(hashCode())
尝试类似这样的方法(使用你自己的字段名称):
@Override
public String toString() {
return name + count + price;
}
还可以考虑使用类似lombok的库,它可以通过添加相应的注解使所有这些变得非常简单。
英文:
You need to define a toString method for your Store class. By default, all objects are using the toString method from java.lang.Object (https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) class if it's not overridden in your class, which simply prints the name of the class and the hexadecimal representation:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Try something like this (using your own field names):
@Override
public String toString() {
return name + count + price;
}
Consider also using libraries like lombok, which makes all of that stuff really easy, simply adding the corresponding annotation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论