Reputation: 924
i am new to java and i have a simple question and i have this code
public abstract class Seq { int a, b, c;}
class For extends Seq{
public For( int first, int last, int step )
{
a=first;
b=last;
c=step;
}
and in the main i have:
For r1= new For(3, 4, 5);
what i want to do is that i want to add this statement in the main System.out.print(r1);
and i want this statement to print "your parameters: 3, 4, 5"
how can i do this?
Upvotes: 1
Views: 1747
Reputation: 14791
You can do this by implementing the For
class' toString
method:
public String toString ( )
{
return "your parameters: " + a + ", " + b + ", " + c;
}
Upvotes: 2
Reputation: 4190
You would have to override the toString()
method and create another one that prints out the contents of r1
.
@Override
public String toString(){
return a + ", " + b + ", " + c;
}
Then it is simply:
System.out.println("Your parameters: " + r1.toString());
Upvotes: 0
Reputation: 34367
Add a toString
method in For
as:
@Override
public String toString(){
return "your parameters: "+ a + ", "+ b+ ", "+c;
}
Upvotes: 4