Any recommendations on how to use anonymous classes while staying consistent with Allman indent style? I don't really like anything I've come up with, e.g.
// P开发者_JS百科ass as parameter.
foo(new Clazz( )
    {
       // Do stuff.
    });
// Assign to variable.
Clazz bar = new Clazz( )
            {
               // Do stuff.
            };
The best compromise I came up with for my own code, is indenting the anonymous class a single tabbing level, and putting the closing parentheses on a new line.
// Pass as parameter.
foo(new Clazz( )
    {
       // Do stuff.
    }
);
void func () {
    foo(new Clazz( )
        {
           // Do stuff.
        }
    );
}
// Assign to variable.
Clazz bar = new Clazz( )
    {
        // Do stuff.
    };
Allman style is really about aligning the {braces}, not all the (brackets). I suppose you are free to do both if you want, but it looks like a source of problems (like this one) to me, without a clear payback in readability. In other words, a logical fetish :-)
You could follow the guide at http://mbreen.com/javastyle.html: "A statement containing a declaration with a code block is indented first as a statement." I think that would change your examples to
foo (new Clazz( )
    {
        // Do stuff.
    });
Clazz bar = (
    new Clazz( )
    {
        // Do stuff.
    });
This is what I've settled on.
Foo foo = new Foo()
{
    // Do stuff.
};
And I just don't define anonymous classes inside method calls anymore.
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
 加载中,请稍侯......
      
精彩评论