简体   繁体   English

奇怪的JAVA语法

[英]Strange JAVA Syntax

The following code is used in Spring - Hibernate Full Java Based Configuration in many places (like here ): 在许多地方,Spring-Hibernate完全基于Java的配置中使用了以下代码(例如here ):

Properties hibernateProperties() {
        return new Properties() {
            {
                setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                setProperty("hibernate.show_sql", "true");
                setProperty("hibernate.format_sql", "true");
                setProperty("use_sql_comments", "true");
                setProperty("hibernate.hbm2ddl.auto", "none");
            }
        };
    }

This is a method in a class. 这是一个类中的方法。 I think that the return object is an anonymous class object (Please tell me if I'm wrong). 我认为返回对象是一个匿名类对象(如果我错了,请告诉我)。 What is with the curly brackets enclosing the setProperty statements? 包含setProperty语句的花括号是什么? Is that an array? 那是数组吗? If so there should not be any semi-colons in there? 如果是这样,那里应该没有分号了吗?

I haven't been able to find any place where this syntax is explained. 我还找不到任何解释此语法的地方。 Please give some link where this explained in detail. 请提供一些链接,其中对此进行了详细说明。

This is a method that returns an object of type Properties . 这是一种返回类型为Properties的对象的方法。 The method creates an anonymous class which defines an instance initializer block that sets some properties in the returned object: 该方法创建一个匿名类该类定义一个实例初始化器块 ,该在返回的对象中设置一些属性:

return new Properties() {
    // this is an instance initializer block which pre-sets some properties
    {
         setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
         setProperty("hibernate.show_sql", "true");
         setProperty("hibernate.format_sql", "true");
         setProperty("use_sql_comments", "true");
         setProperty("hibernate.hbm2ddl.auto", "none");
     }
};

It creates a Properties object with predefined properties. 它创建具有预定义属性的Properties对象。

Classes can have initialization blocks in them (static and non-static). 类中可以包含初始化块 (静态和非静态)。 In case of non-static blocks their code will be moved at start of each constructor of your class (actually almost at start because it will be placed right after explicit or implicit super() call). 如果是非静态块,则它们的代码将在类的每个构造函数的开始处移动(实际上几乎在开始时,因为它将在显式或隐式super()调用之后立即放置)。

So class like 所以像

class Foo{

    void method(){
        System.out.println("method()");
    }

    Foo(){
        System.out.println("constructor of Foo");
    }

    {
        System.out.println("initialization block");
        method();
    }

    public static void main(String[] args) {
        Foo f = new Foo();
    }

}

is same as 与...相同

class Foo {

    void method() {
        System.out.println("method()");
    }

    Foo() {
        super();
        //initialization block is moved here
        {
            System.out.println("initialization block");
            method();
        }
        System.out.println("constructor of Foo");
    }


    public static void main(String[] args) {
        Foo f = new Foo();
    }

}

So inside this initialization block you are able to invoke any method of Foo class, just like you are able to invoke them inside constructor. 因此,在此初始化块中,您可以调用Foo类的任何方法,就像您可以在构造函数中调用它们一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM