内存如何分配以及存储在哪里? :Java继承 [英] How memory gets allocated and what gets stored where? : Java Inheritance
本文介绍了内存如何分配以及存储在哪里? :Java继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个特定的Java继承问题。
以下是我的代码
Hi i have a specific question for inheritance in Java. following is my code
class Parent{
int x = 5;
public void method(){
System.out.println("Parent"+ x);
}
}
public class Child extends Parent{
int x = 4;
public void method(){
System.out.println("Child"+ x);
}
public static void main(String[] args){
Parent p = new Child();
System.out.println(((Child) p).x);
System.out.println(p.x);
}
}
现在我的问题是实际发生在幕后的情况运行这个程序。
Now my question is what happens actually behind the scene while running this program.
- 继承了什么?
- 在内存位置的哪个位置?
- 为什么第一个syso给出4,第二个给出5?(这个我能够在某种程度上理解
但是对上面两个的澄清将有助于
更清楚地理解它)
请指导
推荐答案
在Java中没有变量覆盖,只有方法覆盖。
System.out.println(((Child) p).x);
该行告诉点 p
到 Child
并获取 x
变量。
That line telling point p
to the Child
and get that x
variable.
行 System.out.println(px);
告诉打印 Parent
's x
这篇关于内存如何分配以及存储在哪里? :Java继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文