Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
非常简单的java语法澄清_Java - Fatal编程技术网

非常简单的java语法澄清

非常简单的java语法澄清,java,Java,我从未学习过Java,但我需要理解以下代码的含义,主要问题是大括号: /** * This Universe uses the full HashLife algorithm. * Since this algorithm takes large steps, we have to * change how we increment the generation counter, as * well as how we construct the root object.

我从未学习过Java,但我需要理解以下代码的含义,主要问题是大括号:

/**
 *   This Universe uses the full HashLife algorithm.
 *   Since this algorithm takes large steps, we have to
 *   change how we increment the generation counter, as
 *   well as how we construct the root object.
 */
public class HashLifeTreeUniverse extends TreeUniverse {
   public void runStep() {
      while (root.level < 3 ||
             root.nw.population != root.nw.se.se.population ||
             root.ne.population != root.ne.sw.sw.population ||
             root.sw.population != root.sw.ne.ne.population ||
             root.se.population != root.se.nw.nw.population)
         root = root.expandUniverse() ;
      double stepSize = Math.pow(2.0, root.level-2) ;
      System.out.println("Taking a step of " + nf.format(stepSize)) ;
      root = root.nextGeneration() ;
      generationCount += stepSize ;
   }
   {
      root = HashLifeTreeNode.create() ;
   }
}
它看起来像一个没有签名的方法,这意味着什么

提前谢谢你

这是一个问题

这是一段代码,作为构造新实例的一部分,在执行构造函数体之前执行。但是,直接在一个方法之后以这种方式进行布局是很奇怪的。(老实说,很少看到它。在这种情况下,如果字段在同一个类中声明,它看起来应该只是一个字段初始值设定项。(不清楚您是否向我们展示了整个类。)

实例初始值设定项和字段初始值设定项按照文本顺序在超类构造函数之后和“this”构造函数体之前执行

例如,考虑这个代码:

class Superclass {
    Superclass() {
        System.out.println("Superclass ctor");
    }
}

class Subclass extends Superclass {
    private String x = log("x initializer");

    {
        System.out.println("instance initializer");
    }

    private String y = log("y initializer");

    Subclass() {
        System.out.println("Subclass ctor");
    }

    private static String log(String message)
    {
        System.out.println(message);
        return message;
    }
}

public class Test {    
    public static void main(String[] args) throws Exception {
        Subclass x = new Subclass();
    }
}
输出为:

Superclass ctor
x initializer
instance initializer
y initializer
Subclass ctor
这是一个很好的例子

这是一段代码,作为构造新实例的一部分,在构造函数体被执行之前被执行。但奇怪的是,直接在方法之后以这种方式进行布局。(老实说,很少看到它。在这种情况下,如果字段在同一个类中声明,它看起来应该只是一个字段初始值设定项。(不清楚您是否向我们展示了整个类。)

实例初始值设定项和字段初始值设定项按照文本顺序在超类构造函数之后和“this”构造函数体之前执行

例如,考虑这个代码:

class Superclass {
    Superclass() {
        System.out.println("Superclass ctor");
    }
}

class Subclass extends Superclass {
    private String x = log("x initializer");

    {
        System.out.println("instance initializer");
    }

    private String y = log("y initializer");

    Subclass() {
        System.out.println("Subclass ctor");
    }

    private static String log(String message)
    {
        System.out.println(message);
        return message;
    }
}

public class Test {    
    public static void main(String[] args) throws Exception {
        Subclass x = new Subclass();
    }
}
输出为:

Superclass ctor
x initializer
instance initializer
y initializer
Subclass ctor
它是在构造实例之前执行的(两段代码),每次尝试创建HashLifeTreeUniverse实例时都会执行该实例

在您突出显示的一对大括号之间的代码将在调用构造函数之前执行。

这是在构建实例之前执行的一对代码,每次尝试创建HashLifeTreeUniverse实例时都会执行该实例


您突出显示的一对大括号之间的代码将在调用构造函数之前执行。

可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能是2005年的一段代码,摘自:Thank you是否该根是受扩展基类保护的根?如果是:非常不喜欢!@laune:是的,我喜欢是的,我也不喜欢它。明白了,我不喜欢它…:)谢谢:)这是我看到的2005年的一段代码,摘自:谢谢,这可能是一个扩展基类的受保护根吗?如果是的话:非常不喜欢!@laune:是的,我怀疑这是对的,我也不喜欢它。明白了,我不喜欢它…)谢谢:)