944,000 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2470
  • Java RSS
Apr 4th, 2005
0

Help with overriding methods

Expand Post »
hi everyone

System.out.println(currentPlayer);

it returns to me like this:Player@7cbde6

i know it was caused by toString() and it is suggested to ues overriding method
but i don't know how to do it, so can somebody teach me how to use overiding method so System.out.println(currentPlayer); will display what i want

thank you very very much

drunkpanda
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drunkpanda is offline Offline
5 posts
since Apr 2005
Apr 4th, 2005
0

Re: Help with overriding methods

What's the signature of toString?
All you need to do is provide a method with that exact signature in your own class.

By reading the API docs for java.lang.Object you can find out the signature of toString.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 4th, 2005
0

Re: Help with overriding methods

thank you Jwenting
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drunkpanda is offline Offline
5 posts
since Apr 2005
Apr 5th, 2005
0

Re: Help with overriding methods

hellow,
all u hv to do is that override toString() method as one friend also told it, its signature is

public String toString(){
String str;
str = "any thing you want do it here, and place this method in ur class";
return str;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khoomy is offline Offline
4 posts
since Sep 2004
Apr 5th, 2005
0

Re: Help with overriding methods

Hi khoomy thanks

I have actually done something like this which works ok but what happen if i get mutiple String want to display each individually

Java Syntax (Toggle Plain Text)
  1. public String toString()
  2. {
  3. String thePlayer = currentPlayer.getName();
  4. return thePlayer;
  5. }

for example:
System.out.println(toString()); //which display player's name
System.out.println(somthing else);

Drunkpanda
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drunkpanda is offline Offline
5 posts
since Apr 2005
Apr 6th, 2005
0

Re: Help with overriding methods

Where do you get mutiple Strings ? Not clear with your example.
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
aj.wh.ca is offline Offline
53 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Confusion with constructors
Next Thread in Java Forum Timeline: I Need Help Please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC