官术网_书友最值得收藏!

Creating the Main object

Here we are! Let's create our first application. First, create the entry point for the program. If you are coming from Java, it would be equivalent to defining the public static void main(String[] args).

Right-click on the src/main/scala folder and select New | Scala Class. Give Main as the class name and Object as the Kind:

We have created our first object. This object is a singleton. There can be only one instance of it in the JVM. The equivalent in Java would be a static class with static methods.

We would like to use it as the main entry point of our program. Scala provides a convenient class named App that needs to be extended. Let's extend our Main object with that class:

object Main extends App {

}

The App superclass defines a static main method that will execute all the code defined inside your Main object. That's all – we created our first version, which does nothing!

We can now run the program in IntelliJ. Click on the small green triangle in the gutter of the object definition, as follows:

The program gets compiled and executed, as shown in the following screenshot:

It is not spectacular, but let's improve it. To get the right habits, we are going to use the TDD technique to proceed further.

主站蜘蛛池模板: 高邑县| 固阳县| 紫阳县| 河南省| 万宁市| 鱼台县| 南木林县| 贵溪市| 通州区| 皮山县| 县级市| 泰安市| 塔城市| 咸宁市| 乃东县| 丰宁| 东乡县| 天津市| 安阳市| 巫山县| 无极县| 永修县| 广河县| 安福县| 仁寿县| 洪洞县| 平原县| 东莞市| 平昌县| 泸州市| 阳西县| 南康市| 克山县| 伊川县| 博乐市| 含山县| 九台市| 农安县| 收藏| 晋中市| 轮台县|