简体   繁体   English

如何从主要方法调用scala类的函数?

[英]how to call function of scala class from main method?

i am learning as i could not install scala plugin in my laptop because its 32 bit so i am practising in command prompt. 我正在学习,因为我无法在笔记本电脑中安装Scala插件,因为它的32位,所以我正在命令提示符下进行练习。 I have created below scala classes but i am not sure hot call function from main method. 我已经在scala类下面创建了,但是我不确定主要方法的热调用功能。

class AccessTest
{
  def display():Unit= println("this is from accessTest")
}

Object Hello
{
val access  = new AccessTest();
access.display();
}

both classes are in com folder. 这两个类都在com文件夹中。 i am not sure about creating package manually . 我不确定要手动创建软件包。 can somebody help me on this? 有人可以帮我吗?

you are doing spell mistake while creating the object. 您在创建对象时犯了拼写错误。 You are giving Object instead of object . 您正在给Object而不是object Here is an example: 这是一个例子:

scala> class AccessTest {
     |   def display():Unit= println("this is from accessTest")
     | }
defined class AccessTest

scala> object Hello extends  App{
     | val access  = new AccessTest();
     | access.display();
     | }
defined object Hello

I hope it will help you. 希望对您有帮助。

Simply Write a main method in object and call the main method using object name .(dot) main method with Array("") 只需在对象中编写一个main方法,然后使用对象名称调用main方法。(dot)main方法与Array(“”)

scala>object MynewObj{
def main(args: Array[String]){
println("Hello World")
}
}

defined object MynewObj

scala> MynewObj.main(Array(""))
Hello World

To call it from main method, you either need to extend App or define main method. 要从main方法调用它,您需要扩展App或定义main方法。 And object of 'o' must be in small letter. 并且“ o”的对象必须用小写字母表示。

scala> class AccessTest{
     |   def display():Unit= println("this is from accessTest")
     | }
defined class AccessTest

scala> object Hello extends App{
     | val access  = new AccessTest();
     | access.display();
     | }
defined object Hello

scala> Hello.main(Array(""))
this is from accessTest

scala> 

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

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