zoukankan      html  css  js  c++  java
  • java 接口与继承

    一、继承条件下的构造方法调用

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

    1)  源代码

    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();

        }

    }

    2)运行结果截图

    3)结论

    通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句。

    一、参看ExplorationJDKSource.java示例

    1)  源代码

    public class ExplorationJDKSource {

     

        /**

         * @param args

         */

        public static void main(String[] args) {

            System.out.println(new A());

        }

     

    }

    class A{}

    2)  运行结果截图

     

    3)结论

    前面示例中,main方法实际上调用的是:

    public void println(Object x),这一方法内部调用了String类的valueOf方法。

    valueOf方法内部又调用Object.toString方法:

    public String toString() {

             return getClass().getName() +"@" +

             Integer.toHexString(hashCode());

    }

    hashCode方法是本地方法,由JVM设计者实现:

    public  native int hashCode();

  • 相关阅读:
    java中 Hex(十六进制)和byte[]相互转换
    byte[]和File相互转换
    Stringboot MultiPartFile 转 File
    mysql8.0+运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 解决办法
    idea激活
    mysql8安装成功过后navicat连接失败
    AJAX的使用
    单文件上传和多文件上传实例
    监听器的使用
    SQL分页技术
  • 原文地址:https://www.cnblogs.com/420Rock/p/4948947.html
Copyright © 2011-2022 走看看