我遇到了关于网络上遗传的这个例子,我对它的结果并不容易.我错过了一些关键的东西.
public class A {
int i = 5;
public A() {
foo();
}
private void foo() {
System.out.println(i);
}}
public class B extends A {
int i = 6;}
public class C extends B {
int i = 7;
public void foo() {
System.out.println("C's foo");
System.out.println(super.i);
}}
Run Code Online (Sandbox Code Playgroud)
我试图通过以下命令弄清楚发生了什么: C c = new C();
System.out.println(C.i);
我知道当我们创建一个新的实例时,C我们接近A和B的构造,所以我们达到A()- (问题1)iA(A)是否在路上初始化?现在我们需要打电话给foo()- (问题2) - C是否foo()认为是A的覆盖foo()?如果B有foo()自己的怎么办?然后它被认为是一个覆盖和C的foo()运作?
据我所知,当它与局部变量相关时没有覆盖.怎么会System.out.println(c.i)是7而不是5?不应该i …
出什么问题了
private Map<List<K>, V> multiMap= new HashMap<ArrayList<K>,V>();
Run Code Online (Sandbox Code Playgroud)
编者说它Type mismatch: cannot convert from HashMap<ArrayList<K>,V> to Map<List<K>,V>.我是否必须提供特定类别的列表?为什么?
double d=1/0.0;
System.out.println(d);
Run Code Online (Sandbox Code Playgroud)
它打印Infinity,但如果我们将编写double d=1/0;并打印它,我们将得到这个例外:Exception
in thread "main" java.lang.ArithmeticException: / by zero
at D.main(D.java:3)为什么Java在一种情况下知道潜水为零是无穷大但是对于int 0它没有定义?在这两种情况下,d都是双倍的,在两种情况下,结果都是无穷大.
public class D {
void myMethod() {
try {
throw new IllegalArgumentException();
} catch (NullPointerException npex) {
System.out.println("NullPointerException thrown ");
} catch (Exception ex) {
System.out.println("Exception thrown ");
} finally {
System.out.println("Done with exceptions ");
}
System.out.println("myMethod is done");
}
public static void main(String args[]) {
D d = new D();
d.myMethod();
}
Run Code Online (Sandbox Code Playgroud)
}
我不明白怎么"myMethod is done"也被打印出来.抛出了异常,因此它假设找到一个匹配的catch并执行finally块,但它继续在该myMethod方法上打印myMethod is done,而不是finally块的一部分.为什么?
public class Stack<E> {
public Stack () {....}
public void push (E e) {....}
public E pop () {....}
public boolean isEmpty(){....}
}
public void pushAll (Collection<E> src) {
for (E e: src){
push(e)
}
}
Run Code Online (Sandbox Code Playgroud)
如果我写的话,我不明白会出现什么问题
Stack<number> numberStack = new Stack<Number>();
Collection<Integer> integers=...
numberStack.pushAll(integers);
Run Code Online (Sandbox Code Playgroud)
整数扩展了Number,所以我可以添加一个Integers集合numberStack.但我被告知这是一个错误汇编 - 为什么?
我找不到这个界面或其他相关信息:
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Your name is " + text.getText());
shell.close();
}
});
Run Code Online (Sandbox Code Playgroud)
这个接口是否命名SelectionListener?((i.1) - 为什么我不能在网上找到它?因为它不是通用的?)
这个selectionAapter是一个*抽象类*,我用匿名类实现?
为什么我更喜欢这个适配器而不是处理程序外部类?
非常感谢!
我写了以下内容:
#include <stdio.h>
#include <string.h>
char* getString();
char* getString(){
char str[10];
gets(str);
return str;
}
int main() {
char* s;
s=getString();
strcpy(s,"Hi");
puts(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道长度str必须小于10,但即使我只写"嗨",也没有任何东西被打印出来.据我所知,它应该是好的.编译器说fgets is dangerous and should not be used.
什么原因没有在屏幕上打印?
public class A {
public void foo() {
System.out.println("A's foo");
}
}
public class B extends A {
public void foo() {
System.out.print("B's foo");
}
}
public class Test1 {
public static void main(String[] args){
A a= new B();
a.foo();
}
}
Run Code Online (Sandbox Code Playgroud)
我想用A的foo,这样做的语法是什么?我试过了a.super.foo();.
谢谢
我可以在Web上的几个例子中看到一个新的语法,这是一个例子:
Accumulator<Integer> sum = new Accumulator<Integer>(){
public Integer accumulate(Integer t1, Integer t2) {
return t1+t2;
}
};
Run Code Online (Sandbox Code Playgroud)
一般来说,{ a method }在创建一个类的实例之后写一个是什么意思?是什么东西的某种旧语法?
谢谢
我想在文件的开头写一个字符串,我该怎么做?
我根本不知道如何添加字符串..这是我到目前为止所做的:
public static void prepend (String filename, String data) throws IOException{
FileOutputStream file= new FileOutputStream(filename);
}
Run Code Online (Sandbox Code Playgroud)
(i)该write()方法只接受字节 - 在我的情况下,我应该怎么做才能使用它?
(ii) - 如何将字符串复制到文件的开头?
而且 - 如果有人知道一个网站上有所有的作家\读者和所有这些流被安排和很好地解释 - 我会非常感激,我会失去理智.
谢谢!