除非我在包中声明它

除非我在包中声明它

本文介绍了除非我在包中声明它,否则类不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 projectDir / app中的 AppLoader.scala 中有一个 AppComponents code>

I have a class AppComponents in AppLoader.scala in projectDir/app

我的测试在 projectDir / test / ControllerSpec / UserControllerSpec.scala

UserControllerSpec.scala 中,我尝试创建 AppComponents 的实例但编译器找不到 AppComponents

In UserControllerSpec.scala, I tried to create an instance of AppComponents but the compiler couldn't find the AppComponents class

override def components: BuiltInComponents = new AppComponents(context) //doesn't compile

但如果我包含语句 package app Apploader.scala 中然后编译器能够找到 AppComponents 和上面的代码编译。

But if I include statement package app in Apploader.scala then the compiler is able to find AppComponents and the above code compiles.

我不明白这种行为。请有人解释一下。

I don't understand this behavior. Could someone please explain.

推荐答案

所以 AppComponents 只应对其他类/特征/等可见。在包裹外面。因为

So AppComponents should only be visible to other classes/traits/etc. outside a package. Because

它看起来像 UserControllerSpec 确实声明了一个包,但看不到空包的成员。

it looks like UserControllerSpec does declare a package and can't see the empty package's members.

此行为可能与Java保持一致,请参阅。

This behavior is probably for consistency with Java, see answers to Import package with no name Java.

这篇关于除非我在包中声明它,否则类不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:38