Article View: borland.public.jbuilder.java.language
Article #7178Re: Interfaces and keyword THIS
From: Ronald Wildenberg
Date: Mon, 09 Jul 2001 07:13
Date: Mon, 09 Jul 2001 07:13
66 lines
2763 bytes
2763 bytes
Hi Dick,
This is not so much a question about the keyword 'this' but about
passing references in general.
The general rule is that the value of an actual parameters (like 'this'
in the example) must be assignable to the formal parameter
('MouseListener l' in the example). Since you can say something
like:
MouseListener ml = this;
the call to the method addMouseListener is legal.
Now for the example again. 'this' implements the MouseListener
interface. Because of this, the compiler forces you to implement
the five methods in this interface. Since 'this' is a MouseListener,
you can pass it as a parameter to the addMouseListener method.
This method expects something of type MouseListener.
Hope this helps,
Ronald.
Dick Gerrits wrote:
> Hello jbuilder.java.language people,
>
> Question about keyword "this". As I understood is the keyword this a
> reference to the current object.
> Suppose I extend a class from JApplet and implement the MouseListener
> interface like:
>
> public class Applet1 extends JApplet implements MouseListener{
> .....
> addMouseListener(this);
>
> public void MousePressed(MouseEvent e) {
> ...}
>
> }
>
> To the method "addMouseListener" you can pass an object reference to
> "MouseListener" (the interface).
> (documentation method details -> public void addMouseListener(MouseListener
> l)
> In many examples I see that you can pass the COMPLETE "current object" by
> using the keyword "this"
> (as in my example), but the method expects (only) a reference to a
> MouseListener interface.
>
> My question is:
> How does this work. What makes this possible ("this" implemented the
> interface MouseListener, but
> it is a lot more !).
>
> Thank you in advance for your explanation.
>
> Regards.
>
> B.V. Metaalwarenindustrie
> Witte van Moort
> Dick Gerrits
> dgerrits@wvm.nl
>
> -0-0-0-0-0-0-
Technical Details
Message-ID:
<3B495993.4CCDB60C@tryllian.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3b442242_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,91856323fbe3307a X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-07-09 00:09:41 PST Message-ID: <3B495993.4CCDB60C@tryllian.com> Date: Mon, 09 Jul 2001 09:13:23 +0200 From: Ronald Wildenberg <ronald@tryllian.com> X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Re: Interfaces and keyword THIS References: <3b442242_2@dnews> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 212.0.243.10 X-Trace: dnews 994662530 212.0.243.10 (9 Jul 2001 00:08:50 -0700) Lines: 67 Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews