Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #14037

Re: this

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: this
Date 2012-04-29 12:51 -0700
Organization albasani.net
Message-ID <jnk63c$e5j$1@news.albasani.net> (permalink)
References <6cab49af-72f5-4bb4-87f8-7eb2e6330b05@t23g2000yqd.googlegroups.com> <jnjja8$bcc$1@dont-email.me>

Show all headers | View raw


Tsukino Usagi wrote:
...
> One of the more interesting usages of "this" remains passing a reference to an
> object versus defining scope. It can be used to create a way to pass a
> function as an argument. For example, if we have a well-known function name

Excellent example for several reasons.

> encapsulated as a class (think "Thread", "Runnable", or "Callable"):
>
> class Callable { callme(Object o) { /* code here */ } }

Quibble: This should be an interface, not a class, and we should declare 
pedagogical examples 'public', and most certainly the method should be:

/* not to be confused with
<http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html>
  */
public interface Callable<T>
{
   void callMe(T client);
}

> class Println extends Callable { callme(Object o) { println(o); } }

public class Println implements Callable<Object>
{
   @Override
   public void callMe(Object client)
   {
     System.out.println(client);
   }
}

The pattern of single abstract method (SAM) interfaces is so prevalent and 
useful that Java will introduce a sort-of functional syntax for it in Java 8.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

this mojde <mojdeyazdi@gmail.com> - 2012-04-28 05:33 -0700
  Re: this Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-04-28 11:06 -0300
  Re: this "John B. Matthews" <nospam@nospam.invalid> - 2012-04-28 10:46 -0400
  Re: this Roedy Green <see_website@mindprod.com.invalid> - 2012-04-28 08:01 -0700
  Re: this Tsukino Usagi <usagi@tsukino.ca> - 2012-04-29 23:30 +0900
    Re: this Lew <noone@lewscanon.com> - 2012-04-29 12:51 -0700

csiph-web