Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > method overriding

Reply
Thread Tools

method overriding

 
 
Ryan Stewart
Guest
Posts: n/a
 
      03-07-2004
"chris sennitt" <(E-Mail Removed)> wrote in message
news:y2G2c.575$(E-Mail Removed)...
>
> "Ryan Stewart" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "chris sennitt" <(E-Mail Removed)> wrote in message
> > news:kbF2c.2$(E-Mail Removed)...
> > > > If you want to be able to do this, could you not class cast the

object
> > > back to bClass?
> > > > ((bClass)b.aa()).bb()
> > > > That's the only way I think you can do what you want to do in one

> line.
> > > >
> > > lol - well the idea was to make the code concise and simple to read
> > >

> > A cast doesn't make code difficult to read, but chaining method calls

> does.
>
> i agree, chaining can make the code un readable, however in this case it
> works well.
>
> oHtmlText.bold().italic().font("blahblah").size(12 ).colour("blahblah");
>
> seems reasonably easy to read
>
> or casting as a work around .........
>
> ( (htmlText)( (htmlText) ( (htmlText) ( (htmlText)
>

oHtmlText.bold() ).italic() ).font("blahblah") ).size(12) ).colour("blahblah
> ");
>
> seems pretty confusing to me and , yes i may have missed some brackets
> somewhere but as u can see, casting is not an option here
>

Yeah, that's ugly. Is that what you're trying to do with Java? The
..bold().italic().... thing? Why don't you make a Style object that lets you
call those methods to set its state, then associate your oHtmlText and
whatever other objects with a Style object? Then the methods in your Style
class can each return a Style object and you get the effect you're looking
for. Something like:

public class HtmlText {
private Style style;
private String text;

public void setStyle(Style style) {
this.style = style;
}

public void setText(String text) {
this.text = text;
}

public static void main(String[] args) {
Style myStyle = new Style().bold().italic();
HtmlText ht = new HtmlText();
ht.setStyle(myStyle);
}

}

class Style {
private boolean bold = false;
private boolean italic = true;
// Other states

private Style bold() {
this.bold = true;
return this;
}

private Style italic() {
this.italic = true;
return this;
}
}

This code may have typos. I just typed it in here, don't have time to check
it right now. You should be able to get the general idea though.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding a class method with an object method Koszalek Opalek Perl Misc 8 07-06-2007 08:59 AM
I forget: what is the rule for method overriding? puzzlecracker Java 4 01-18-2006 04:15 PM
overriding method with different return type Paul J. Lucas Java 5 04-12-2004 09:45 AM
question about overriding method Allan Streib Python 0 10-29-2003 11:36 PM
Overriding finalize() method No R. Barranco Java 1 08-30-2003 09:05 AM



Advertisments