GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Saturday 17 July 2004
Chapter 6, Exercise 3

3. Create a simple class. Inside a second class, define a reference to an object of the first class. Use lazy initialization to instantiate this object.

Well, Scala doesn't really quite permit lazy initialization (see the "= _;" phrase in sc's declaration), but it's substantially the same. Here are the Java and Lisp versions that Larry wrote.

package x;
class SimpleClass {} 
class SecondClass {
    private var sc : SimpleClass = _;
    def getSC : SimpleClass = {
        if (sc == null) sc = new SimpleClass();
        sc;
    }
}
object c6x3 {
    def main ( args : Array[String] ) = {
        val c2 = new SecondClass;
        W.rn( "sc is " + c2.getSC );
    }
}

12:50:50 AM   comment/     


Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:41:22 .
Click here to send an email to the editor of this weblog.

July 2004
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Jun   Aug

Previous/Next