gpt4 book ai didi

java - Java中的线程继承

转载 作者:行者123 更新时间:2023-11-29 05:42:31 24 4
gpt4 key购买 nike

   class ThreadInherit extends Helper implements Runnable 
{

@Override
public void run() {
// TODO Auto-generated method stub
try {
for (int i = 1; i <= 20; i++) {
System.out.println("Hello World");
Thread.sleep(500);
if (i == 10) {
Notify();
Wait();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Thread_Inherit1 extends ThreadInherit
{
@Override
public void run() {
// TODO Auto-generated method stub
try {
Wait();
for(int i = 1; i<=20; i++)
{
System.out.println("Welcome");
Thread.sleep(550);
}
Notify();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Helper
{
public synchronized void Wait() throws InterruptedException
{
wait();
}
public synchronized void Notify() throws InterruptedException
{
notify();
}
}
public class Inheritance_Class {

public static void main (String[] args)
{
Thread_Inherit1 u = new Thread_Inherit1();
Thread_Inherit1 t = new ThreadInherit();
t.run();
u.run();

}

}

所以我有这段代码...我这条线有问题

  Thread_Inherit1 t = new ThreadInherit();

不能转换为Thread_Inherit1。Eclipse 的建议:

1.Add cast to Thread_Inherit1(我做了,但仍然报错)2. 改为ThreadInherit。 (可能不是解决方案)

有什么帮助吗?

最佳答案

您的代码有:

class Thread_Inherit1 extends ThreadInherit 

...你的问题是:

Thread_Inherit1 t = new ThreadInherit();

麻烦的是事情倒退了。 ThreadInherit 不是 thread_Inherit1。我想你想写的那一行是:

ThreadInherit t = new Thread_Inherit1();

希望这能解决您的问题。

关于java - Java中的线程继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16974206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com