zoukankan      html  css  js  c++  java
  • Javap课堂练习——接口与继承

    运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大

    class Grandparent {

        public Grandparent() {

            System.out.println("GrandParent Created.");

        }

        public Grandparent(String string) {

            System.out.println("GrandParent Created.String:" + string);

        }

    }

    class Parent extends Grandparent {

        public Parent() {

            //super("Hello.Grandparent.");

            System.out.println("Parent Created");

           // super("Hello.Grandparent.");

        }

    }

    class Child extends Parent {

        public Child() {

            System.out.println("Child Created");

        }

    }

    public class Javapingshi {

        public static void main(String args[]) {

            Child c = new Child();

        }

    }

    结果截图

    在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

    class Grandparent {

        public Grandparent() {

            System.out.println("GrandParent Created.");

        }

        public Grandparent(String string) {

            System.out.println("GrandParent Created.String:" + string);

        }

    }

    class Parent extends Grandparent {

        public Parent() {

            super("Hello.Grandparent.");

            System.out.println("Parent Created");

           // super("Hello.Grandparent.");

        }

    }

    class Child extends Parent {

        public Child() {

            System.out.println("Child Created");

        }

    }

    public class TestInherits {

        public static void main(String args[]) {

            Child c = new Child();

        }

    }

    程序截图

  • 相关阅读:
    python字符串操作
    老男孩购物车程序
    python数据类型,判断,循环
    Matplotlib 绘图参考手册
    numpy 基础知识
    numpy random 模块
    numpy 算术运算
    pandas 读写数据
    python 读写文本
    python--windows文件操作
  • 原文地址:https://www.cnblogs.com/qq1499632156/p/6053883.html
Copyright © 2011-2022 走看看