English 中文(简体)
对应物体在 s体内编入静态方法的方法?
原标题:Method in companion object compiled into static methods in scala?
  • 时间:2011-10-28 00:04:38
  •  标签:
  • scala

它像Scala那样将相伴物体的方法编纂成静态方法,使得从java法典中援引这些方法稍加容易。 例如,你可以撰写CompanionObject.method(而不是CompanionObject$)。 教育部 然而,有时似乎不相干的法典变化将打破这一行为。 我举这个例子来说明问题。

$ cat TestCompanion.scala 
class TestCompanion

object TestCompanion {
  def init2 {}
}

@SerialVersionUID(1L)
class TestCompanion2

object TestCompanion2 {
  def init2 {}
}


$ scalac -version
Scala compiler version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL

$ scalac TestCompanion.scala
$ javap TestCompanion
Compiled from "TestCompanion.scala"
public class TestCompanion extends java.lang.Object implements scala.ScalaObject{
    public static final void init2();
    public TestCompanion();
}

$ javap TestCompanion2
Compiled from "TestCompanion.scala"
public class TestCompanion2 extends java.lang.Object implements scala.ScalaObject{
    public static final long serialVersionUID;
    public static {};
    public TestCompanion2();
}

So the only difference between TestCompanion and TestCompanion2 is that the latter is annotated with @SerialVersionUID, and init2 is compiled into a static method in TestCompanion but not in TestCompanion2.

谁能解释为什么这些班级的加油处理方式不同? 我看不出“SerialVersionUID”说明如何影响静态方法的提供。

问题回答




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签