OOP AP CS 练习
我正在准备 AP CS 考试,并在我的书的 OOP 部分遇到了这个练习题。 给出以下两个类。
package chap4q9;
public class Person
{
private int age;
public Person(int a)
{
age = a;
}
public String toString()
{
return "Age: " + age + "\n";
}
}
package chap4q9;
public class Student extends Person
{
private double gpa;
public Student(int a, double g)
{
super(a);
gpa = g;
}
public String toString()
{
return super.toString() + "GPA: " + gpa; //This was where the missing code was
}
}
下面是调用这两个类的客户端程序。
package chap4q9;
public class Chap4Q9
{
public static void main(String[] args)
{
Student kathy = new Student(17, 3.85);
System.out.println(kathy);
}
}
最后,输出为:
Age: 17
GPA: 3.85
以防万一您想知道,输出中的 Age 和 GPA 之间实际上不应该有一条线,当我发布此内容时,这是一种奇怪的格式设置。
目标是替换第二个 toString 方法中缺失的代码(在上面的代码中,为缺失的代码插入了正确的答案,但我标记了位置)。我认为这本书是错的,但运行代码并得到相同的输出。我认为它只会打印 kathy 所在的内存位置,如果您想获得该输出,则必须打印 kathy.toString()。但是,仅打印 kathy 似乎正在运行 Student 类中的 toString 方法。我的问题是,为什么要打印 Student 对象 kathy 来获取该输出,而不仅仅是一个内存位置。 预先感谢所有回复。
I am studying for the AP CS Exam, and came across this practice problem in the OOP section of my book.
The following two classes are given.
package chap4q9;
public class Person
{
private int age;
public Person(int a)
{
age = a;
}
public String toString()
{
return "Age: " + age + "\n";
}
}
package chap4q9;
public class Student extends Person
{
private double gpa;
public Student(int a, double g)
{
super(a);
gpa = g;
}
public String toString()
{
return super.toString() + "GPA: " + gpa; //This was where the missing code was
}
}
And the following is the client program that calls these two classes.
package chap4q9;
public class Chap4Q9
{
public static void main(String[] args)
{
Student kathy = new Student(17, 3.85);
System.out.println(kathy);
}
}
Finally, the output is:
Age: 17
GPA: 3.85
Just in case you were wondering, there is not actually supposed to be a line between the Age and GPA in the output, that was a weird formatting thing when I posted this.
The goal was to replace missing code in the second toString method (in the code above, the correct answer was inserted for the missing code, but I marked the location). I thought the book was wrong, but ran the code and got the same output. I thought that it would simply print the memory location that kathy was located at, and if you wanted to get that output, you would have to print kathy.toString(). However, just printing kathy seems to be running the toString method in the Student class. My question is, why is printing the Student object, kathy, getting that output, and not simply a memory location.
Thanks in advance for all responses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那只是因为 System.out.println 默认情况下调用对象的 toString() 方法。在您的情况下,您已经在子类中实现了自己的 toString,因此使用了它。
仅当您的 Person 和 Student 类中没有 toString 时,才会调用 Object 类中的 toString ,它会打印一个对象标识字符串,其中包含类名和对象的十六进制表示形式哈希码。
有关更多详细信息,请参阅对象类的 javadoc: http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString()
Well that's just because System.out.println by default calls the toString()-method of an object. In your case you have implemented your own toString in the child class, so this is used.
Only if you wouldn't have a toString in your Person and in your Student class, the toString from the Object-class would be called, which prints an object identification string, which consists of the class-name and the hexadecimal representation of the object's hashCode.
See the javadoc for the Object-class for more details: http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString()
println(Object)
默认调用String.valueOf(...)
方法。看看:
http:// /download.oracle.com/javase/1.4.2/docs/api/java/io/PrintStream.html#print(java.lang.Object)
和
http:// /download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#valueOf(java.lang.Object)
println(Object)
per default calls theString.valueOf(...)
method.Have a look at:
http://download.oracle.com/javase/1.4.2/docs/api/java/io/PrintStream.html#print(java.lang.Object)
and
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#valueOf(java.lang.Object)
我认为在书中的某个地方,它说在执行 System.out.println 时,它会自动调用 toString 方法(如果有)。
I think in the book somewhere, it says that when doing System.out.println, it automatically calls the toString method if there's one.
您已经在两个类中重写了 toString() - 您到底为什么认为会调用标准 Object toString() 方法?
You've overriden toString() in both classes - why on earth do you think the standard Object toString() method would be called?