On Sat, Dec 27, 2008 at 9:24 PM, CuppoJava <patrickli_2...@hotmail.com> wrote:
>
> I believe the first parameter must be "this", only in the case of
> methods .
> The init function doesn't take a "this" parameter.

Correct.

My understanding is that the init function is actually run before the
instance is even created.  So no only is there no "this" parameter,
there's not even a "this" value yet.

Instead, your init function returns values to pass to the superclass
constructor and the value to use as your object's state.  Also note
that I had to specify :constructors in order for my init to accept no
args but to use the constructor of the superclass that takes a String.

(ns net.n01se.MyThread
  (:gen-class
    :extends "java.lang.Thread"
    :constructors {[] [String]}
    :init my-init
    :state myState
    :exposes-methods {getId getIdSuper}))

(defn -my-init []
  (prn :init-my-thread)
  [["my derived class"] "this is my state"])

(defn -getId [this]
  (+ 123000 (.getIdSuper this)))

This also demonstrates the use of :exposes-methods to call the
superclass's implementation of a method I'm overridding.

Note that because I didn't use a mutable object for myState, (instead
using the string "this is my state"), there's no way to change the
value of myState.

user=> (compile 'net.n01se.MyThread)
net.n01se.MyThread
user=> (def x (net.n01se.MyThread.))
:init-my-thread
#'user/x
user=> (.myState x)
"this is my state"
user=> (.getId x)
123008
user=> (.getName x)
"my derived class"

--Chouser

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to