向所有Java大师致敬!
从Java8开始,我们可以在接口中使用默认实现(yay!).但是,如果要从默认方法进行日志记录,则会出现问题.
我有一种感觉,每次我想在默认方法中记录某些内容时调用.getLogger()是不明智的.
是的,可以在接口中定义静态变量 - 但这对接口来说不是一个好的做法+它暴露了记录器(必须是公共的).
我现在的解决方案:
interface WithTimeout<Action> {
default void onTimeout(Action timedOutAction) {
LogHolder.LOGGER.info("Action {} time out ignored.", timedOutAction);
}
static final class LogHolder {
private static final Logger LOGGER = getLogger(WithTimeout.class);
}
}
Run Code Online (Sandbox Code Playgroud)
LogHolder对所有人来说仍然是可见的,因为它没有提供任何方法,并且它应该是接口内部的.
你们有没有人知道更好的解决方案?:)
编辑:我使用Logback支持的SLF4J
我正在写作业,我使用Octave遇到了这个错误.它不会影响我的解决方案的功能,但我很好奇为什么会发出此警告.
% X is column vector, p is max degree of polynom
% example:
% X = [1;2;3;4], p = 3
% X_poly = [1,1,1; 2,4,8; 3,9,27; 4,16,64]
function [X_poly] = polyFeatures(X, p)
powers = ones(numel(X),1) * linspace(1,p,p);
X_poly = X .^ powers;
end
Run Code Online (Sandbox Code Playgroud)
问候,
汤姆
此代码将无法编译
val sortedSet = SortedSet[Int](Array(1,2,3,4).toSeq)
Error: type mismatch; found :Seq[Int] required Int
Run Code Online (Sandbox Code Playgroud)
但是,以下是SortedSet中apply的定义:
def apply[A](elems: A*)(implicit ord: Ordering[A]): CC[A] = (newBuilder[A](ord) ++= elems).result
Run Code Online (Sandbox Code Playgroud)
它说elem是一个vararg因此应该是Seq [A]类型我错过了什么?为什么我不能通过seq作为vararg?
我在Scala中写俄罗斯方块作为练习,我遇到了这种奇怪的行为:
abstract class Orientation(protected val index: Int) {
private[Orientation] val impls: Array[Orientation] = Array(OrientedLeft, OrientedUp, OrientedRight, OrientedDown)
def rotateCounterClockwise = impls(
if (index == 0) 3
else index - 1
)
def rotateClockwise = impls((index + 1) % 4)
}
object OrientedLeft extends Orientation(0) {
override def toString = "Left"
}
object OrientedUp extends Orientation(1) {
override def toString = "Up"
}
object OrientedRight extends Orientation(2) {
override def toString = "Right"
}
object OrientedDown extends Orientation(3) {
override def …Run Code Online (Sandbox Code Playgroud)