gpt4 book ai didi

java - java中的静态类

转载 作者:行者123 更新时间:2023-11-29 06:39:40 25 4
gpt4 key购买 nike

package shubh;
class thread5
{
static class a7 extends Thread
{
public void run()
{
for(int i=0;i<=10;i++)
{
System.out.println(i);
}
}
}

static class a8 implements Runnable
{
public void run()
{
for(int i=21;i<=30;i++)
{
System.out.println(i);
}
}
}

public static void main(String arg[])
{
a7 a=new a7();
a.start();
a8 b=new a8();
Thread th=new Thread(b);
th.start();
for(int i=40;i<=50;i++)
{
System.out.println(i);
}
}
}

当我从 2 个嵌套类中删除 static 关键字时,出现错误 无法从静态上下文中引用非静态变量 任何人都可以向我解释此错误的含义或为什么有必要给出嵌套类为静态

最佳答案

如果你删除了 static 那么你需要一个实际的对象来实例化内部类。即
new thread5().new a7();

关于java - java中的静态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045929/

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