我想爲某些導入的Java類型創建伴隨對象,因此我不必使用new來分配它們。我想從類型Vector3f開始,它是從jMonkeyEngine的com.jme3.math中導入的。爲從Java導入的類創建伴隨對象
我想的是:
package com.jme3.math
object Vector3f {
def apply() = new Vector3f()
def apply(x:Float, y:Float, z:Float) = new Vector3f(x,y,z)
}
編譯時,我得到錯誤:
Error:(8, 21) not found: type Vector3f def apply() = new Vector3f()
當我添加import com.jme3.math.Vector3f
,我得到警告,這或許可以解釋我所看到的:
Warning:(3, 22) imported `Vector3f' is permanently hidden by definition of object Vector3f in package math
如何爲com.jme3.math.Vector3f或其他類型的導入創建伴隨對象從Java編輯?
可能重複在Java中定義的類?](http://stackoverflow.com/questions/4921827/in-scala-how-can-i-define-a-companion-object-for-a-class-defined-in-java) – Suma 2014-09-04 11:59:26