除非我在包中声明它,否则类不可见 [英] Class is not visible unless I declare it in a package

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

问题描述

我在 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


如果我在Apploader.scala中包含语句包应用程序,那么编译器就能找到AppComponents

if I include statement package app in Apploader.scala then the compiler is able to find AppComponents

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

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

此行为可能与Java保持一致,请参阅导入没有Java名称的包

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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆