我认为通过一个例子更容易展示.
假设我有一个Condition case类,Condition伴随对象用于提供替代构造函数,如下所示:
case class Condition(
field: String,
values: List[String])
}
object Condition {
def apply(field: String, value: String): Condition = {
Condition(field, List(value))
}
}
Run Code Online (Sandbox Code Playgroud)
当我从另一个导入它时,我得到以下警告(最终变成错误):
import utils.query.Condition
[warn] [...]/ConditionBuilder.scala:14: imported `Condition' is permanently hidden by definition of object Condition in package query
[warn] import utils.query.Condition
[warn] ^
[warn] one warning found
Run Code Online (Sandbox Code Playgroud)
我想在执行变量类型时访问条件类型,并在执行其中一个方法时访问伴随对象
有没有办法实现这一点,并避免这种警告(当然,除了重命名伴侣对象)?
有人可以澄清为什么以下代码导致MatchError?在这种情况下,MatchError意味着什么?
class A {
def g = A.f
}
object A {
val f = "Object A"
}
class B extends A {
override val A.f = "Object B"
}
val b = new B
b.g
Run Code Online (Sandbox Code Playgroud)
鉴于这不起作用,有没有办法覆盖与此类似的伴侣对象val或def?
我需要在我的伴侣对象中定义一个val,它用一个以伴侣类作为参数的方法初始化.
我想用特征处理这个问题,不要重复自己.我的问题是,X.getClass与classOf [X]不同.首先是类的同伴对象,第二个是班里的同伴类的,但我需要得到同伴类,而不直接硬编码.
基本上我需要这样的东西:
trait Foo {
}
object FooCompanionObject[f <: Foo] {
val fClazz = classOf[f]
}
// Bar's fClass should be classOf[Bar]
case class Bar extends Foo;
object Bar extends FooCompanionObject[Bar];
Run Code Online (Sandbox Code Playgroud)
问题是由于类型擦除,我无法获得泛型类
我想知道,为什么班级不能访问伴侣对象的字段?
class MyClass {
println(val1) // not found, why?
}
object MyClass {
val val1 = "str"
}
Run Code Online (Sandbox Code Playgroud)
应该,甚至应该可以访问私人领域object MyClass.
我们考虑一下这段代码:
class A
object A{
implicit def A2Int(implicit a:A)=1
implicit def A2String(a:A)="Hello"
}
object Run extends App{
implicit val a: A =new A
import A.A2Int
// without this import this code does not compile, why ?
// why is no import needed for A2String then ?
def iWantInt(implicit i:Int)=println(i)
def iWantString(implicit s:String)=println(s)
iWantInt
iWantString(a)
}
Run Code Online (Sandbox Code Playgroud)
它运行和打印:
1
Hello
Run Code Online (Sandbox Code Playgroud)
现在,如果我们注释掉这条线
import A.A2Int
Run Code Online (Sandbox Code Playgroud)
然后我们得到一个编译错误:

随着该系列的注释,为什么Scala无法找到A.A2String它是否能找到A.A2Int?
如何解决这个问题?
谢谢阅读.
我注意到,如果不推荐使用 case 类,则不推荐使用它的伴生对象。
scala> @deprecated case class A(x: Int)
warning: there was one deprecation warning; re-run with -deprecation for details
defined class A
scala> A(0)
res0: A = A(0)
scala> new A(0)
warning: there was one deprecation warning; re-run with -deprecation for details
res1: A = A(0)
Run Code Online (Sandbox Code Playgroud)
我想收到一个警告,A(0)正如我收到的一样new A(0)。我应该明确定义伴随对象并弃用它吗?有没有更好的方法?
我从名为 UserViewHolder 的官方示例中学习了 ViewHolder 的使用。
public class UserViewHolder extends RecyclerView.ViewHolder {
static UserViewHolder create(LayoutInflater inflater, ViewGroup parent) {
UserItemBinding binding = UserItemBinding
.inflate(inflater, parent, false);
return new UserViewHolder(binding);
}
private UserItemBinding mBinding;
private UserViewHolder(UserItemBinding binding) {
super(binding.getRoot());
mBinding = binding;
}
public void bindTo(User user) {
mBinding.setUser(user);
mBinding.executePendingBindings();
}
}
Run Code Online (Sandbox Code Playgroud)
我要写很多ViewHolder类,所以我希望我能写一个抽象类。在 Java 中,它看起来像:
public abstract static class BaseViewHolder {
abstract static BaseViewHolder create()
abstract void bindTo()
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 Kotlin 编写它,但最后我发现它不像在 Java 中那么简单。
abstract class BaseViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
abstract …Run Code Online (Sandbox Code Playgroud) 我遇到了伴随对象选择其类型而不是案例类的问题
我正在使用喷雾 json serdes。他们需要一个隐式的 JsonFormat。此格式是通过调用取决于案例类参数数量的函数来获得的: jsonFormat2(Class2) 如果案例类有两个字段,例如
case class Class2(a: String, b: Integer)
Run Code Online (Sandbox Code Playgroud)
或 jsonFormat3(Class3) 的
case class Class3(a: String, b: Integer, c: Long)
Run Code Online (Sandbox Code Playgroud)
鉴于必须知道案例类在整个代码中具有的参数数量并不好,我想创建一个案例类伴随对象,以便您可以封装此信息并从类本身获取 JsonFormat,例如:
object Class2 extends DefaultJsonProtocol
{
def getJsonFormat() = {
jsonFormat2(Class2)
}
}
Run Code Online (Sandbox Code Playgroud)
但如果我这样做,我会遇到以下编译问题:
type mismatch;
[error] found : mypackage.Class2.type
[error] required: (?, ?) => ?
[error] jsonFormat2(Class2)
Run Code Online (Sandbox Code Playgroud)
如果我们查看 jsonFormat2 中的代码,签名是:
def jsonFormat2[P1 :JF, P2 :JF, T <: Product :ClassManifest
(construct: (P1, P2) => T): RootJsonFormat[T] = { // ...
Run Code Online (Sandbox Code Playgroud)
如果我更改伴随对象名称(例如更改为 MyClass2),它将正常工作。所以,类型似乎是冲突的。
似乎在处理类型时,伴生对象无法像它们所使用的类那样命名。
有人可以解释为什么会发生这种情况吗?如果有限制,或者如何解决它,以便伴生对象可以使用相同的名称?
在 Kotlin 语言中,这个语法有什么作用以及如何工作?
class ClassName1 {
companion object {
fun ClassName2.funName()=""
}
}
Run Code Online (Sandbox Code Playgroud) 我希望能够从它的伴随对象访问我的类的 simpleName。
我想要这个:
val o1 = Outer("foo")
val o2 = Outer("bar")
Run Code Online (Sandbox Code Playgroud)
打印以下输出:
Outer: hello
Outer: foo
Outer: bar
Run Code Online (Sandbox Code Playgroud)
实际用例在java中是这样的:
class Outer {
static final String TAG = Outer.class.simpleName();
// and now I'm able to use Outer.TAG or just TAG in both static and non-static methods
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两件事:
将 Outer 的 simpleName 分配给伴生对象的 COMPANION_TAG,然后使用来自伴生初始化和所有 Outer 函数的 COMPANION_TAG。我可以从我需要的任何地方访问 COMPANION_TAG,但不幸的是,我只能通过这种方式获得“Companion”而不是“Outer”。
从伴随对象的初始化访问 Outer.OUTER_TAG。这里的问题是我找不到访问它的方法。
这是代码:
class Outer(str: String) {
private val OUTER_TAG = javaClass.simpleName
companion object {
@JvmStatic val COMPANION_TAG = PullDownAnimationLayout.javaClass.simpleName // gives "Companion" …Run Code Online (Sandbox Code Playgroud) companion-object ×10
scala ×7
kotlin ×3
static ×2
deprecated ×1
implicits ×1
java ×1
oop ×1
overriding ×1
spray-json ×1
types ×1