Java有:
public void someMethod(int ... intArray) { // question: what is the equivalent to "..."
// do something with intArray
}
Run Code Online (Sandbox Code Playgroud)
如何在Scala中实现相同的功能?也就是说,将未定义数量的参数传递给方法?
当我尝试在MIPS中处理无符号整数时,我执行的每个操作的结果都保持有符号(即,整数都是2的补码),即使我执行的每个操作都是无符号的:addu,multu所以第四个......
当我在该范围内打印数字时,[2^31, 2^32 - 1]我得到它们的"溢出"负值,好像它们已经签名(我猜它们是).
但是,当我尝试这样的事情:
li $v0, 1
li $a0, 2147483648 # or any bigger number
syscall
Run Code Online (Sandbox Code Playgroud)
打印的号码总是 2147483647 (2^31 - 1)
我很困惑......我错过了什么?
PS:我没有包含我的代码,因为它不是非常易读(例如汇编代码)并且抛开这个问题,似乎工作正常.如果有人认为有必要,我会马上加入!
考虑以下:
class X {
public:
X(int i) { cout << "X(int i)" << endl; }
X(const X& x) { cout << "X(const X& x)" << endl; }
};
void main() {
X x1(1);
X x2 = X(1);
X x3 = (X)1;
}
Run Code Online (Sandbox Code Playgroud)
运行此代码会产生以下输出:
X(int i)
X(int i)
X(int i)
Run Code Online (Sandbox Code Playgroud)
我认为以上所有三个语句都是等价的,因为从未调用CTOR副本.但是,将X复制CTOR更改为私有:
class X {
public:
X(int i) { cout << "X(int i)" << endl; }
private:
X(const X& x) { cout << "X(const X& x)" << endl; }
}; …Run Code Online (Sandbox Code Playgroud) 假设我有以下java类:
package com.test;
public class Outer {
public static class Inner { public static final String VAL = "Inner"; }
}
Run Code Online (Sandbox Code Playgroud)
我可以VAL像你期望的那样从scala代码引用常量,但是当我尝试使用scala工具箱编译器编译引用该字段的代码时,它会失败.(stacktrace遵循以下示例).
这段代码证明了我的意思:
import scala.reflect.runtime.universe
import scala.tools.reflect.ToolBox
object Issue extends App {
val mirror = universe.runtimeMirror(getClass.getClassLoader)
val toolbox = ToolBox(mirror).mkToolBox()
println (com.test.Outer.Inner.VAL) // prints 'Inner'
val tree = toolbox.parse("com.test.Outer.Inner.VAL")
val compiled = toolbox.compile(tree)() // fails to compile
}
Run Code Online (Sandbox Code Playgroud)
这是我获得的异常跟踪:
Exception in thread "main" scala.tools.reflect.ToolBoxError: reflective compilation has failed:
value Inner is not a member of object com.test.Outer
at …Run Code Online (Sandbox Code Playgroud) 考虑以下计划:
class Base {
public:
virtual void foo() const {
cout << "Base::foo()" << endl;
}
};
class Derived : public Base {
public:
virtual void foo() {
cout << "Derived::foo()" << endl;
}
};
void func(Base& obj) {
obj.foo();
}
void main() {
Derived d;
func(d); // Base::foo() is printed
}
Run Code Online (Sandbox Code Playgroud)
如果我const从Base类foo方法中删除然后Derived::foo()调用.我似乎无法理解这种行为.
1)这种行为的原因是什么?
2)这是在编译时还是在运行时决定的?
谢谢
假设我想扩展课程C,获得SubC.
现在我想访问变量c中SubC的方法下面在实施例所呈现printC.
以下不工作,作为调用printC上的SubC实例将打印SubC的c,而不是C的c(我想我可能已经选择更好的名字......)
class C(protected var c : Int) {
def update(n : Int) {
c = n
}
}
class SubC(c: Int) extends C(c) {
def printC : Unit = {
println(c)
}
}
object Main {
def main(args: Array[String]) {
val subC = new SubC(1)
subC.update(3)
subC.printC // prints 1
}
}
Run Code Online (Sandbox Code Playgroud)
可能的(但不合需要的)解决方案是:
class SubC(cc: Int) …Run Code Online (Sandbox Code Playgroud) scala ×3
c++ ×2
inheritance ×2
java ×2
mips ×1
reflection ×1
scala-2.10 ×1
toolbox ×1
unsigned ×1