java中的以下代码,当在elipse上运行时,即使我们替换也会提供相同的输出
superclass s=new sub();
Run Code Online (Sandbox Code Playgroud)
用,
sub s= new sub();
Run Code Online (Sandbox Code Playgroud)
请注意,我们已经重写了方法.
输出是:
changed supermethod in sub class
num is sub class 5
Run Code Online (Sandbox Code Playgroud)
码:
public class superclass {
int num=2;
public static void main(String str[]){
superclass s=new sub();
//HERE: nothing changes if we write, sub s=new sub();
s.supermethod();
s.method();
}
void supermethod(){
System.out.println("supermethod as in superclass");
}
void method(){
System.out.println("num in superclass "+num);
}
}
class sub extends superclass{
int num=5;
void method(){
System.out.println("num is sub class "+num);
}
void supermethod(){
System.out.println("changed …Run Code Online (Sandbox Code Playgroud) 基本上这个代码有两个在两个类中创建的线程,它们是从第三个类中调用的.每个线程都有一个循环,并在每次迭代后休眠.
(代码到底)
输出是:
CHECK 0 CHECK
CHECK 1 CHECK
run one
in thread1
CHECK 2 CHECK
run two
in thread2
Run Code Online (Sandbox Code Playgroud)
1)我不知道它为什么会这样运作.我的意思是可以先打印CHECK 0 CHECK.但是为什么CHECK 1 CHECK在Thread1之前打印(而在代码中调用Thread1之后),同样用于CHECK 2 CHECK和Thread2?
2)如果我用System.exit(0)替换CHECK 2 CHECK,如上例所示,在运行Thread2之前打印CHECK 2 CHECK(在Thread2旁边),为什么System.exit(0)在发生之后发生在这种情况下运行Thread2?
第二种情况的输出:
CHECK 0 CHECK
CHECK 1 CHECK
run one
in thread1
run two
in thread2
Run Code Online (Sandbox Code Playgroud)
请告诉我为什么会这样?为什么方法中的线程和代码会以这种方式混淆?我想我对java如何管理线程一无所知.我试了很多,但找不到任何我能理解的东西.
码:
public class Thread1 implements Runnable
{
public Thread1()
{
new Thread(this).start();
}
public void run()
{
// TODO Auto-generated method stub
System.out.println("run one");
try
{
for(int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我在全球范围内宣布了这种结构,
struct a{
int a;
int x;
union b{
int a;
int b;
int y;
};
};
Run Code Online (Sandbox Code Playgroud)
然后在主要内部宣布一个联盟,
union b a;
Run Code Online (Sandbox Code Playgroud)
这并没有给出任何错误.但是如果在结构定义中声明联合'a',例如:
struct a{
int a;
int x;
union b{
int a;
int b;
int y;
}a;
};
Run Code Online (Sandbox Code Playgroud)
它给出错误"重复成员a".(我们在以前的案例中使用了同一个名称'为什么一个人工作而另一个人不工作?
其次,我们如何独立地使用struct内部声明的union,但不能使用任何其他整数变量,比如'x'?就像,我可以成功执行以下操作:
union b z; //works in this case, BUT not if we declare 'z' with the definition.
z.y=6; //works
x=6; //gives error
Run Code Online (Sandbox Code Playgroud)
(我知道我们在main中声明了union,但是它在struct中的定义.就像struct.union.union_variable有意义一样,但是union.union_variable直接使它变得独立.不应该把它当作'x'来对待?)