如何从Scala公共API屏蔽实现级别类,特征和对象?
我想减少实现级细节泄露到我的
Scala公共API中,并且有很多可用于调整可见性的旋钮,我的头脑正在试图弄清楚:
我能想到的最好的事情就是做到这一点: package restaurant { trait Service { acceptOrder(dish: String,quantity: Int) } package impl { private class ServiceImpl { // mark this & everything else in package as private acceptOrder(dish: String,quantity: Int) = println("order accepted") } private class ServiceHelper { // mark this & everything else in package as private def helpDoStuff() = ??? } } } 但请注意,我必须使用’private’标记impl包中的所有内容, IDEA用户的附注: 解决方法
好问题!
那么使用私有对象呢?当然,只有你能做到这一点才有用 package restaurant sealed trait Service { def acceptOrder(dish: String,quantity: Int): Unit } object Service { def create: Service = new Impl.ServiceImpl() private object Impl { class ServiceImpl extends Service { def acceptOrder(dish: String,quantity: Int) = println("order accepted") } class ServiceHelper { def helpDoStuff() = ??? } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |