如果不是informati = tom.toString();而是informati.toString();呢
时间: 2023-07-26 11:37:11 浏览: 40
如果是 `informati.toString()`,由于 `informati` 是一个空字符串,调用 `toString()` 方法并不会返回有意义的信息,而会返回一个空字符串。
因此,如果将 `informati` 修改为以下代码:
```java
String informati = "";
informati.toString(); // 调用 toString() 方法,返回一个空字符串,并且不会对 informati 字符串本身造成影响
```
则 `informati` 的值仍然是空字符串。这样,无论后面如何修改 `informati` 的值,调用 `informati.toString()` 方法都只会返回空字符串,因为 `informati` 变量本身并没有被修改。
相关问题
请问下列语句1和语句2的information值分别是? class Student { int age; String name; public String toString(){ return "Student"; } } class Bachelor extends Student { String counselor;//辅导员 public String toString(){ return "Bachelor"; } } class Master extends Student { String supervisor;//硕士导师 public String toString(){ return "Master"; } } class Doctor extends Student { String supervisor;//博士导师 public String toString(){ return "Doctor"; } } public class Test { public static void main(String[] args) { Student tom=new Student(); Bachelor jack=new Bachelor(); Master rose=new Master(); Doctor jerry=new Doctor(); String informati""; tom=jack; informatitom.toString(); //语句1 tom=rose; informatitom.toString(); //语句2 } }
在语句1中,`informati`的值为"Bachelor",因为`tom`此时指向`jack`对象,而`jack`是`Bachelor`类的一个实例,所以调用`tom.toString()`实际上是调用`jack.toString()`,返回的是"Bachelor"。
在语句2中,`informati`的值为"Master",因为`tom`此时指向`rose`对象,而`rose`是`Master`类的一个实例,所以调用`tom.toString()`实际上是调用`rose.toString()`,返回的是"Master"。
可以帮我在main()中用注释详细解释吗
当然可以,下面是详细的解释:
```java
public class Test {
public static void main(String[] args) {
Student tom = new Student(); // 创建一个 Student 类型的对象 tom
Bachelor jack = new Bachelor(); // 创建一个 Bachelor 类型的对象 jack
Master rose = new Master(); // 创建一个 Master 类型的对象 rose
Doctor jerry = new Doctor(); // 创建一个 Doctor 类型的对象 jerry
String informati = ""; // 定义一个空字符串,用来存储调用 toString() 方法返回的信息
tom = jack; // 将 tom 指向 jack 对象
informati = tom.toString(); // 调用 tom 对象的 toString() 方法,返回 "Bachelor",并存储到 informati 字符串中
System.out.println(informati); // 输出 informati 的值,即 "Bachelor"
tom = rose; // 将 tom 指向 rose 对象
informati = tom.toString(); // 调用 tom 对象的 toString() 方法,返回 "Master",并存储到 informati 字符串中
System.out.println(informati); // 输出 informati 的值,即 "Master"
}
}
```
在 `main()` 方法中,首先创建了一个 `Student` 类型的对象 `tom`,以及 `Bachelor`、`Master` 和 `Doctor` 类型的对象 `jack`、`rose` 和 `jerry`。
然后定义了一个空字符串 `informati`,用来存储调用 `toString()` 方法返回的信息。
接着将 `tom` 指向 `jack` 对象,调用 `tom.toString()` 方法返回 "Bachelor",并将其存储到 `informati` 字符串中,再将 `tom` 指向 `rose` 对象,调用 `tom.toString()` 方法返回 "Master",并将其存储到 `informati` 字符串中。
最后通过 `System.out.println()` 方法,输出 `informati` 的值,即 "Bachelor" 和 "Master"。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)