我怎样才能改变这个使用超类的方法的输出
how can I change the output of this method which uses a superclass
嗨,我想知道如何更改显示方法的输出,以便输出,目前看起来是这样的:
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
可以做成这样
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
40 seconds ago - 2 people like this.
No comments.
就是这两个方法,负责超类中的部分输出
public String toString()
{
String text = username + "\n" + timeString(timestamp);
if(likes > 0) {
text+= " - " + likes + " people like this.\n";
}
else {
text+= "\n";
}
if(comments.isEmpty()) {
return text + " No comments.\n";
}
else {
return text + " " + comments.size() +
" comment(s). Click here to view.\n";
}
}
public void display()
{
System.out.println(toString());
}
这是子类中的两个方法,通过调用上面的父类完成输出
public String toString()
{
return super.toString() + message + "\n";
}
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
System.out.println(toString());
}
这是您期望的结果:
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
40 seconds ago - 2 people like this.
No comments.
message
的值为:
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
而 super.toString()
的值:
40 seconds ago - 2 people like this.
No comments.
你的子类 toString() returns 这个:
public String toString()
{
return super.toString() + message + "\n";
}
你的结果是:super.toString():
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
和message
:
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
你明白为什么要按这个顺序打印了吗?
您只需将顺序更改为:
return message + super.toString() + "\n";
这将更改它的显示顺序。
并在变量 message
中打印 super.toString()
中的 username
的值
您可以为 类 中的每个字符串创建单独的方法,并在构建您的最终字符串时单独调用它们。
例如:
在超类中:
public String getUsername() {
return username;
}
public String getTimeString() {
return timeString(timestamp);
}
在子类中:
public void display() {
System.out.println(super.getTimeString()+"\n"+message);
//or put whatever in whatever order you want.
}
嗨,我想知道如何更改显示方法的输出,以便输出,目前看起来是这样的:
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
可以做成这样
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
40 seconds ago - 2 people like this.
No comments.
就是这两个方法,负责超类中的部分输出
public String toString()
{
String text = username + "\n" + timeString(timestamp);
if(likes > 0) {
text+= " - " + likes + " people like this.\n";
}
else {
text+= "\n";
}
if(comments.isEmpty()) {
return text + " No comments.\n";
}
else {
return text + " " + comments.size() +
" comment(s). Click here to view.\n";
}
}
public void display()
{
System.out.println(toString());
}
这是子类中的两个方法,通过调用上面的父类完成输出
public String toString()
{
return super.toString() + message + "\n";
}
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
System.out.println(toString());
}
这是您期望的结果:
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
40 seconds ago - 2 people like this.
No comments.
message
的值为:
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
而 super.toString()
的值:
40 seconds ago - 2 people like this.
No comments.
你的子类 toString() returns 这个:
public String toString()
{
return super.toString() + message + "\n";
}
你的结果是:super.toString():
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
和message
:
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
你明白为什么要按这个顺序打印了吗? 您只需将顺序更改为:
return message + super.toString() + "\n";
这将更改它的显示顺序。
并在变量 message
super.toString()
中的 username
的值
您可以为 类 中的每个字符串创建单独的方法,并在构建您的最终字符串时单独调用它们。
例如:
在超类中:
public String getUsername() {
return username;
}
public String getTimeString() {
return timeString(timestamp);
}
在子类中:
public void display() {
System.out.println(super.getTimeString()+"\n"+message);
//or put whatever in whatever order you want.
}