Scala“构造函数Stopwatch不能在类Main” [英] Scala "constructor Stopwatch cannot be accessed in class Main"

查看:332
本文介绍了Scala“构造函数Stopwatch不能在类Main”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决问题的总结,我以为我在处理一个Scala问题,但事实证明,Stopwatch和Scala Logging有私人构造函数,我没有调用正确的公共方法来实例化它们。

Summary on resolution, I thought I was dealing with a Scala problem but it turns out Stopwatch and Scala Logging have private constructors, and I was not calling the proper public methods to instantiate them. gzm0's answer below points this out.

尝试使用Guava Stopwatch和Scala Logging,无论是否创建新的Stopwatch()新的Logger()在Main或实例化的类中我得到这个错误 gradle run

Trying to use Guava Stopwatch and Scala Logging, no matter whether I create a new Stopwatch() or new Logger() in Main or in an instantiated class I get this error with gradle run:

constructor Logger in class Logger cannot be accessed in class RedBlackTree4150
var loggerInst = new Logger()
constructor Stopwatch in class Stopwatch cannot be accessed in class RedBlackTree4150
var stopWatchInst = new Stopwatch()

如果这是这个问题我不知道足够实现它。我查看了此问题,但它不有一个接受的答案,我试着(只是为了乐趣)把我的构造函数从我的构造函数调用无效。

If this is a duplicate of this or this question I don't know enough to realize it. I looked at this question but it doesn't have an accepted answer, and I tried (just for fun) to take my parens off my constructor calls to no avail.

编写我的第一个Gradle / Scala项目对于算法分配的分析。我认为如果我在Java我会问静态与非静态,不知道这是我处理的问题的类型。 Scala不是作业的一部分,所以我不使用家庭作业标签。

Writing my first Gradle/Scala project for a Analysis of Algorithms assignment. I think if I was in Java I would be asking about static vs. non-static, not sure if that's the type of issue I am dealing with. Scala is not part of the assignment so I am not using a homework tag.

这是我如何调用他们和我的程序的第一部分,完整的.scala文件和build.gradle在Github上

Here's how I am calling them and the first part of my program, the full .scala file and the build.gradle are on Github

import com.google.common.base.Stopwatch
import com.typesafe.scalalogging.slf4j.Logger
import scala.collection.immutable.TreeMap
import java.util.concurrent.TimeUnit

object Main extends App {
  // var rbtree = new RedBlackTree4150(logger, stopWatch)
  var rbtree = new RedBlackTree4150()
}
// class RedBlackTree4150 (var loggerInst: Logger, var stopWatchInst: Stopwatch) {
class RedBlackTree4150() {
  var loggerInst = new Logger()
  var stopWatchInst = new Stopwatch()

正如你看到的,我已经尝试简化这一切,在Main中实例化Logger和Stopwatch,并将它们传递给类(我知道的坏主意),但没有一个工作。什么简单的Scala的东西,我在这里缺少?谢谢。

As you can see I have tried simplifying this by making it all one Object, and by instantiating Logger and Stopwatch in Main and passing them to the class (bad idea I know) but none of that works. What simple Scala thing am I missing here? Thanks.

否则我相信我在项目中的所有依赖项都正确, 。

Otherwise I believe I have all my dependencies in the project properly, and I get the same error on command line.

我还有一个错误我在这里作为一个单独的问题发布,以防万一相关,错误是:

I do have one more error I posted as a separate question here, just in case it's relevant, the error is:

/home/jim/workspace/Scala/RedBlackTree4150/src/main/scala/Main.scala:36: value map is not a member of Double
  timingsMap = for (i <- powersList; j <- runTest(i)) yield i -> j

秒表和记录器是私有的。

Stopwatch 的情况下,您可以使用 createUnstarted () 方法:

In case of the Stopwatch, you could use the createUnstarted() method:

val stopwatch = Stopwatch.createUnstarted()

如果 Logger ,必须使用 apply 方法。然而,它需要一个底层的SLF4J记录器。您可以通过SLF4J的 LoggerFactory 创建一个:

In case of Logger, you must use the apply method. However, it requires an underlying SLF4J logger. You can create one through SLF4J's LoggerFactory:

import org.slf4j.LoggerFactory
val logger = Logger(LoggerFactory.getLogger(getClass))

这篇关于Scala“构造函数Stopwatch不能在类Main”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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