public class Test {
- public static void main(String[] args) {
-
- Man p = new Man(); //Man
- p.printI(); //1
- p.printII(); //0
-
- }
}
class Person {
int i = 0;
- public Person(){
- System.out.println(this.getClass());
- }
-
- void printI(){
- System.out.println(i);
- }
-
- void printII(){
- System.out.println(i);
- }
}
class Man extends Person {
int i = 1;
- void printI(){
- System.out.println(i);
- }
}
疑问有二:
1. 在Person的构造方法中,理论上Man对象还不存在才对,怎么还会打印出Man。另外this一般理解为调用该方法的对象,那么我觉得这个方法应该是由Person调用的,应该会打印Person才对。
2. printI和printII为什么打印的是不一样的呢。
问题补充:
在构造函数中的this到底是指什么呢?在实例化子类的时候不是要先实例化父类吗?那么这个构造方法到底是由谁来调用的呢。