请帮助我理解继承是如何工作的.
如果我有两个班,父母和孩子.当我创建一个子实例时,是否构造了父类实例?
我的Parent班级代码.
public class Parent {
private char s;
@Override
public int hashCode() {
return s;
}
}
Run Code Online (Sandbox Code Playgroud)
和 Child
public class Child extends Parent {
private int i;
public Child(int i) {
super();
this.i = i;
}
@Override
public int hashCode() {
return i;
}
}
Run Code Online (Sandbox Code Playgroud)
最后是测试
public class Main {
public static void main(String[] args) {
Child child = new Child(100);
System.out.println(child.hashCode());
System.out.println(child.getClass().getSuperclass().hashCode());
}
}
Run Code Online (Sandbox Code Playgroud)
在输出中,我得到
100
2000502626
Run Code Online (Sandbox Code Playgroud)
所以对象的哈希是不同的.这意味着当我创建Child它的实例时也会创建它的实例Parent.我对吗?
我正在尝试制作可以改变颜色的div.这是我的代码:
window.onload = function () {
for (;;){
setTimeout(function () {
document.querySelector('style').style.backgroundColor = colors[rand(colors.length)];
}, 2000);
}
}
var colors = [
'red',
'green',
'blue',
'yellow',
'magenta',
'pink'
];
var rand = function (max) {
return Math.floor(Math.random() * max);
};Run Code Online (Sandbox Code Playgroud)
.style{
background-color: pink;
top: 50px;
left: 50px;
height: 50px;
width: 50px;
}Run Code Online (Sandbox Code Playgroud)
<body>
<div class="style"></div>
</body>Run Code Online (Sandbox Code Playgroud)
但我无法找出它为什么不起作用.
编辑:该脚本也崩溃了浏览器
我应该指定哪种函数类型AnyRef?
case class Fun() {
val anonFun: AnyRef = {
(x: Int) => x + 1
}
}
Run Code Online (Sandbox Code Playgroud)
我的通话代码:
val f = Fun()
val result = f.anonFun(1)
Run Code Online (Sandbox Code Playgroud)
PS
正如@jarandaf所说,这个特定功能的返回类型是Function1[Int, Int].但谁可以解释我为什么不Function1[Int]呢?
这两种方法有什么区别?
def method1(msg: => String) = print(msg)
def method2(msg: String) = print(msg)
Run Code Online (Sandbox Code Playgroud)
我可以调用它们.
method1("1")
method2("2")
Run Code Online (Sandbox Code Playgroud)
他们打印 12
Scala如何定义一种返回值?
def sqrt = (x: Int) =>
if (x > 0 && x < 4)
x * x
Run Code Online (Sandbox Code Playgroud)
我的代码的返回类型是Int => AnyVal.但如果我把它改成
def method = (x: String) =>
if (x.equals("abc"))
x.concat(x)
Run Code Online (Sandbox Code Playgroud)
返回类型将是String => Any.但为什么不AnyRef呢?String是一个对象,因此最好使用它AnyRef.我错了吗?
我想知道如何用getter/setter将任何实例包装到原子包装器中以使该实例线程安全吗?
private Console console;
...
@Override
public Callable<Boolean> execute() {
return new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
console.print("Hi!");
// some logic
console.print("Bye!");
return true;
}
};
}
Run Code Online (Sandbox Code Playgroud)
我需要让Console线程安全.
我尝试用类型指定组合两个函数.
foo :: Num a => a -> a
foo a = a + 2
bar :: Num a => a -> a
bar a = a * 2
fooBarCompose :: (Num a, Num b, Num c) => (a -> b) -> (c -> a) -> c -> b
fooBarCompose f g = f . g
Run Code Online (Sandbox Code Playgroud)
我的模块编译,但在运行时我调用
fooBarCompose bar foo
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
No instance for (Show (b0 -> b0))
(maybe you haven't applied enough arguments to a function?)
arising from a …Run Code Online (Sandbox Code Playgroud) 是否可以制作Level一个Eq和Ord?的实例?
instance Eq Ord Level where
compare First Second = LT
...
Run Code Online (Sandbox Code Playgroud)
我试过这个
instance (Ord, Eq) => Level where ...
Run Code Online (Sandbox Code Playgroud)
但我有一个错误
‘compare’ is not a (visible) method of class ‘Level’
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用deriving (Eq)我的data Level.但我不能改变我的data.
我正在尝试的只是通过立即调用函数在控制台中打印消息.这是一个例子:
function (a){
return function (a){
console.log('msg' + a);
}
}(3);
Run Code Online (Sandbox Code Playgroud)
PS
请不要过快评判我,我只是开始学习JavaScript.