结果怎么会是这样的呢?
class Parent {
int a = 55;public void function() {
System.out.println("Parent's function()");
}Parent() {
function();
}
}public class Child extends Parent {
int a = 15;public static void main(String argv[]) {
Parent p = new Child();
System.out.println(p.a);
p.function();
}public void function() {
System.out.println("Child's function()");
}
}结果怎么会是这样的呢?Child's function()
55
Child's function()
请教名位高手。
Parent's function()
55
Parent's function()
仔细读一下Java Language Specification,关于Field hiding和Method hiding的部分,讲的很清楚,但是不是那么容易理解,自己慢慢想想。
55
Child's function()
输出内容中第一行和第三行都好理解,第二行为什么会是55
而不是15,继承时对子类和父类中的同名变量会怎么处理啊?
期待答案...
(http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html)
8.3.3 Examples of Field Declarations
8.3.3.2 Example: Hiding of Instance Variables
8.4.8 Inheritance, Overriding, and Hiding
8.4.9 Overloading
8.4.10 Examples of Method Declarations
解释可能有点牵强或错误,但同样作为初学者,大家一同进步。我错的地方大家指正。谢谢。Parent p = new Child();
子类里这行代码意思是创建一个父类类型的变量p,他指向的是子类构造的一个对象。
new的同时他必须指向一个构造器.由于P是父类类型的。所以指向的是父类的构造器,也就是
Parent() {function();}但是子类发生了方法覆盖`所以第一个打印出来的是Child's function()System.out.println(p.a);
这行代码打印出的是55,我觉得可以这么解释,但不知道对不对..
P指向的是子类构造出的对象。但他本身是父类类型的。所以打印的时候应该按照父类里面的a来打印。。p.function();
这个就好理解了..P指向的新构造的对象..所以打印子类的方法。。我自己写了一个跟你差不多的。运行结果是一样的。。求高手看一下我的解释`有错误请痛骂我。
这样楼主的问题就解决拉。
---------------------------------------------------
class Parent {
int a = 55; public void function() {
System.out.println("Parent's function()");
} Parent() {
super(); // Inserted by compiler
function();
}
}public class Child extends Parent {
int a = 15; public static void main(String argv[]) {
Parent p = new Child();
System.out.println(p.a);
p.function();
} // Default constructor, inserted by compiler
public Child() {
super();
} public void function() {
System.out.println("Child's function()");
}
}
-----------------------------------------------------
Parent p = new Child();When this line is executed, methods invokation is like this:
1, constructor of class Child;
2, constructor of class Parent, invoked by super() in Child constructor;
3, constructor of class Object, invoked by super() in Parent constructor;
4, return from constructor of class Object;
5, function();
6, return of constructor of class Parent;
7, return of constructor of class Child;Step 5 is the confusing step. Which function() is invoked? According to the instance method overridding and hiding rules, the actual method that will be invoked is determined by the runtime type of the executing object instance. What we have here is an instance of class Child, so function() of class Child is invoked, i.e. "Child's function" is printed as the first line of output.
---------------------------------------------------------------------
System.out.println(p.a);Which variable a should be printed? Both Child and Parent have an instance variable named a. According to the instance variable hiding rules, the actual instance variable is determined by the reference variable type. What's the type of reference variable p? It's Parent, so instance variable a in class Parent is printed.----------------------------------------------------------------------
p.function();This one is similar to line 1, easy to understand.