java 静态方法和非静态变量的问题
publicclassBeetleextendsInsect{privateintk=print("Beetle.kinitialized");publicBeetle(...
public class Beetle extends Insect {
private int k = print("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static void dd()
{
System.out.println("dd ");
}
private static int x2 =
print("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
}
}
class Insect {
private int i = 9;
protected int j;
private int p=print(" Insect.p initialized");
private static int x1 =print("static Insect.x1 initialized");
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
static int print(String s) {
System.out.println(s);
return 47;
}
}
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
Insect.p initialized
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
运行结果如下 其中不明白的是
K怎么等于47的, static int print(String s)这个是静态办法 private int k = print("Beetle.k initialized");这个是非静态属性, 展开
private int k = print("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static void dd()
{
System.out.println("dd ");
}
private static int x2 =
print("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
}
}
class Insect {
private int i = 9;
protected int j;
private int p=print(" Insect.p initialized");
private static int x1 =print("static Insect.x1 initialized");
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
static int print(String s) {
System.out.println(s);
return 47;
}
}
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
Insect.p initialized
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
运行结果如下 其中不明白的是
K怎么等于47的, static int print(String s)这个是静态办法 private int k = print("Beetle.k initialized");这个是非静态属性, 展开
1个回答
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询