一个关于java继承的题目

 2022-09-19
原文地址:https://blog.51cto.com/u_15762357/5613203

复制来的:

一个关于java继承的题目,跟大家分享下。题目如下:

有三个类,一父二子:
父类:

Java代码

    1. public class Father {
    2. static {
    3. "Father static Create");
    4. }
    5. {
    6. "Father Create");
    7. }
    8. 
    9. public static void StaticFunction(){
    10. "Father static Function");
    11. }
    12. 
    13. public void Function(){
    14. "Father Function");
    15. }
    16. }

子类1

Java代码

    1. public class ChildOne extends Father {
    2. static {
    3. "ChildOne static Create");
    4. }
    5. {
    6. "ChildOne Create");
    7. }
    8. 
    9. public static void StaticFunction(){
    10. "ChildOne static Function");
    11. }
    12. 
    13. }

子类2

Java代码

    1. public class ChildTwo extends Father {
    2. static {
    3. "ChildTwo static Create");
    4. }
    5. {
    6. "ChildTwo Create");
    7. }
    8. 
    9. public static void StaticFunction() {
    10. "ChildTwo static Function");
    11. }
    12. 
    13. public void Function() {
    14. "ChildTwo Function");
    15. }
    16. }

调用方法

Java代码

    1. public class Main {
    2. public static void main(String[] args) {
    3. new ChildOne();
    4. new ChildTwo();
    5. A.StaticFunction();
    6. A.Function();
    7. B.StaticFunction();
    8. B.Function();
    9. }

输出结果:

    Father static Create
    ChildOne static Create
    Father Create
    ChildOne Create
    ChildTwo static Create
    Father Create
    ChildTwo Create
    Father static Function
    Father Function
    Father static Function
    ChildTwo Function