package com.industry;
public class SuperClass {
public int n;
public SuperClass() {
n = 1;
System.out.println("n is " + n);
}
public int getN() {
return n;
}
}
package com.industry;
public class SubClass extends SuperClass{
public int n = 2;
public SubClass() {
System.out.println("n is " + n);
}
public int getN() {
return n;
}
public static void main(String[] args) {
SuperClass superClass = new SuperClass();
SuperClass subClass = new SubClass();
System.out.println(superClass.n);
System.out.println(subClass.n);
System.out.println(superClass.getN());
System.out.println(subClass.getN());
}
}
问题:写出执行结果是什么?
答案如下:
n is 1
n is 1
n is 2
1
1
1
2