@Override public String toString()
时间: 2023-11-12 11:11:21 浏览: 190
这是一个 Java 语言中的方法,用于将对象转换为字符串并返回该字符串。通常情况下,这个方法会被重写,以便返回有意义的字符串描述对象的状态。例如:
```
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
```
在上面的例子中,我们重写了 `toString()` 方法,以便返回一个包含对象名称、姓名和年龄的字符串。这样就可以方便地打印出 Person 对象的信息了:
```
Person p = new Person("Alice", 25);
System.out.println(p); // 输出:Person{name='Alice', age=25}
```
相关问题
class Contact { protected String name; protected String phone; protected String remark; public Contact(String name, String phone, String remark) { this.name = name; this.phone = phone; this.remark = remark; } public void display() { System.out.println(toString()); } @Override public String toString() { return "姓名:" + name + ",电话:" + phone + ",备注:" + remark; } } class Family extends Contact { private String address; private String homePhone; public Family(String name, String phone, String remark, String address, String homePhone) { super(name, phone, remark); this.address = address; this.homePhone = homePhone; } @Override public void display() { System.out.println(toString()); } @Override public String toString() { return super.toString() + ",地址:" + address + ",固定电话:" + homePhone; } } class Friend extends Contact { private String category; public Friend(String name, String phone, String remark, String category) { super(name, phone, remark); this.category = category; } @Override public void display() { System.out.println(toString()); } @Override public String toString() { return super.toString() + ",类别:" + category; } } class WorkContact extends Contact { private String company; private String department; private String position; public WorkContact(String name, String phone, String remark, String company, String department, String position) { super(name, phone, remark); this.company = company; this.department = department; this.position = position; } @Override public void display() { System.out.println(toString()); } @Override public String toString() { return super.toString() + ",公司:" + company + ",部门:" + department + ",职务:" + position; } }此程序的UML图怎么画
以下是此程序的UML图:
```
+------------------+
| Contact |
+------------------+
| - name: String |
| - phone: String |
| - remark: String |
+------------------+
| + Contact(name: String, phone: String, remark: String) |
| + display(): void |
| + toString(): String |
+------------------+
^
|
|
+-------+-------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Family | +-----------+
| | | WorkContact|
| | +-----------+
| | ^
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+---------------+ |
| - address: String |
| - homePhone: String |
+---------------+ |
| + Family(name: String, phone: String, remark: String, address: String, homePhone: String) |
| + display(): void |
| + toString(): String |
+---------------+ |
|
|
|
|
|
|
|
|
|
|
|
|
+--------+--------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Friend |
| |
+---------------+
| - category: String |
+---------------+
| + Friend(name: String, phone: String, remark: String, category: String) |
| + display(): void |
| + toString(): String |
+---------------+
```
@Override public String toString() { return cases +"\t"+ deaths; }
这是Java中的一个重写toString()方法,它的作用是将该对象的属性cases和deaths转化为String类型并返回。在该方法中,将属性cases和deaths转换成字符串并用制表符"\t"分隔,然后将它们拼接成一个字符串并返回。如果需要在程序中输出该对象的字符串形式,就可以直接调用该方法。
阅读全文