前几天去面试,遇到一个关于java继承的题目,跟大家分享下。大牛就不用看啦,只是检查一下大家的基础。题目如下:
有三个类,一父二子,最好调用看执行结果:
父类:
1.public class Father { 2. static { 3. System.out.println("Father static Create"); 4. } 5. { 6. System.out.println("Father Create"); 7. } 8. 9. public static void StaticFunction(){ 10. System.out.println("Father static Function"); 11. } 12. 13. public void Function(){ 14. System.out.println("Father Function"); 15. } 16.}
子类1
1.public class ChildOne extends Father {
2. static {
3. System.out.println("ChildOne static Create");
4. }
5. {
6. System.out.println("ChildOne Create");
7. }
8.
9. public static void StaticFunction(){
10. System.out.println("ChildOne static Function");
11. }
12.
13.}
子类2
1.public class ChildTwo extends Father {
2. static {
3. System.out.println("ChildTwo static Create");
4. }
5. {
6. System.out.println("ChildTwo Create");
7. }
8.
9. public static void StaticFunction() {
10. System.out.println("ChildTwo static Function");
11. }
12.
13. public void Function() {
14. System.out.println("ChildTwo Function");
15. }
16.}
测试类:
1.public class Main {
2. public static void main(String[] args) {
3. Father A = new ChildOne();
4. Father B = new ChildTwo();
5. A.StaticFunction();
6. A.Function();
7. B.StaticFunction();
8. B.Function();
9. }
10.}
最后的输出结果请大家写一下,不用去调试,凭自己的理解写写试试