java的类中调用tostring()方法为什么不能返回对应的值
时间: 2024-09-21 22:08:04 浏览: 35
在 Java 中,`toString()` 方法是一个对象转换成字符串的标准方法,它通常用于提供对对象的一种可读表示。当直接在类中调用 `toString()` 方法而没有显式地覆盖这个方法时,JVM 使用的是默认的、系统提供的实现。这个默认的实现通常是基于类名和内存地址,对于基本类型的值,它不会返回具体的数值,而是可能会转化为类似于 "MyClass@123456" 的形式。
如果你期望 `toString()` 返回特定的数据值,你需要在类中自定义这个方法,让它根据类的业务逻辑生成相应的字符串表示。例如:
```java
public class MyClass {
private int value;
// 自定义 toString 方法
@Override
public String toString() {
return "MyClass{" +
"value=" + value +
'}';
}
}
```
在这个例子中,当你创建 `MyClass` 对象并调用 `toString()`,会得到类似 "MyClass{value=123}" 的字符串,而不是原始整数值。
相关问题
编程定义一个汽车类Car。要求如下: (1)定义成员变量:品牌(String brand),价格(double price),颜色(String color),销售日期(Calendar date); (2)定义无参构造方法和带参构造方法,其中无参构造方法调用带参构造方法来初始化类中的变量; (3)定义上述成员变量所对应的getXxx()/setXxx()方法;以及一个显示所有成员信息的toString()方法; (4)定义一个测试类CarDemo,使用无参构造方法创建Car对象car1,调用toString()方法显示car1对象的信息并显示当前时间,使用带参构造方法创建Car对象car2,调用toString()方法显示car2对象的信息。java代码
```
import java.util.Calendar;
public class Car {
private String brand;
private double price;
private String color;
private Calendar date;
public Car() {
this("default", 0, "white", Calendar.getInstance());
}
public Car(String brand, double price, String color, Calendar date) {
this.brand = brand;
this.price = price;
this.color = color;
this.date = date;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Calendar getDate() {
return date;
}
public void setDate(Calendar date) {
this.date = date;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", color='" + color + '\'' +
", date=" + date.getTime() +
'}';
}
}
public class CarDemo {
public static void main(String[] args) {
Car car1 = new Car();
System.out.println(car1.toString() + " Current time: " + Calendar.getInstance().getTime());
Calendar date = Calendar.getInstance();
date.set(2021, Calendar.JANUARY, 1);
Car car2 = new Car("BMW", 1000000, "black", date);
System.out.println(car2.toString());
}
}
```
1)定义成员变量:品牌(String brand),价格(double price),颜色(String color),销售日期(Calendar date); (2)定义无参构造方法和带参构造方法,其中无参构造方法调用带参构造方法来初始化类中的变量; (3)定义上述成员变量所对应的getXxx()/setXxx()方法;以及一个显示所有成员信息的toString()方法; (4)定义一个测试类CarDemo,使用无参构造方法创建Car对象car1,调用toString()方法显示car1对象的信息,使用带参构造方法创建Car对象car2,调用toString()方法显示car2对象的信息
Car 类的实现如下:
```java
import java.util.Calendar;
public class Car {
private String brand;
private double price;
private String color;
private Calendar date;
public Car() {
this("Unknown", 0.0, "Unknown", Calendar.getInstance());
}
public Car(String brand, double price, String color, Calendar date) {
this.brand = brand;
this.price = price;
this.color = color;
this.date = date;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Calendar getDate() {
return date;
}
public void setDate(Calendar date) {
this.date = date;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", color='" + color + '\'' +
", date=" + date.getTime() +
'}';
}
}
```
CarDemo 类的实现如下:
```java
public class CarDemo {
public static void main(String[] args) {
Car car1 = new Car();
System.out.println(car1);
Calendar date = Calendar.getInstance();
date.set(2021, Calendar.JANUARY, 1);
Car car2 = new Car("Toyota", 25000.0, "Red", date);
System.out.println(car2);
}
}
```
输出结果如下:
```
Car{brand='Unknown', price=0.0, color='Unknown', date=Mon Oct 11 23:33:45 CST 2021}
Car{brand='Toyota', price=25000.0, color='Red', date=Fri Jan 01 00:00:00 CST 2021}
```
阅读全文