基本的打印输出问题使用构造函数

huangapple go评论83阅读模式
英文:

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

  1. 你在构造函数中什么也没有做。
  2. 如果你在不覆盖 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 + "]";
  }
  
  
}
英文:
  1. You are doing nothing in your constructor.
  2. 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.

huangapple
  • 本文由 发表于 2020年9月11日 14:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定