Berkeley CSUA MOTD:Entry 37384
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/24 [General] UID:1000 Activity:popular
5/24    

2005/4/27-28 [Computer/SW/Languages/Java] UID:37384 Activity:low
4/27    Dear Java gurus, say I have the following code, where Visitor()
        has a method called "visit(Node n)...". I see the following code.
        Does it mean I'm temporarily over-writing the visit method?
         root.accept(new Visitor() {
            public void visit(Node n) {
               //Do stuff with n
            }
         });
         \_ for a lot more on the above code, see Erich Gamma's book
             "Design Patterns" (introduction and visitor pattern)
        \_ It's an anonymous class and yup you've got it.  Even more
           useful is the fact that as long as you have final variables
           in the code that calls visit, you can use those variables
           in the redefinition of visit.
           final Long x;
           root.accept(new Visitor() {
              public void visit(Node n, Long y) {
                 super.visit(n, x+y);
              }
           });
           Is valid.  (I may have a few details wrong, but that's the basic
           idea.  See documentation for anonymous classes if you want to
           know more.)
           \_ To slightly correct the above, it's an anonymous *inner*
              class, which is the Java version of a closure-like-thing
              (lamdas if you know LISP, Blocks if you do Smalltalk).
              If you aren't familiar with them, you should write a few little
              programs to explore them -- they can be quite useful.
           \_ Did you actually try compiling the above code or the code
              below it? It doesn't seem to work.
              \_ No, no I didn't.  I just gave the concept.  Note where
                 I said I may have a few details wrong.  Here, because you
                 are obviously unable to fill in the blanks yourself...

     public class Foo {
         public Foo() {
         }

         public void a(Long a) {
             System.out.println("a("+a+")");
         }

         public void doA(Long a) {
             final Long b = new Long(11);
             new Foo() {
                 public void a(Long a) {
                     super.a(new Long(b.longValue()+a.longValue()));
                 }
             }.a(a);
         }

         public static void main(String[] args) {
             new Foo().doA(new Long(5));
         }
     }
2025/05/24 [General] UID:1000 Activity:popular
5/24    

You may also be interested in these entries...
2013/4/29-5/18 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:54665 Activity:nil
4/29    Why were C and Java designed to require "break;" statements for a
        "case" section to terminate rather than falling-through to the next
        section?  99% of the time poeple want a "case" section to terminate.
        In fact some compilers issue warning if there is no "break;" statement
        in a "case" section.  Why not just design the languages to have
        termination as the default behavior, and provide a "fallthru;"
	...
2013/5/1-18 [Computer/SW/Languages/Java, Computer/Theory] UID:54669 Activity:nil
5/1     What's the difference between CS and Computer Engineering?
        http://holykaw.alltop.com/top-ten-paying-degrees-for-college-graduates
        \_ One is science and the other is engineering.
        \_ From http://en.wikiquote.org/wiki/Computer_science
           'A folkloric quotation ... states that "computer science is no more
           about computers than astronomy is about telescopes."  The design
	...
2013/3/5-26 [Computer/SW/Languages/Java] UID:54618 Activity:nil
3/5     Three emergency Java updates in a month. Why do I have a feeling
        that the third one won't be the last one?
        \_ Bingo!
	...
2012/12/18-2013/1/24 [Computer/SW/Languages/Perl] UID:54561 Activity:nil
12/18   Happy 25th birthday Perl, and FUCK YOU Larry Wall for fucking up
        the computer science formalism that sets back compilers development
        back for at least a decade:
        http://techcrunch.com/2012/12/18/print-happy-25th-birthday-perl
        \_ I tried to learn Perl but was scared away by it.  Maybe scripting
           lanauages have to be like that in order to work well?
	...
2012/12/4-18 [Computer/SW/Languages/Java] UID:54544 Activity:nil
12/4    Holy cow, everyone around me in Silicon Valley is way beyond
        middle class according to Chinni's definition:
        http://en.wikipedia.org/wiki/American_middle_class
        \_ Let's set our goals higher:
           http://en.wikipedia.org/wiki/Upper_middle_class_in_the_United_States
           \_ How about this one?
	...
2012/10/29-12/4 [Science/Disaster, Computer/SW/Languages/Java, Politics/Domestic/President/Bush] UID:54516 Activity:nil
10/29   Go Away Sandy.
        \_ Sorry, Coursera is performing preventive maintenance for this
           class site ahead of Hurricane Sandy. Please check back in 15 minutes.
           class site ahead of Hurricane Sandy. Please check back in 15
           minutes.
        \_ Bitch.
	...