代码之家  ›  专栏  ›  技术社区  ›  Hartmut Pfarr

返回数组的第一个元素,在def中

  •  1
  • Hartmut Pfarr  · 技术社区  · 7 年前

    def 不是编译,第二个怎么写 def 为此,它正在编译(在一行中返回数组的第一个条目)。谢谢

    import java.awt.{GraphicsDevice, GraphicsEnvironment}
    
    class SeparatedTestCase {
    
       def does_compile: GraphicsDevice = {
          val ge: GraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment
          val devices: Array[GraphicsDevice] = ge.getScreenDevices
          devices(0)
       }
    
       def does_not_compile: GraphicsDevice = {
          val ge: GraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment
          val device0: GraphicsDevice = (ge.getScreenDevices)(0)   // <---- compile error
          device0
       }
    }
    
    //Error:(13, 59) no arguments allowed for nullary method getScreenDevices: ()Array[java.awt.GraphicsDevice]
    //val device0: GraphicsDevice = (ge.getScreenDevices)(0)
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Vladimir Matveev    7 年前

    必须使用显式括号调用该方法:

    ge.getScreenDevices()(0)
    

    这不会编译,因为您的第二次调用与

    ge.getScreenDevices(0)
    

    这不符合你的要求,因为 getScreenDevices

    推荐文章